Skip to content

Commit

Permalink
ResetTask before starting
Browse files Browse the repository at this point in the history
  • Loading branch information
jh-bate committed Aug 22, 2023
1 parent b3d4798 commit 60e40d8
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 8 deletions.
8 changes: 4 additions & 4 deletions dexcom/fetch/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,16 +124,16 @@ func (r *Runner) Run(ctx context.Context, tsk *task.Task) bool {
}

if !skipToAvoidDexcomBackup {
tsk.ClearError()
ResetTask(tsk)

if serverSessionToken, sErr := r.AuthClient().ServerSessionToken(); sErr != nil {
SetErrorOrAllowTaskRetry(tsk, errors.Wrap(sErr, "unable to get server session token"))
ErrorOrRetryTask(tsk, errors.Wrap(sErr, "unable to get server session token"))
} else {
ctx = auth.NewContextWithServerSessionToken(ctx, serverSessionToken)
if taskRunner, tErr := NewTaskRunner(r, tsk); tErr != nil {
SetErrorOrAllowTaskRetry(tsk, errors.Wrap(tErr, "unable to create task runner"))
ErrorOrRetryTask(tsk, errors.Wrap(tErr, "unable to create task runner"))
} else if tErr = taskRunner.Run(ctx); tErr != nil {
SetErrorOrAllowTaskRetry(tsk, errors.Wrap(tErr, "unable to run task runner"))
ErrorOrRetryTask(tsk, errors.Wrap(tErr, "unable to run task runner"))
}
}
}
Expand Down
7 changes: 6 additions & 1 deletion dexcom/fetch/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func NewTaskCreate(providerSessionID string, dataSourceID string) (*task.TaskCre
}, nil
}

func SetErrorOrAllowTaskRetry(t *task.Task, err error) {
func ErrorOrRetryTask(t *task.Task, err error) {
if shouldTaskError(t) {
t.AppendError(err)
t.SetFailed()
Expand All @@ -42,6 +42,11 @@ func SetErrorOrAllowTaskRetry(t *task.Task, err error) {
incrementTaskRetryCount(t)
}

func ResetTask(t *task.Task) {
t.ClearError()
t.Data[dexcomTaskRetryField] = 0
}

func shouldTaskError(t *task.Task) bool {
if t.Data[dexcomTaskRetryField] != nil {
count, ok := t.Data[dexcomTaskRetryField].(int)
Expand Down
18 changes: 15 additions & 3 deletions dexcom/fetch/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ var _ = Describe("Task", func() {
}
}

Context("SetErrorOrAllowTaskRetry", func() {
Context("ErrorOrRetryTask", func() {
DescribeTable("will not append error",
func(setupFunc func() (*task.Task, int)) {
tsk, startCount := setupFunc()
Expect(tsk.Data["retryCount"]).To(Equal(startCount))
fetch.SetErrorOrAllowTaskRetry(tsk, errors.New("some error"))
fetch.ErrorOrRetryTask(tsk, errors.New("some error"))
Expect(tsk.HasError()).To(Equal(false))
Expect(tsk.IsFailed()).To(Equal(false))
Expect(tsk.Data["retryCount"]).To(Equal(startCount + 1))
Expand All @@ -45,7 +45,7 @@ var _ = Describe("Task", func() {
func(setupFunc func() (*task.Task, int)) {
tsk, startCount := setupFunc()
Expect(tsk.Data["retryCount"]).To(Equal(startCount))
fetch.SetErrorOrAllowTaskRetry(tsk, errors.New("some error"))
fetch.ErrorOrRetryTask(tsk, errors.New("some error"))
Expect(tsk.HasError()).To(Equal(true))
Expect(tsk.IsFailed()).To(Equal(true))
},
Expand All @@ -58,6 +58,18 @@ var _ = Describe("Task", func() {
)
})

Context("ResetTask", func() {
It("returns the unit value when set", func() {
tsk := getTask(3)
tsk.AppendError(errors.New("some error"))
Expect(tsk.HasError()).To(Equal(true))
Expect(tsk.Data["retryCount"]).To(Equal(3))
fetch.ResetTask(tsk)
Expect(tsk.HasError()).To(Equal(false))
Expect(tsk.Data["retryCount"]).To(Equal(0))
})
})

Context("NewTaskCreate", func() {
const providerID = "some-provider-id"
const sourceID = "some-source-id"
Expand Down

0 comments on commit 60e40d8

Please sign in to comment.