|
| 1 | +package batch |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "math/rand" |
| 7 | + "time" |
| 8 | + |
| 9 | + "go.uber.org/cadence/workflow" |
| 10 | + "go.uber.org/multierr" |
| 11 | +) |
| 12 | + |
| 13 | +type BatchWorkflowInput struct { |
| 14 | + Concurrency int |
| 15 | + TotalSize int |
| 16 | +} |
| 17 | + |
| 18 | +func BatchWorkflow(ctx workflow.Context, input BatchWorkflowInput) error { |
| 19 | + wg := workflow.NewWaitGroup(ctx) |
| 20 | + |
| 21 | + buffered := workflow.NewBufferedChannel(ctx, input.Concurrency) |
| 22 | + futures := workflow.NewNamedChannel(ctx, "futures") |
| 23 | + |
| 24 | + var errs error |
| 25 | + wg.Add(1) |
| 26 | + // task result collector |
| 27 | + workflow.Go(ctx, func(ctx workflow.Context) { |
| 28 | + defer wg.Done() |
| 29 | + for { |
| 30 | + var future workflow.Future |
| 31 | + ok := futures.Receive(ctx, &future) |
| 32 | + if !ok { |
| 33 | + break |
| 34 | + } |
| 35 | + err := future.Get(ctx, nil) |
| 36 | + errs = multierr.Append(errs, err) |
| 37 | + buffered.Receive(ctx, nil) |
| 38 | + } |
| 39 | + }) |
| 40 | + |
| 41 | + // submit all tasks |
| 42 | + for taskID := 0; taskID < input.TotalSize; taskID++ { |
| 43 | + taskID := taskID |
| 44 | + buffered.Send(ctx, nil) |
| 45 | + |
| 46 | + aCtx := workflow.WithActivityOptions(ctx, workflow.ActivityOptions{ |
| 47 | + ScheduleToStartTimeout: time.Second * 10, |
| 48 | + StartToCloseTimeout: time.Second * 10, |
| 49 | + }) |
| 50 | + futures.Send(ctx, workflow.ExecuteActivity(aCtx, BatchActivity, taskID)) |
| 51 | + } |
| 52 | + // close the channel to signal the task result collector that no more tasks are coming |
| 53 | + futures.Close() |
| 54 | + |
| 55 | + wg.Wait(ctx) |
| 56 | + |
| 57 | + return errs |
| 58 | +} |
| 59 | + |
| 60 | +func BatchActivity(ctx context.Context, taskID int) error { |
| 61 | + select { |
| 62 | + case <-ctx.Done(): |
| 63 | + return fmt.Errorf("batch activity %d failed: %w", taskID, ctx.Err()) |
| 64 | + case <-time.After(time.Duration(rand.Int63n(100))*time.Millisecond + 900*time.Millisecond): |
| 65 | + return nil |
| 66 | + } |
| 67 | +} |
0 commit comments