-
Notifications
You must be signed in to change notification settings - Fork 1
/
workflow.go
358 lines (315 loc) · 12.5 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
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
package tempts
import (
"context"
"errors"
"fmt"
"reflect"
"time"
"go.temporal.io/api/serviceerror"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
)
// WorkflowWithImpl is a temporary struct that implements Registerable. It's meant to be passed into `tempts.NewWorker`.
type WorkflowWithImpl[Param any, Return any] struct {
workflowName string
queue Queue
fn any
positional bool
}
// Name returns the name of the workflow being implemented.
func (w WorkflowWithImpl[Param, Return]) Name() string {
return w.workflowName
}
func (w WorkflowWithImpl[Param, Return]) register(ar worker.Registry) {
ar.RegisterWorkflowWithOptions(w.fn, workflow.RegisterOptions{Name: w.workflowName})
}
func (w WorkflowWithImpl[Param, Return]) validate(q *Queue, v *validationState) error {
if w.queue.name != q.name {
return fmt.Errorf("workflow for queue %s can't be registered on worker with queue %s", w.queue.name, q.name)
}
_, ok := v.workflowsValidated[w.workflowName]
if ok {
return fmt.Errorf("duplicate activtity name %s for queue %s", w.workflowName, q.name)
}
v.workflowsValidated[w.workflowName] = struct{}{}
return nil
}
type testEnvironment interface {
ExecuteWorkflow(workflowFn interface{}, args ...interface{})
GetWorkflowResult(valuePtr interface{}) error
GetWorkflowError() error
}
// ExecuteInTest executes the given workflow implementation in a unit test and returns the output of the workflow.
func (w WorkflowWithImpl[Param, Return]) ExecuteInTest(e testEnvironment, p Param) (Return, error) {
e.ExecuteWorkflow(w.fn, p)
var ret Return
err := e.GetWorkflowResult(&ret)
if err != nil {
return ret, err
}
return ret, e.GetWorkflowError()
}
// WorkflowDeclaration always contains Workflow but doesn't have type parameters, so it can be passed into non-generic functions.
type WorkflowDeclaration interface {
Name() string
getQueue() *Queue
}
// Workflow is used for interacting with workflows in a safe way that takes into account the input and output types, queue name and other properties.
// Workflows are resumable functions registered on workers that execute activities.
type Workflow[Param any, Return any] struct {
name string
queue *Queue
positional bool
}
// NewWorkflow declares the existence of a workflow on a given queue with a given name.
func NewWorkflow[
Param any,
Return any,
](queue *Queue, name string,
) Workflow[Param, Return] {
panicIfNotStruct[Param]("NewWorkflow")
queue.registerWorkflow(name, func(workflow.Context, Param) (Return, error) {
panic(fmt.Sprintf("Workflow %s execution not mocked", name))
})
return Workflow[Param, Return]{
name: name,
queue: queue,
}
}
// NewWorkflowPositional declares the existence of a workflow on a given queue with a given name.
// Instead of passing the Param struct directly to the workflow, it passes each field of the struct
// as a separate positional argument in the order they are defined.
func NewWorkflowPositional[Param any, Return any](queue *Queue, name string) Workflow[Param, Return] {
panicIfNotStruct[Param]("NewWorkflowPositional")
// Get the type information for the Param struct
paramType := reflect.TypeOf((*Param)(nil)).Elem()
// Create a slice of function parameter types: (context.Context, field1Type, field2Type, ...)
var fieldTypes []reflect.Type
if paramType.Kind() == reflect.Ptr {
fieldTypes = extractFieldTypes(paramType.Elem())
} else {
fieldTypes = extractFieldTypes(paramType)
}
paramTypes := make([]reflect.Type, len(fieldTypes)+1)
paramTypes[0] = reflect.TypeOf((*workflow.Context)(nil)).Elem()
copy(paramTypes[1:], fieldTypes)
// Create the function type: func(workflow.Context, field1Type, field2Type, ...) (Return, error)
returnType := reflect.TypeOf((*Return)(nil)).Elem()
errorType := reflect.TypeOf((*error)(nil)).Elem()
fnType := reflect.FuncOf(paramTypes, []reflect.Type{returnType, errorType}, false)
// Create a function that panics with the message "Function execution not mocked"
mockFn := reflect.MakeFunc(fnType, func(args []reflect.Value) []reflect.Value {
panic(fmt.Sprintf("Workflow %s execution not mocked", name))
})
// Register the mock function
queue.registerWorkflow(name, mockFn.Interface())
return Workflow[Param, Return]{
name: name,
queue: queue,
positional: true,
}
}
// Name returns the name of the workflow.
func (w Workflow[Param, Return]) Name() string {
return w.name
}
// getQueue returns the queue for the workflow
func (w Workflow[Param, Return]) getQueue() *Queue {
return w.queue
}
// WithImplementation should be called to create the parameters for NewWorker(). It declares which function implements the workflow.
func (w Workflow[Param, Return]) WithImplementation(fn func(workflow.Context, Param) (Return, error)) *WorkflowWithImpl[Param, Return] {
if !w.positional {
return &WorkflowWithImpl[Param, Return]{workflowName: w.name, queue: *w.queue, fn: func(ctx workflow.Context, param Param) (Return, error) {
// Set a default timeout so if a workflow doesn't need to customize it, it doesn't have to call this function.
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: time.Second * 10,
})
return fn(ctx, param)
}}
}
// For positional workflows, create a wrapper function that converts positional arguments to a struct
paramType := reflect.TypeOf((*Param)(nil)).Elem()
var fieldTypes []reflect.Type
if paramType.Kind() == reflect.Ptr {
fieldTypes = extractFieldTypes(paramType.Elem())
} else {
fieldTypes = extractFieldTypes(paramType)
}
wrapper := reflect.MakeFunc(
reflect.FuncOf(
append([]reflect.Type{reflect.TypeOf((*workflow.Context)(nil)).Elem()}, fieldTypes...),
[]reflect.Type{reflect.TypeOf((*Return)(nil)).Elem(), reflect.TypeOf((*error)(nil)).Elem()},
false,
),
func(args []reflect.Value) []reflect.Value {
ctx := args[0]
// Create a new instance of the Param struct
var paramVal reflect.Value
if paramType.Kind() == reflect.Ptr {
paramVal = reflect.New(paramType.Elem())
// Fill the struct fields with the positional arguments
for i := 0; i < paramType.Elem().NumField(); i++ {
paramVal.Elem().Field(i).Set(args[i+1])
}
} else {
paramVal = reflect.New(paramType).Elem()
// Fill the struct fields with the positional arguments
for i := 0; i < paramType.NumField(); i++ {
paramVal.Field(i).Set(args[i+1])
}
}
// Set default timeout
ctx = reflect.ValueOf(workflow.WithActivityOptions(ctx.Interface().(workflow.Context), workflow.ActivityOptions{
StartToCloseTimeout: time.Second * 10,
}))
// Call the implementation function with the context and constructed struct
results := reflect.ValueOf(fn).Call([]reflect.Value{ctx, paramVal})
return results
},
)
return &WorkflowWithImpl[Param, Return]{
workflowName: w.name,
queue: *w.queue,
fn: wrapper.Interface(),
positional: true,
}
}
// Run executes the workflow and synchronously returns the output.
func (w Workflow[Param, Return]) Run(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions, param Param) (Return, error) {
var ret Return
r, err := w.Execute(ctx, temporalClient, opts, param)
if err != nil {
return ret, err
}
err = r.Get(ctx, &ret)
return ret, err
}
// Execute asynchronously executes the workflow and returns a promise.
func (w Workflow[Param, Return]) Execute(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions, param Param) (client.WorkflowRun, error) {
opts.TaskQueue = w.queue.name
if !w.positional {
return temporalClient.Client.ExecuteWorkflow(ctx, opts, w.name, param)
}
// For positional workflows, extract struct fields into separate arguments
paramVal := reflect.ValueOf(param)
if paramVal.Kind() == reflect.Ptr {
paramVal = paramVal.Elem()
}
args := make([]interface{}, paramVal.NumField())
for i := 0; i < paramVal.NumField(); i++ {
args[i] = paramVal.Field(i).Interface()
}
return temporalClient.Client.ExecuteWorkflow(ctx, opts, w.name, args...)
}
// Run executes the workflow from another parent workflow and synchronously returns the output.
func (w Workflow[Param, Return]) RunChild(ctx workflow.Context, opts workflow.ChildWorkflowOptions, param Param) (Return, error) {
var ret Return
err := w.ExecuteChild(ctx, opts, param).Get(ctx, &ret)
if err != nil {
return ret, err
}
return ret, nil
}
// Execute asynchronously executes the workflow from another parent workflow and returns a promise.
func (w Workflow[Param, Return]) ExecuteChild(ctx workflow.Context, opts workflow.ChildWorkflowOptions, param Param) workflow.ChildWorkflowFuture {
opts.TaskQueue = w.queue.name
if !w.positional {
return workflow.ExecuteChildWorkflow(workflow.WithChildOptions(ctx, opts), w.name, param)
}
// For positional workflows, extract struct fields into separate arguments
paramVal := reflect.ValueOf(param)
if paramVal.Kind() == reflect.Ptr {
paramVal = paramVal.Elem()
}
args := make([]interface{}, paramVal.NumField())
for i := 0; i < paramVal.NumField(); i++ {
args[i] = paramVal.Field(i).Interface()
}
return workflow.ExecuteChildWorkflow(workflow.WithChildOptions(ctx, opts), w.name, args...)
}
// SetSchedule creates or updates the schedule to match the given definition.
// WARNING:
// This feature is not as seamless as it could be because of the complex API exposed by temporal.
// In some cases, when the schedule has been modified in some non-updateable way, this method can't update the schedule and it returns an error.
func (w Workflow[Param, Return]) SetSchedule(ctx context.Context, temporalClient *Client, opts client.ScheduleOptions, param Param) error {
// Handle positional arguments
var args []any
if !w.positional {
args = []any{param}
} else {
// For positional workflows, extract struct fields into separate arguments
paramVal := reflect.ValueOf(param)
if paramVal.Kind() == reflect.Ptr {
paramVal = paramVal.Elem()
}
args = make([]any, paramVal.NumField())
for i := 0; i < paramVal.NumField(); i++ {
args[i] = paramVal.Field(i).Interface()
}
}
return setSchedule(ctx, temporalClient, opts, w.name, w.queue, args)
}
func setSchedule(ctx context.Context, temporalClient *Client, opts client.ScheduleOptions, workflowName string, queue *Queue, args []any) error {
if opts.Action == nil {
opts.Action = &client.ScheduleWorkflowAction{}
}
a, ok := opts.Action.(*client.ScheduleWorkflowAction)
if !ok {
return fmt.Errorf("opts.Action is %T, not *client.ScheduleWorkflowAction", opts.Action)
}
a.Workflow = workflowName
a.TaskQueue = queue.name
a.Args = args
opts.Action = a
s := temporalClient.Client.ScheduleClient().GetHandle(ctx, opts.ID)
info, err := s.Describe(ctx)
if err != nil {
var notFound *serviceerror.NotFound
if !errors.As(err, ¬Found) {
return err
}
_, err = temporalClient.Client.ScheduleClient().Create(ctx, opts)
if err != nil {
return err
}
return nil
}
// If not equal, error because we can't make them match?
// Don't compare if neither is set
if !(info.Memo == nil && len(opts.Memo) == 0) && !info.Memo.Equal(opts.Memo) {
// TODO: re-create the schedule in this case?
return fmt.Errorf("provided memo %s doesn't match schedule memo %s and there's no way to fix this without re-creating the schedule", opts.Memo, info.Memo)
}
if info.Schedule.State.Note != opts.Note {
// TODO: re-create the schedule in this case?
return fmt.Errorf("provided note %s doesn't match schedule note %s and there's no way to fix this without re-creating the schedule", opts.Note, info.Schedule.State.Note)
}
// Warning: comparing search attributes doesn't seem to work because temporal craetes its own even when none are provided, so it's not safe to simply compare them.
// if !info.SearchAttributes.Equal(opts.SearchAttributes) {
// // TODO: re-create the schedule in this case?
// return fmt.Errorf("provided search attributes %s doesn't match schedule search attributes %s and there's no way to fix this without re-creating the schedule", opts.SearchAttributes, info.SearchAttributes)
// }
// Update anything we can
err = s.Update(ctx, client.ScheduleUpdateOptions{
DoUpdate: func(input client.ScheduleUpdateInput) (*client.ScheduleUpdate, error) {
s := input.Description.Schedule
s.Action = opts.Action
s.Spec = &opts.Spec
s.State.Paused = opts.Paused
s.Policy = &client.SchedulePolicies{
Overlap: opts.Overlap,
CatchupWindow: opts.CatchupWindow,
PauseOnFailure: opts.PauseOnFailure,
}
return &client.ScheduleUpdate{
Schedule: &s,
}, nil
},
})
if err != nil {
return err
}
return nil
}