|
| 1 | +package workflows |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "go.uber.org/cadence/activity" |
| 6 | + "go.uber.org/cadence/workflow" |
| 7 | + "time" |
| 8 | +) |
| 9 | + |
| 10 | +type parallelBranchInput struct { |
| 11 | + Message string `json:"message"` |
| 12 | +} |
| 13 | + |
| 14 | +// ParallelBranchPickFirstWorkflow is a sample workflow simulating two parallel activities running |
| 15 | +// at the same time and picking the first successful result. |
| 16 | +func ParallelBranchPickFirstWorkflow(ctx workflow.Context) (string, error) { |
| 17 | + logger := workflow.GetLogger(ctx) |
| 18 | + logger.Info("ParallelBranchPickFirstWorkflow started") |
| 19 | + |
| 20 | + selector := workflow.NewSelector(ctx) |
| 21 | + var firstResp string |
| 22 | + |
| 23 | + // Use a cancel handler to cancel all rest of other activities. |
| 24 | + childCtx, cancelHandler := workflow.WithCancel(ctx) |
| 25 | + ao := workflow.ActivityOptions{ |
| 26 | + ScheduleToStartTimeout: time.Minute, |
| 27 | + StartToCloseTimeout: time.Minute, |
| 28 | + HeartbeatTimeout: time.Second * 20, |
| 29 | + WaitForCancellation: true, // wait for cancellation to complete |
| 30 | + } |
| 31 | + childCtx = workflow.WithActivityOptions(childCtx, ao) |
| 32 | + |
| 33 | + // Set WaitForCancellation to true to demonstrate the cancellation to the other activities. In real world case, |
| 34 | + // you might not care about them and could set WaitForCancellation to false (which is default value). |
| 35 | + |
| 36 | + // Run two activities in parallel |
| 37 | + f1 := workflow.ExecuteActivity(childCtx, ParallelActivity, parallelBranchInput{Message: "first activity"}, time.Second*10) |
| 38 | + f2 := workflow.ExecuteActivity(childCtx, ParallelActivity, parallelBranchInput{Message: "second activity"}, time.Second*2) |
| 39 | + pendingFutures := []workflow.Future{f1, f2} |
| 40 | + selector.AddFuture(f1, func(f workflow.Future) { |
| 41 | + f.Get(ctx, &firstResp) |
| 42 | + }).AddFuture(f2, func(f workflow.Future) { |
| 43 | + f.Get(ctx, &firstResp) |
| 44 | + }) |
| 45 | + |
| 46 | + // wait for any of the future to complete |
| 47 | + selector.Select(ctx) |
| 48 | + |
| 49 | + // now if at least one future is complete, cancel all other pending futures |
| 50 | + cancelHandler() |
| 51 | + |
| 52 | + // - If you want to wait for pending activities to finish after issuing cancellation |
| 53 | + // then wait for the future to complete. |
| 54 | + // - if you don't want to wait for completion of pending activities cancellation then you can choose to |
| 55 | + // set WaitForCancellation to false through WithWaitForCancellation(false) |
| 56 | + for _, f := range pendingFutures { |
| 57 | + err := f.Get(ctx, &firstResp) |
| 58 | + if err != nil { |
| 59 | + return "", err |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + logger.Info("ParallelBranchPickFirstWorkflow completed") |
| 64 | + return firstResp, nil |
| 65 | +} |
| 66 | + |
| 67 | +func ParallelActivity(ctx context.Context, input parallelBranchInput) (string, error) { |
| 68 | + logger := activity.GetLogger(ctx) |
| 69 | + logger.Info("ParallelActivity started") |
| 70 | + return "Hello " + input.Message, nil |
| 71 | +} |
0 commit comments