-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathsplitmerge_workflow.go
62 lines (50 loc) · 1.85 KB
/
splitmerge_workflow.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
package splitmerge_future
import (
"context"
"time"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/workflow"
)
/**
* This sample workflow demonstrates how to execute multiple activities in parallel and merge their results using futures.
* The futures are awaited using Get method in the same order the activities are invoked. See `split-merge-selector` sample
* to see how to process them in the order of activity completion instead.
*/
// ChunkResult contains the activity result for this sample
type ChunkResult struct {
NumberOfItemsInChunk int
SumInChunk int
}
// SampleSplitMergeFutureWorkflow workflow definition
func SampleSplitMergeFutureWorkflow(ctx workflow.Context, processorCount int) (ChunkResult, error) {
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)
var results []workflow.Future
for i := 0; i < processorCount; i++ {
// ExecuteActivity returns Future that doesn't need to be awaited immediately.
future := workflow.ExecuteActivity(ctx, ChunkProcessingActivity, i+1)
results = append(results, future)
}
var totalItemCount, totalSum int
for i := 0; i < processorCount; i++ {
var result ChunkResult
// Blocks until the activity result is available.
err := results[i].Get(ctx, &result)
if err != nil {
return ChunkResult{}, err
}
totalItemCount += result.NumberOfItemsInChunk
totalSum += result.SumInChunk
}
workflow.GetLogger(ctx).Info("Workflow completed.")
return ChunkResult{totalItemCount, totalSum}, nil
}
func ChunkProcessingActivity(ctx context.Context, chunkID int) (result ChunkResult, err error) {
// some fake processing logic here
numberOfItemsInChunk := chunkID
sumInChunk := chunkID * chunkID
activity.GetLogger(ctx).Info("Chunk processed", "chunkID", chunkID)
return ChunkResult{numberOfItemsInChunk, sumInChunk}, nil
}