-
Notifications
You must be signed in to change notification settings - Fork 93
/
workflow.go
68 lines (57 loc) · 2.12 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
64
65
66
67
68
package main
import (
"time"
"go.uber.org/cadence/workflow"
"go.uber.org/zap"
)
const (
// ApplicationName is the task list for this sample
ApplicationName = "expenseGroup"
)
var expenseServerHostPort = "http://localhost:8099"
// This is registration process where you register all your workflow handlers.
func init() {
workflow.Register(SampleExpenseWorkflow)
}
// SampleExpenseWorkflow workflow decider
func SampleExpenseWorkflow(ctx workflow.Context, expenseID string) (result string, err error) {
// step 1, create new expense report
ao := workflow.ActivityOptions{
ScheduleToStartTimeout: time.Minute,
StartToCloseTimeout: time.Minute,
HeartbeatTimeout: time.Second * 20,
}
ctx1 := workflow.WithActivityOptions(ctx, ao)
logger := workflow.GetLogger(ctx)
err = workflow.ExecuteActivity(ctx1, createExpenseActivity, expenseID).Get(ctx1, nil)
if err != nil {
logger.Error("Failed to create expense report", zap.Error(err))
return "", err
}
// step 2, wait for the expense report to be approved (or rejected)
ao = workflow.ActivityOptions{
ScheduleToStartTimeout: 10 * time.Minute,
StartToCloseTimeout: 10 * time.Minute,
}
ctx2 := workflow.WithActivityOptions(ctx, ao)
// Notice that we set the timeout to be 10 minutes for this sample demo. If the expected time for the activity to
// complete (waiting for human to approve the request) is longer, you should set the timeout accordingly so the
// cadence system will wait accordingly. Otherwise, cadence system could mark the activity as failure by timeout.
var status string
err = workflow.ExecuteActivity(ctx2, waitForDecisionActivity, expenseID).Get(ctx2, &status)
if err != nil {
return "", err
}
if status != "APPROVED" {
logger.Info("Workflow completed.", zap.String("ExpenseStatus", status))
return "", nil
}
// step 3, request payment to the expense
err = workflow.ExecuteActivity(ctx2, paymentActivity, expenseID).Get(ctx2, nil)
if err != nil {
logger.Info("Workflow completed with payment failed.", zap.Error(err))
return "", err
}
logger.Info("Workflow completed with expense payment completed.")
return "COMPLETED", nil
}