Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: improve interrupt handling #996

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions internal/http/rdcservice.go
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,12 @@ func (c *RDCService) PollJob(ctx context.Context, id string, interval, timeout t

for {
select {
case <-ctx.Done():
return job.Job{ID: id}, ctx.Err()
case <-ticker.C:
j, err := c.Job(ctx, id, realDevice)
if err != nil {
return job.Job{}, err
return job.Job{ID: id}, err
}

if job.Done(j.Status) {
Expand All @@ -280,7 +282,7 @@ func (c *RDCService) PollJob(ctx context.Context, id string, interval, timeout t
case <-deathclock.C:
j, err := c.Job(ctx, id, realDevice)
if err != nil {
return job.Job{}, err
return job.Job{ID: id}, err
}
j.TimedOut = true
return j, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/http/rdcservice_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,21 +208,21 @@ func TestRDCService_PollJob(t *testing.T) {
name: "job not found error from external API",
client: rdc,
jobID: "3",
expectedResp: job.Job{},
expectedResp: job.Job{ID: "3"},
expectedErr: ErrJobNotFound,
},
{
name: "http status is not 200, but 401 from external API",
client: rdc,
jobID: "4",
expectedResp: job.Job{},
expectedResp: job.Job{ID: "4"},
expectedErr: errors.New("unexpected statusCode: 401"),
},
{
name: "unexpected status code from external API",
client: rdc,
jobID: "333",
expectedResp: job.Job{},
expectedResp: job.Job{ID: "333"},
expectedErr: errors.New("internal server error"),
},
{
Expand Down
6 changes: 4 additions & 2 deletions internal/http/resto.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,12 @@ func (c *Resto) PollJob(ctx context.Context, id string, interval, timeout time.D

for {
select {
case <-ctx.Done():
return job.Job{ID: id}, ctx.Err()
case <-ticker.C:
j, err := c.Job(ctx, id, realDevice)
if err != nil {
return job.Job{}, err
return job.Job{ID: id}, err
}

if job.Done(j.Status) {
Expand All @@ -139,7 +141,7 @@ func (c *Resto) PollJob(ctx context.Context, id string, interval, timeout time.D
case <-deathclock.C:
j, err := c.Job(ctx, id, realDevice)
if err != nil {
return job.Job{}, err
return job.Job{ID: id}, err
}
j.TimedOut = true
return j, nil
Expand Down
6 changes: 3 additions & 3 deletions internal/http/resto_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,21 +226,21 @@ func TestResto_GetJobStatus(t *testing.T) {
name: "user not found error from external API",
client: resto,
jobID: "3",
expectedResp: job.Job{},
expectedResp: job.Job{ID: "3"},
expectedErr: ErrJobNotFound,
},
{
name: "http status is not 200, but 401 from external API",
client: resto,
jobID: "4",
expectedResp: job.Job{},
expectedResp: job.Job{ID: "4"},
expectedErr: errors.New("job status request failed; unexpected response code:'401', msg:''"),
},
{
name: "unexpected status code from external API",
client: resto,
jobID: "333",
expectedResp: job.Job{},
expectedResp: job.Job{ID: "333"},
expectedErr: errors.New("internal server error"),
},
{
Expand Down
2 changes: 1 addition & 1 deletion internal/job/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,5 +115,5 @@ type Service interface {

// DownloadArtifacts downloads artifacts from a Job. Returns a list of
// file paths.
DownloadArtifacts(job Job, isLastAttempt bool) []string
DownloadArtifacts(ctx context.Context, job Job, isLastAttempt bool) []string
}
8 changes: 3 additions & 5 deletions internal/mocks/job.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type FakeJobService struct {
PollJobFn func(ctx context.Context, id string, interval time.Duration, timeout time.Duration) (job.Job, error)

UploadAssetFn func(ctx context.Context, jobID string, realDevice bool, fileName string, contentType string, content []byte) error
DownloadArtifactFn func(job job.Job, isLastAttempt bool) []string
DownloadArtifactFn func(ctx context.Context, job job.Job, isLastAttempt bool) []string
GetJobAssetFileNamesFn func(ctx context.Context, jobID string) ([]string, error)
GetJobAssetFileContentFn func(ctx context.Context, jobID, fileName string) ([]byte, error)
}
Expand All @@ -34,10 +34,8 @@ func (s *FakeJobService) StopJob(
return s.StopJobFn(ctx, jobID, realDevice)
}

func (s *FakeJobService) DownloadArtifacts(
job job.Job, isLastAttempt bool,
) []string {
return s.DownloadArtifactFn(job, isLastAttempt)
func (s *FakeJobService) DownloadArtifacts(ctx context.Context, job job.Job, isLastAttempt bool) []string {
return s.DownloadArtifactFn(ctx, job, isLastAttempt)
}

// Job mock function
Expand Down
Loading
Loading