-
Notifications
You must be signed in to change notification settings - Fork 202
/
workflow.go
63 lines (50 loc) · 1.62 KB
/
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
package saga
import (
"time"
"go.uber.org/multierr"
"go.temporal.io/sdk/temporal"
"go.temporal.io/sdk/workflow"
)
func TransferMoney(ctx workflow.Context, transferDetails TransferDetails) (err error) {
retryPolicy := &temporal.RetryPolicy{
InitialInterval: time.Second,
BackoffCoefficient: 2.0,
MaximumInterval: time.Minute,
MaximumAttempts: 3,
}
options := workflow.ActivityOptions{
// Timeout options specify when to automatically timeout Activity functions.
StartToCloseTimeout: time.Minute,
// Optionally provide a customized RetryPolicy.
// Temporal retries failures by default, this is just an example.
RetryPolicy: retryPolicy,
}
ctx = workflow.WithActivityOptions(ctx, options)
err = workflow.ExecuteActivity(ctx, Withdraw, transferDetails).Get(ctx, nil)
if err != nil {
return err
}
defer func() {
if err != nil {
errCompensation := workflow.ExecuteActivity(ctx, WithdrawCompensation, transferDetails).Get(ctx, nil)
err = multierr.Append(err, errCompensation)
}
}()
err = workflow.ExecuteActivity(ctx, Deposit, transferDetails).Get(ctx, nil)
if err != nil {
return err
}
defer func() {
if err != nil {
errCompensation := workflow.ExecuteActivity(ctx, DepositCompensation, transferDetails).Get(ctx, nil)
err = multierr.Append(err, errCompensation)
}
// uncomment to have time to shut down worker to simulate worker rolling update and ensure that compensation sequence preserves after restart
// workflow.Sleep(ctx, 10*time.Second)
}()
err = workflow.ExecuteActivity(ctx, StepWithError, transferDetails).Get(ctx, nil)
if err != nil {
return err
}
return nil
}