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

[BACK-2633] dexcom tasks #654

Closed
wants to merge 3 commits into from
Closed
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
41 changes: 18 additions & 23 deletions dexcom/fetch/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,7 @@ func (t *TaskRunner) Run(ctx context.Context) error {
}

if len(t.task.Data) == 0 {
t.task.SetFailed()
return errors.New("data is missing")
return t.failTask(errors.New("data is missing"))
}

t.context = ctx
Expand All @@ -204,7 +203,7 @@ func (t *TaskRunner) Run(ctx context.Context) error {
}
if err := t.fetchSinceLatestDataTime(); err != nil {
if request.IsErrorUnauthenticated(errors.Cause(err)) {
t.task.SetFailed()
t.failTask(err)
if updateErr := t.updateDataSourceWithError(err); updateErr != nil {
t.Logger().WithError(updateErr).Error("unable to update data source with error")
}
Expand All @@ -217,16 +216,14 @@ func (t *TaskRunner) Run(ctx context.Context) error {
func (t *TaskRunner) getProviderSession() error {
providerSessionID, ok := t.task.Data["providerSessionId"].(string)
if !ok || providerSessionID == "" {
t.task.SetFailed()
return errors.New("provider session id is missing")
return t.failTask(errors.New("provider session id is missing"))
}

providerSession, err := t.AuthClient().GetProviderSession(t.context, providerSessionID)
if err != nil {
return errors.Wrap(err, "unable to get provider session")
} else if providerSession == nil {
t.task.SetFailed()
return errors.Wrap(err, "provider session is missing")
return t.failTask(errors.Wrap(err, "provider session is missing"))
}
t.providerSession = providerSession

Expand All @@ -247,27 +244,30 @@ func (t *TaskRunner) updateProviderSession() error {
if err != nil {
return errors.Wrap(err, "unable to update provider session")
} else if providerSession == nil {
t.task.SetFailed()
return errors.Wrap(err, "provider session is missing")
return t.failTask(errors.Wrap(err, "provider session is missing"))
}
t.providerSession = providerSession

return nil
}

func (t *TaskRunner) failTask(err error) error {
t.task.SetFailed()
t.logger.Warnf("dexcom task %s failed: %s", t.task.ID, err)
return err
}

func (t *TaskRunner) getDataSource() error {
dataSourceID, ok := t.task.Data["dataSourceId"].(string)
if !ok || dataSourceID == "" {
t.task.SetFailed()
return errors.New("data source id is missing")
return t.failTask(errors.New("data source id is missing"))
}

source, err := t.DataSourceClient().Get(t.context, dataSourceID)
if err != nil {
return errors.Wrap(err, "unable to get data source")
} else if source == nil {
t.task.SetFailed()
return errors.Wrap(err, "data source is missing")
return t.failTask(errors.Wrap(err, "data source is missing"))
}
t.dataSource = source

Expand Down Expand Up @@ -320,8 +320,7 @@ func (t *TaskRunner) updateDataSource(update *dataSource.Update) error {
if err != nil {
return errors.Wrap(err, "unable to update data source")
} else if source == nil {
t.task.SetFailed()
return errors.Wrap(err, "data source is missing")
return t.failTask(errors.Wrap(err, "data source is missing"))
}

t.dataSource = source
Expand All @@ -331,8 +330,7 @@ func (t *TaskRunner) updateDataSource(update *dataSource.Update) error {
func (t *TaskRunner) createTokenSource() error {
tokenSource, err := oauthToken.NewSourceWithToken(t.providerSession.OAuthToken)
if err != nil {
t.task.SetFailed()
return errors.Wrap(err, "unable to create token source")
return t.failTask(errors.Wrap(err, "unable to create token source"))
}

t.tokenSource = tokenSource
Expand All @@ -346,16 +344,14 @@ func (t *TaskRunner) getDeviceHashes() error {
}
rawMap, rawMapOK := raw.(map[string]interface{})
if !rawMapOK || rawMap == nil {
t.task.SetFailed()
return errors.New("device hashes is invalid")
return t.failTask(errors.New("device hashes is invalid"))
}
deviceHashes := map[string]string{}
for key, value := range rawMap {
if valueString, valueStringOK := value.(string); valueStringOK {
deviceHashes[key] = valueString
} else {
t.task.SetFailed()
return errors.New("device hash is invalid")
return t.failTask(errors.New("device hash is invalid"))
}
}

Expand Down Expand Up @@ -390,8 +386,7 @@ func (t *TaskRunner) updateDataSet(dataSetUpdate *data.DataSetUpdate) error {
if err != nil {
return errors.Wrap(err, "unable to update data set")
} else if dataSet == nil {
t.task.SetFailed()
return errors.Wrap(err, "data set is missing")
return t.failTask(errors.Wrap(err, "data set is missing"))
}

t.dataSet = dataSet
Expand Down