Skip to content

Commit

Permalink
Merge pull request #47 from lifeomic/add-retries-to-eventually-consis…
Browse files Browse the repository at this point in the history
…tent-operations

fix: add retries to help with eventual consistency
  • Loading branch information
SchaeStewart authored Oct 12, 2022
2 parents 9300f82 + 48e6360 commit ca471cd
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions internal/provider/resource_marketplace_wellness_offering.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ import (
"github.com/lifeomic/terraform-provider-lifeomic/internal/gqlclient"
)

func retry[T any](fn func() (T, error), maxRetries int, delay time.Duration) (T, error) {
var err error
var res T
for attempts := 0; attempts < maxRetries; attempts++ {
res, err = fn()
if err == nil {
return res, err
}
time.Sleep(delay)
}
return res, err
}

// wellnessOffering represents the state of marketplace_wellness_offering resource
type wellnessOffering struct {
ID types.String `tfsdk:"id"`
Expand Down Expand Up @@ -454,9 +467,11 @@ func (w wellnessOfferingResource) handleApproval(ctx context.Context, plan welln
// If we're using lambda we automatically attempt to publish a review for the module
tflog.Info(ctx, "using lambda detected. Attempting to automatically approve the module")

// TODO: fight the eventual consistency
time.Sleep(5 * time.Second)
assignModuleResp, err := w.clientSet.Marketplace.AssignModuleReviewToSelf(ctx, moduleId)
assignModuleForReview := func() (*gqlclient.AssignModuleReviewToSelfResponse, error) {
return w.clientSet.Marketplace.AssignModuleReviewToSelf(ctx, moduleId)
}

assignModuleResp, err := retry(assignModuleForReview, 10, time.Second*1)
if err != nil {
diags.AddError("failed to assign module for review", err.Error())
return
Expand All @@ -471,9 +486,13 @@ func (w wellnessOfferingResource) handleApproval(ctx context.Context, plan welln
diags.AddError("failed to approve module", err.Error())
return
}

tflog.Info(ctx, "Approved module", map[string]any{"approval": approveResp.ApproveModulePublish})
offering, err := w.clientSet.Marketplace.GetWellnessOfferingModule(ctx, moduleId)

getApprovedModule := func() (*gqlclient.GetWellnessOfferingModuleResponse, error) {
return w.clientSet.Marketplace.GetWellnessOfferingModule(ctx, moduleId)
}

offering, err := retry(getApprovedModule, 10, time.Second*1)
if err != nil {
diags.AddError("failed to get published and approved Wellness Offering Module", err.Error())
return
Expand Down

0 comments on commit ca471cd

Please sign in to comment.