Skip to content

Commit dfead9c

Browse files
author
“Kevin”
committed
Add comments to workflow code for clarity
Signed-off-by: “Kevin” <“[email protected]”>
1 parent d8683f1 commit dfead9c

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

cmd/samples/batch/workflow.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package main
33
import (
44
"context"
55
"fmt"
6+
"math/rand"
67
"time"
78

89
"go.uber.org/cadence/workflow"
@@ -20,31 +21,36 @@ type BatchWorkflowInput struct {
2021
}
2122

2223
func BatchWorkflow(ctx workflow.Context, input BatchWorkflowInput) error {
24+
// Create activity factories for each task (not yet executed)
2325
factories := make([]func(workflow.Context) workflow.Future, input.TotalSize)
2426
for taskID := 0; taskID < input.TotalSize; taskID++ {
25-
taskID := taskID
27+
taskID := taskID // Capture loop variable for closure
2628
factories[taskID] = func(ctx workflow.Context) workflow.Future {
29+
// Configure activity timeouts
2730
aCtx := workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
28-
ScheduleToStartTimeout: time.Minute * 10,
29-
StartToCloseTimeout: time.Minute * 10,
31+
ScheduleToStartTimeout: time.Minute * 1,
32+
StartToCloseTimeout: time.Minute * 1,
3033
})
3134
return workflow.ExecuteActivity(aCtx, BatchActivity, taskID)
3235
}
3336
}
3437

38+
// Execute all activities with controlled concurrency
3539
batch, err := x.NewBatchFuture(ctx, input.Concurrency, factories)
3640
if err != nil {
3741
return fmt.Errorf("failed to create batch future: %w", err)
3842
}
3943

44+
// Wait for all activities to complete
4045
return batch.Get(ctx, nil)
4146
}
4247

4348
func BatchActivity(ctx context.Context, taskID int) error {
4449
select {
4550
case <-ctx.Done():
51+
// Return error if workflow/activity is cancelled
4652
return fmt.Errorf("batch activity %d failed: %w", taskID, ctx.Err())
47-
case <-time.After(time.Duration(10000)*time.Millisecond):
53+
case <-time.After(time.Duration(rand.Int63n(100))*time.Millisecond + 900*time.Millisecond):
4854
return nil
4955
}
5056
}

cmd/samples/batch/workflow_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,21 @@ import (
88
)
99

1010
func Test_BatchWorkflow(t *testing.T) {
11+
// Create test environment for workflow testing
1112
testSuite := &testsuite.WorkflowTestSuite{}
1213
env := testSuite.NewTestWorkflowEnvironment()
1314

15+
// Register the workflow and activity functions
1416
env.RegisterWorkflow(BatchWorkflow)
1517
env.RegisterActivity(BatchActivity)
1618

19+
// Execute workflow with 2 concurrent workers processing 10 tasks
1720
env.ExecuteWorkflow(BatchWorkflow, BatchWorkflowInput{
1821
Concurrency: 2,
1922
TotalSize: 10,
2023
})
2124

25+
// Assert workflow completed successfully without errors
2226
assert.True(t, env.IsWorkflowCompleted())
2327
assert.Nil(t, env.GetWorkflowError())
2428
}

0 commit comments

Comments
 (0)