Skip to content

Commit

Permalink
Refactor retries
Browse files Browse the repository at this point in the history
  • Loading branch information
Quinn-With-Two-Ns committed Sep 25, 2023
1 parent 7857f03 commit 20ee51e
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 25 deletions.
5 changes: 2 additions & 3 deletions session-failure/starter/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,12 @@ func main() {
}
defer c.Close()

fileID := uuid.New()
workflowOptions := client.StartWorkflowOptions{
ID: "session_failure_" + fileID,
ID: "session_failure_" + uuid.New(),
TaskQueue: "session-failure",
}

we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, sessionfailure.SampleSessionFailureRecoveryWorkflow, fileID)
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, sessionfailure.SampleSessionFailureRecoveryWorkflow)
if err != nil {
log.Fatalln("Unable to execute workflow", err)
}
Expand Down
36 changes: 15 additions & 21 deletions session-failure/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,32 +13,26 @@ var (
)

// SampleSessionFailureRecoveryWorkflow workflow definition
func SampleSessionFailureRecoveryWorkflow(ctx workflow.Context, fileName string) (err error) {

err = runSession(ctx, fileName)
numOfRetries := 10
for err != nil && numOfRetries >= 0 {
// Only retry if we detected the session failed. In a production application
// it may make sense to also retry if some other errors occur, it
// depends on your business logic.
if errors.Is(err, ErrSessionHostDown) {
workflow.Sleep(ctx, 5*time.Minute)
err = runSession(ctx, fileName)
func SampleSessionFailureRecoveryWorkflow(ctx workflow.Context) (err error) {
for retryNum := 0; retryNum < 10; retryNum++ {
if err = runSession(ctx); errors.Is(err, ErrSessionHostDown) {
if sleepErr := workflow.Sleep(ctx, 5*time.Minute); sleepErr != nil {
return sleepErr
}
continue
}
if err != nil {
workflow.GetLogger(ctx).Error("Workflow failed.", "Error", err.Error())
} else {
break
workflow.GetLogger(ctx).Info("Workflow completed.")
}
numOfRetries--
}

if err != nil {
workflow.GetLogger(ctx).Error("Workflow failed.", "Error", err.Error())
} else {
workflow.GetLogger(ctx).Info("Workflow completed.")
return
}
return err
workflow.GetLogger(ctx).Error("Workflow failed after multiple session retries.", "Error", err.Error())
return
}

func runSession(ctx workflow.Context, fileName string) (err error) {
func runSession(ctx workflow.Context) (err error) {

so := &workflow.SessionOptions{
CreationTimeout: time.Minute,
Expand Down
2 changes: 1 addition & 1 deletion session-failure/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func (s *UnitTestSuite) Test_SampleFileProcessingWorkflow() {

env.RegisterActivity(a)

env.ExecuteWorkflow(SampleSessionFailureRecoveryWorkflow, "file1")
env.ExecuteWorkflow(SampleSessionFailureRecoveryWorkflow)

s.True(env.IsWorkflowCompleted())
s.NoError(env.GetWorkflowError())
Expand Down

0 comments on commit 20ee51e

Please sign in to comment.