-
Notifications
You must be signed in to change notification settings - Fork 202
/
splitmerge_workflow.go
69 lines (57 loc) · 2.05 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
63
64
65
66
67
68
69
package splitmerge_selector
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 Selector. It allows processing them as soon as they become ready. See `split-merge-future` sample
* to see how to process them without Selector in the order of activity invocation instead.
*/
// ChunkResult contains the activity result for this sample
type ChunkResult struct {
NumberOfItemsInChunk int
SumInChunk int
}
// SampleSplitMergeSelectorWorkflow workflow definition
func SampleSplitMergeSelectorWorkflow(ctx workflow.Context, workerCount int) (result ChunkResult, err error) {
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)
selector := workflow.NewSelector(ctx)
var totalItemCount, totalSum int
for i := 0; i < workerCount; i++ {
// ExecuteActivity returns Future that doesn't need to be awaited immediately.
future := workflow.ExecuteActivity(ctx, ChunkProcessingActivity, i+1)
selector.AddFuture(future, func(f workflow.Future) {
var r ChunkResult
err1 := f.Get(ctx, &r)
if err1 != nil {
err = err1
return
}
totalItemCount += r.NumberOfItemsInChunk
totalSum += r.SumInChunk
})
}
for i := 0; i < workerCount; i++ {
// Each call to Select matches a single ready Future.
// Each Future is matched only once independently on the number of Select calls.
selector.Select(ctx)
if err != nil {
return ChunkResult{}, err
}
}
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
}