forked from temporalio/samples-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
workflow.go
166 lines (145 loc) · 4.2 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package dsl
import (
"time"
"go.temporal.io/sdk/workflow"
)
type (
// Workflow is the type used to express the workflow definition. Variables are a map of valuables. Variables can be
// used as input to Activity.
Workflow struct {
Variables map[string]string
Root Statement
}
// Statement is the building block of dsl workflow. A Statement can be a simple ActivityInvocation or it
// could be a Sequence or Parallel.
Statement struct {
Activity *ActivityInvocation
Sequence *Sequence
Parallel *Parallel
}
// Sequence consist of a collection of Statements that runs in sequential.
Sequence struct {
Elements []*Statement
}
// Parallel can be a collection of Statements that runs in parallel.
Parallel struct {
Branches []*Statement
}
// ActivityInvocation is used to express invoking an Activity. The Arguments defined expected arguments as input to
// the Activity, the result specify the name of variable that it will store the result as which can then be used as
// arguments to subsequent ActivityInvocation.
ActivityInvocation struct {
Name string
Arguments []string
Result string
}
executable interface {
execute(ctx workflow.Context, bindings map[string]string) error
}
)
// SimpleDSLWorkflow workflow definition
func SimpleDSLWorkflow(ctx workflow.Context, dslWorkflow Workflow) ([]byte, error) {
bindings := make(map[string]string)
//workflowcheck:ignore Only iterates for building another map
for k, v := range dslWorkflow.Variables {
bindings[k] = v
}
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx = workflow.WithActivityOptions(ctx, ao)
logger := workflow.GetLogger(ctx)
err := dslWorkflow.Root.execute(ctx, bindings)
if err != nil {
logger.Error("DSL Workflow failed.", "Error", err)
return nil, err
}
logger.Info("DSL Workflow completed.")
return nil, err
}
func (b *Statement) execute(ctx workflow.Context, bindings map[string]string) error {
if b.Parallel != nil {
err := b.Parallel.execute(ctx, bindings)
if err != nil {
return err
}
}
if b.Sequence != nil {
err := b.Sequence.execute(ctx, bindings)
if err != nil {
return err
}
}
if b.Activity != nil {
err := b.Activity.execute(ctx, bindings)
if err != nil {
return err
}
}
return nil
}
func (a ActivityInvocation) execute(ctx workflow.Context, bindings map[string]string) error {
inputParam := makeInput(a.Arguments, bindings)
var result string
err := workflow.ExecuteActivity(ctx, a.Name, inputParam).Get(ctx, &result)
if err != nil {
return err
}
if a.Result != "" {
bindings[a.Result] = result
}
return nil
}
func (s Sequence) execute(ctx workflow.Context, bindings map[string]string) error {
for _, a := range s.Elements {
err := a.execute(ctx, bindings)
if err != nil {
return err
}
}
return nil
}
func (p Parallel) execute(ctx workflow.Context, bindings map[string]string) error {
//
// You can use the context passed in to activity as a way to cancel the activity like standard GO way.
// Cancelling a parent context will cancel all the derived contexts as well.
//
// In the parallel block, we want to execute all of them in parallel and wait for all of them.
// if one activity fails then we want to cancel all the rest of them as well.
childCtx, cancelHandler := workflow.WithCancel(ctx)
selector := workflow.NewSelector(ctx)
var activityErr error
for _, s := range p.Branches {
f := executeAsync(s, childCtx, bindings)
selector.AddFuture(f, func(f workflow.Future) {
err := f.Get(ctx, nil)
if err != nil {
// cancel all pending activities
cancelHandler()
activityErr = err
}
})
}
for i := 0; i < len(p.Branches); i++ {
selector.Select(ctx) // this will wait for one branch
if activityErr != nil {
return activityErr
}
}
return nil
}
func executeAsync(exe executable, ctx workflow.Context, bindings map[string]string) workflow.Future {
future, settable := workflow.NewFuture(ctx)
workflow.Go(ctx, func(ctx workflow.Context) {
err := exe.execute(ctx, bindings)
settable.Set(nil, err)
})
return future
}
func makeInput(argNames []string, argsMap map[string]string) []string {
var args []string
for _, arg := range argNames {
args = append(args, argsMap[arg])
}
return args
}