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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
|
package protocol
import (
"time"
"github.com/spiral/errors"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/sdk/activity"
bindings "go.temporal.io/sdk/internalbindings"
"go.temporal.io/sdk/workflow"
)
const (
getWorkerInfoCommand = "GetWorkerInfo"
invokeActivityCommand = "InvokeActivity"
startWorkflowCommand = "StartWorkflow"
invokeSignalCommand = "InvokeSignal"
invokeQueryCommand = "InvokeQuery"
destroyWorkflowCommand = "DestroyWorkflow"
cancelWorkflowCommand = "CancelWorkflow"
getStackTraceCommand = "StackTrace"
executeActivityCommand = "ExecuteActivity"
executeChildWorkflowCommand = "ExecuteChildWorkflow"
getChildWorkflowExecutionCommand = "GetChildWorkflowExecution"
newTimerCommand = "NewTimer"
sideEffectCommand = "SideEffect"
getVersionCommand = "GetVersion"
completeWorkflowCommand = "CompleteWorkflow"
continueAsNewCommand = "ContinueAsNew"
signalExternalWorkflowCommand = "SignalExternalWorkflow"
cancelExternalWorkflowCommand = "CancelExternalWorkflow"
cancelCommand = "Cancel"
panicCommand = "Panic"
)
// GetWorkerInfo reads worker information.
type GetWorkerInfo struct{}
// InvokeActivity invokes activity.
type InvokeActivity struct {
// Name defines activity name.
Name string `json:"name"`
// Info contains execution context.
Info activity.Info `json:"info"`
// HeartbeatDetails indicates that the payload also contains last heartbeat details.
HeartbeatDetails int `json:"heartbeatDetails,omitempty"`
}
// StartWorkflow sends worker command to start workflow.
type StartWorkflow struct {
// Info to define workflow context.
Info *workflow.Info `json:"info"`
// LastCompletion contains offset of last completion results.
LastCompletion int `json:"lastCompletion,omitempty"`
}
// InvokeSignal invokes signal with a set of arguments.
type InvokeSignal struct {
// RunID workflow run id.
RunID string `json:"runId"`
// Name of the signal.
Name string `json:"name"`
}
// InvokeQuery invokes query with a set of arguments.
type InvokeQuery struct {
// RunID workflow run id.
RunID string `json:"runId"`
// Name of the query.
Name string `json:"name"`
}
// CancelWorkflow asks worker to gracefully stop workflow, if possible (signal).
type CancelWorkflow struct {
// RunID workflow run id.
RunID string `json:"runId"`
}
// DestroyWorkflow asks worker to offload workflow from memory.
type DestroyWorkflow struct {
// RunID workflow run id.
RunID string `json:"runId"`
}
// GetStackTrace asks worker to offload workflow from memory.
type GetStackTrace struct {
// RunID workflow run id.
RunID string `json:"runId"`
}
// ExecuteActivity command by workflow worker.
type ExecuteActivity struct {
// Name defines activity name.
Name string `json:"name"`
// Options to run activity.
Options bindings.ExecuteActivityOptions `json:"options,omitempty"`
}
// ExecuteChildWorkflow executes child workflow.
type ExecuteChildWorkflow struct {
// Name defines workflow name.
Name string `json:"name"`
// Options to run activity.
Options bindings.WorkflowOptions `json:"options,omitempty"`
}
// GetChildWorkflowExecution returns the WorkflowID and RunId of child workflow.
type GetChildWorkflowExecution struct {
// ID of child workflow command.
ID uint64 `json:"id"`
}
// NewTimer starts new timer.
type NewTimer struct {
// Milliseconds defines timer duration.
Milliseconds int `json:"ms"`
}
// SideEffect to be recorded into the history.
type SideEffect struct{}
// GetVersion requests version marker.
type GetVersion struct {
ChangeID string `json:"changeID"`
MinSupported int `json:"minSupported"`
MaxSupported int `json:"maxSupported"`
}
// CompleteWorkflow sent by worker to complete workflow. Might include additional error as part of the payload.
type CompleteWorkflow struct{}
// ContinueAsNew restarts workflow with new running instance.
type ContinueAsNew struct {
// Result defines workflow execution result.
Name string `json:"name"`
// Options for continued as new workflow.
Options struct {
TaskQueueName string
WorkflowExecutionTimeout time.Duration
WorkflowRunTimeout time.Duration
WorkflowTaskTimeout time.Duration
} `json:"options"`
}
// SignalExternalWorkflow sends signal to external workflow.
type SignalExternalWorkflow struct {
Namespace string `json:"namespace"`
WorkflowID string `json:"workflowID"`
RunID string `json:"runID"`
Signal string `json:"signal"`
ChildWorkflowOnly bool `json:"childWorkflowOnly"`
}
// CancelExternalWorkflow canceller external workflow.
type CancelExternalWorkflow struct {
Namespace string `json:"namespace"`
WorkflowID string `json:"workflowID"`
RunID string `json:"runID"`
}
// Cancel one or multiple internal promises (activities, local activities, timers, child workflows).
type Cancel struct {
// CommandIDs to be cancelled.
CommandIDs []uint64 `json:"ids"`
}
// Panic triggers panic in workflow process.
type Panic struct {
// Message to include into the error.
Message string `json:"message"`
}
// ActivityParams maps activity command to activity params.
func (cmd ExecuteActivity) ActivityParams(env bindings.WorkflowEnvironment, payloads *commonpb.Payloads) bindings.ExecuteActivityParams {
params := bindings.ExecuteActivityParams{
ExecuteActivityOptions: cmd.Options,
ActivityType: bindings.ActivityType{Name: cmd.Name},
Input: payloads,
}
if params.TaskQueueName == "" {
params.TaskQueueName = env.WorkflowInfo().TaskQueueName
}
return params
}
// WorkflowParams maps workflow command to workflow params.
func (cmd ExecuteChildWorkflow) WorkflowParams(env bindings.WorkflowEnvironment, payloads *commonpb.Payloads) bindings.ExecuteWorkflowParams {
params := bindings.ExecuteWorkflowParams{
WorkflowOptions: cmd.Options,
WorkflowType: &bindings.WorkflowType{Name: cmd.Name},
Input: payloads,
}
if params.TaskQueueName == "" {
params.TaskQueueName = env.WorkflowInfo().TaskQueueName
}
return params
}
// ToDuration converts timer command to time.Duration.
func (cmd NewTimer) ToDuration() time.Duration {
return time.Millisecond * time.Duration(cmd.Milliseconds)
}
// returns command name (only for the commands sent to the worker)
func commandName(cmd interface{}) (string, error) {
const op = errors.Op("command_name")
switch cmd.(type) {
case GetWorkerInfo, *GetWorkerInfo:
return getWorkerInfoCommand, nil
case StartWorkflow, *StartWorkflow:
return startWorkflowCommand, nil
case InvokeSignal, *InvokeSignal:
return invokeSignalCommand, nil
case InvokeQuery, *InvokeQuery:
return invokeQueryCommand, nil
case DestroyWorkflow, *DestroyWorkflow:
return destroyWorkflowCommand, nil
case CancelWorkflow, *CancelWorkflow:
return cancelWorkflowCommand, nil
case GetStackTrace, *GetStackTrace:
return getStackTraceCommand, nil
case InvokeActivity, *InvokeActivity:
return invokeActivityCommand, nil
case ExecuteActivity, *ExecuteActivity:
return executeActivityCommand, nil
case ExecuteChildWorkflow, *ExecuteChildWorkflow:
return executeChildWorkflowCommand, nil
case GetChildWorkflowExecution, *GetChildWorkflowExecution:
return getChildWorkflowExecutionCommand, nil
case NewTimer, *NewTimer:
return newTimerCommand, nil
case GetVersion, *GetVersion:
return getVersionCommand, nil
case SideEffect, *SideEffect:
return sideEffectCommand, nil
case CompleteWorkflow, *CompleteWorkflow:
return completeWorkflowCommand, nil
case ContinueAsNew, *ContinueAsNew:
return continueAsNewCommand, nil
case SignalExternalWorkflow, *SignalExternalWorkflow:
return signalExternalWorkflowCommand, nil
case CancelExternalWorkflow, *CancelExternalWorkflow:
return cancelExternalWorkflowCommand, nil
case Cancel, *Cancel:
return cancelCommand, nil
case Panic, *Panic:
return panicCommand, nil
default:
return "", errors.E(op, errors.Errorf("undefined command type: %s", cmd))
}
}
// reads command from binary payload
func initCommand(name string) (interface{}, error) {
const op = errors.Op("init_command")
switch name {
case getWorkerInfoCommand:
return &GetWorkerInfo{}, nil
case startWorkflowCommand:
return &StartWorkflow{}, nil
case invokeSignalCommand:
return &InvokeSignal{}, nil
case invokeQueryCommand:
return &InvokeQuery{}, nil
case destroyWorkflowCommand:
return &DestroyWorkflow{}, nil
case cancelWorkflowCommand:
return &CancelWorkflow{}, nil
case getStackTraceCommand:
return &GetStackTrace{}, nil
case invokeActivityCommand:
return &InvokeActivity{}, nil
case executeActivityCommand:
return &ExecuteActivity{}, nil
case executeChildWorkflowCommand:
return &ExecuteChildWorkflow{}, nil
case getChildWorkflowExecutionCommand:
return &GetChildWorkflowExecution{}, nil
case newTimerCommand:
return &NewTimer{}, nil
case getVersionCommand:
return &GetVersion{}, nil
case sideEffectCommand:
return &SideEffect{}, nil
case completeWorkflowCommand:
return &CompleteWorkflow{}, nil
case continueAsNewCommand:
return &ContinueAsNew{}, nil
case signalExternalWorkflowCommand:
return &SignalExternalWorkflow{}, nil
case cancelExternalWorkflowCommand:
return &CancelExternalWorkflow{}, nil
case cancelCommand:
return &Cancel{}, nil
case panicCommand:
return &Panic{}, nil
default:
return nil, errors.E(op, errors.Errorf("undefined command name: %s", name))
}
}
|