forked from cadence-workflow/cadence-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_workflow.go
More file actions
66 lines (57 loc) · 1.61 KB
/
Copy pathparallel_workflow.go
File metadata and controls
66 lines (57 loc) · 1.61 KB
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
package main
import (
"errors"
"time"
"go.uber.org/cadence/workflow"
"go.uber.org/zap"
)
/**
* This sample workflow executes multiple branches in parallel using workflow.Go() method.
*/
// sampleParallelWorkflow workflow decider
func sampleParallelWorkflow(ctx workflow.Context) error {
waitChannel := workflow.NewChannel(ctx)
ao := workflow.ActivityOptions{
ScheduleToStartTimeout: time.Minute,
StartToCloseTimeout: time.Minute,
HeartbeatTimeout: time.Second * 20,
}
ctx = workflow.WithActivityOptions(ctx, ao)
logger := workflow.GetLogger(ctx)
workflow.Go(ctx, func(ctx workflow.Context) {
err := workflow.ExecuteActivity(ctx, sampleActivity, "branch1.1").Get(ctx, nil)
if err != nil {
logger.Error("Activity failed", zap.Error(err))
waitChannel.Send(ctx, err.Error())
return
}
err = workflow.ExecuteActivity(ctx, sampleActivity, "branch1.2").Get(ctx, nil)
if err != nil {
logger.Error("Activity failed", zap.Error(err))
waitChannel.Send(ctx, err.Error())
return
}
waitChannel.Send(ctx, "")
})
workflow.Go(ctx, func(ctx workflow.Context) {
err := workflow.ExecuteActivity(ctx, sampleActivity, "branch2").Get(ctx, nil)
if err != nil {
logger.Error("Activity failed", zap.Error(err))
waitChannel.Send(ctx, err.Error())
return
}
waitChannel.Send(ctx, "")
})
// wait for both of the coroutinue to complete.
var errMsg string
for i := 0; i != 2; i++ {
waitChannel.Receive(ctx, &errMsg)
if errMsg != "" {
err := errors.New(errMsg)
logger.Error("Coroutine failed", zap.Error(err))
return err
}
}
logger.Info("Workflow completed.")
return nil
}