-
Notifications
You must be signed in to change notification settings - Fork 231
Expand file tree
/
Copy pathworkflows.go
More file actions
183 lines (164 loc) · 6.11 KB
/
workflows.go
File metadata and controls
183 lines (164 loc) · 6.11 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package worker_versioning
import (
"context"
"time"
"go.temporal.io/sdk/activity"
"go.temporal.io/sdk/workflow"
)
// AutoUpgradingWorkflowV1 will automatically move to the latest worker version. We'll be making
// changes to it, which must be replay safe.
//
// Note that generally you won't want or need to include a version number in your workflow name if
// you're using the worker versioning feature. This sample does it to illustrate changes to the
// same code over time - but really what we're demonstrating here is the evolution of what would
// have been one workflow definition.
func AutoUpgradingWorkflowV1(ctx workflow.Context) error {
workflow.GetLogger(ctx).Info("Changing workflow v1 started.", "StartTime", workflow.Now(ctx))
// This workflow will listen for signals from our starter, and upon each signal either run
// an activity, or conclude execution.
signalChan := workflow.GetSignalChannel(ctx, "do-next-signal")
for {
var signal string
signalChan.Receive(ctx, &signal)
if signal == "do-activity" {
workflow.GetLogger(ctx).Info("Changing workflow v1 running activity")
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx1 := workflow.WithActivityOptions(ctx, ao)
err := workflow.ExecuteActivity(ctx1, SomeActivity, "v1").Get(ctx1, nil)
if err != nil {
return err
}
} else {
workflow.GetLogger(ctx).Info("Concluding workflow v1")
return nil
}
}
}
// AutoUpgradingWorkflowV1b represents us having made *compatible* changes to
// AutoUpgradingWorkflowV1.
//
// The compatible changes we've made are:
// - Altering the log lines
// - Using the workflow.GetVersion API to properly introduce branching behavior while maintaining
// compatibility
func AutoUpgradingWorkflowV1b(ctx workflow.Context) error {
workflow.GetLogger(ctx).Info("Changing workflow v1b started.", "StartTime", workflow.Now(ctx))
// This workflow will listen for signals from our starter, and upon each signal either run
// an activity, or conclude execution.
signalChan := workflow.GetSignalChannel(ctx, "do-next-signal")
for {
var signal string
signalChan.Receive(ctx, &signal)
if signal == "do-activity" {
workflow.GetLogger(ctx).Info("Changing workflow v1b running activity")
v := workflow.GetVersion(ctx, "DifferentActivity", workflow.DefaultVersion, 1)
if v == workflow.DefaultVersion {
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx1 := workflow.WithActivityOptions(ctx, ao)
// Note it is a valid compatible change to alter the input to an activity.
// However, because we're using the GetVersion API, this branch will never be
// taken.
err := workflow.ExecuteActivity(ctx1, SomeActivity, "v1b").Get(ctx1, nil)
if err != nil {
return err
}
} else {
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx1 := workflow.WithActivityOptions(ctx, ao)
err := workflow.ExecuteActivity(ctx1, SomeIncompatibleActivity, &IncompatibleActivityInput{
CalledBy: "v1b",
MoreData: "hello!",
}).Get(ctx1, nil)
if err != nil {
return err
}
}
} else {
workflow.GetLogger(ctx).Info("Concluding workflow v1b")
break
}
}
return nil
}
// PinnedWorkflowV1 demonstrates a workflow that likely has a short lifetime, and we want to always
// stay pinned to the same version it began on.
//
// Note that generally you won't want or need to include a version number in your workflow name if
// you're using the worker versioning feature. This sample does it to illustrate changes to the
// same code over time - but really what we're demonstrating here is the evolution of what would
// have been one workflow definition.
func PinnedWorkflowV1(ctx workflow.Context) error {
workflow.GetLogger(ctx).Info("Pinned Workflow v1 started.", "StartTime", workflow.Now(ctx))
signalChan := workflow.GetSignalChannel(ctx, "do-next-signal")
for {
var signal string
signalChan.Receive(ctx, &signal)
if signal == "conclude" {
break
}
}
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx1 := workflow.WithActivityOptions(ctx, ao)
err := workflow.ExecuteActivity(ctx1, SomeActivity, "Pinned-v1").Get(ctx, nil)
if err != nil {
return err
}
return nil
}
// PinnedWorkflowV2 has changes that would make it incompatible with v1, and aren't protected by
// a patch.
func PinnedWorkflowV2(ctx workflow.Context) error {
workflow.GetLogger(ctx).Info("Pinned Workflow v1 started.", "StartTime", workflow.Now(ctx))
ao := workflow.ActivityOptions{
StartToCloseTimeout: 10 * time.Second,
}
ctx1 := workflow.WithActivityOptions(ctx, ao)
// Here we call an activity where we didn't before, which is an incompatible change.
err := workflow.ExecuteActivity(ctx1, SomeActivity, "Pinned-v2").Get(ctx, nil)
if err != nil {
return err
}
signalChan := workflow.GetSignalChannel(ctx, "do-next-signal")
for {
var signal string
signalChan.Receive(ctx, &signal)
if signal == "conclude" {
break
}
}
// We've also changed the activity type here, another incompatible change
err = workflow.ExecuteActivity(ctx1, SomeIncompatibleActivity, &IncompatibleActivityInput{
CalledBy: "Pinned-v2",
MoreData: "hi",
}).Get(ctx, nil)
if err != nil {
return err
}
return nil
}
func SomeActivity(ctx context.Context, calledBy string) (string, error) {
activity.GetLogger(ctx).Info("SomeActivity executing", "called by", calledBy)
return calledBy, nil
}
// SomeIncompatibleActivity represents the need to change the interface to SomeActivity. Perhaps
// we didn't realize we would need to pass additional data, and we change the string parameter to
// a struct. (Hint: It's a great idea to always start with structs for this reason, as they can
// be extended without breaking compatibility as long as you use a wire format that maintains
// compatibility.)
func SomeIncompatibleActivity(ctx context.Context, input IncompatibleActivityInput) error {
activity.GetLogger(ctx).Info("SomeIncompatibleActivity executing",
"called by", input.CalledBy, "more data", input.MoreData)
return nil
}
type IncompatibleActivityInput struct {
CalledBy string
MoreData string
}