-
Notifications
You must be signed in to change notification settings - Fork 1
/
workflow.go
316 lines (276 loc) · 10.9 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
package tstemporal
import (
"context"
"errors"
"fmt"
"go.temporal.io/api/serviceerror"
"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
"go.temporal.io/sdk/workflow"
)
type WorkflowWithImpl struct {
workflowName string
queue Queue
fn any
}
func (a WorkflowWithImpl) register(ar Registry) {
ar.RegisterWorkflowWithOptions(a.fn, workflow.RegisterOptions{Name: a.workflowName})
}
func (a WorkflowWithImpl) validate(q *Queue, v *ValidationState) error {
if a.queue.name != q.name {
return fmt.Errorf("workflow for queue %s can't be registered on worker with queue %s", a.queue.name, q.name)
}
if a.queue.namespace.name != q.namespace.name {
return fmt.Errorf("workflow for namespace %s can't be registered on worker with namespace %s", a.queue.namespace.name, q.namespace.name)
}
_, ok := v.workflowsValidated[a.workflowName]
if ok {
return fmt.Errorf("duplicate activtity name %s for queue %s and namespace %s", a.workflowName, q.name, q.namespace.name)
}
v.workflowsValidated[a.workflowName] = struct{}{}
return nil
}
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
if temporalClient.namespace != queue.namespace.name {
return fmt.Errorf("attempting to set a schedule for a workflow in %s on the wrong namespace: %s", queue.namespace.name, temporalClient.namespace)
}
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.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
}
type Workflow0R[Return any] struct {
Name string
queue *Queue
}
func NewWorkflow0R[Return any](queue *Queue, name string) Workflow0R[Return] {
queue.registerWorkflow(name, (func(context.Context) (Return, error))(nil))
return Workflow0R[Return]{
Name: name,
queue: queue,
}
}
func (w Workflow0R[Return]) WithImplementation(fn func(workflow.Context) (Return, error)) *WorkflowWithImpl {
return &WorkflowWithImpl{workflowName: w.Name, queue: *w.queue, fn: fn}
}
func (w Workflow0R[Return]) Register(wr worker.WorkflowRegistry, fn func(workflow.Context) (Return, error)) {
wr.RegisterWorkflowWithOptions(fn, workflow.RegisterOptions{
Name: w.Name,
})
}
func (w Workflow0R[Return]) Run(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) (Return, error) {
var ret Return
r, err := w.Execute(ctx, temporalClient, opts)
if err != nil {
return ret, err
}
err = r.Get(ctx, &ret)
return ret, err
}
func (w Workflow0R[Return]) Execute(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) (client.WorkflowRun, error) {
opts.TaskQueue = w.queue.name
if w.queue.namespace.name != temporalClient.namespace {
// The user must provide a client that's connected to the right namespace to be able to start this workflow.
return nil, fmt.Errorf("wrong namespace for client %s vs workflow %s", temporalClient.namespace, w.queue.namespace.name)
}
return temporalClient.Client.ExecuteWorkflow(ctx, opts, w.Name)
}
type Workflow0 struct {
Name string
queue *Queue
}
func NewWorkflow0(queue *Queue, name string) Workflow0 {
queue.registerWorkflow(name, (func(context.Context) error)(nil))
return Workflow0{
Name: name,
queue: queue,
}
}
func (w Workflow0) WithImplementation(fn func(workflow.Context) error) *WorkflowWithImpl {
return &WorkflowWithImpl{workflowName: w.Name, queue: *w.queue, fn: fn}
}
func (w Workflow0) Register(wr worker.WorkflowRegistry, fn func(workflow.Context) error) {
wr.RegisterWorkflowWithOptions(fn, workflow.RegisterOptions{
Name: w.Name,
})
}
func (w Workflow0) Run(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) error {
r, err := w.Execute(ctx, temporalClient, opts)
if err != nil {
return err
}
err = r.Get(ctx, nil)
return err
}
func (w Workflow0) Execute(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions) (client.WorkflowRun, error) {
opts.TaskQueue = w.queue.name
if w.queue.namespace.name != temporalClient.namespace {
// The user must provide a client that's connected to the right namespace to be able to start this workflow.
return nil, fmt.Errorf("wrong namespace for client %s vs workflow %s", temporalClient.namespace, w.queue.namespace.name)
}
return temporalClient.Client.ExecuteWorkflow(ctx, opts, w.Name)
}
type Workflow1[Param any] struct {
Name string
queue *Queue
}
func NewWorkflow1[
Param any,
](queue *Queue, name string,
) Workflow1[Param] {
queue.registerWorkflow(name, (func(context.Context, Param) error)(nil))
return Workflow1[Param]{
Name: name,
queue: queue,
}
}
func (w Workflow1[Param]) WithImplementation(fn func(workflow.Context, Param) error) *WorkflowWithImpl {
return &WorkflowWithImpl{workflowName: w.Name, queue: *w.queue, fn: fn}
}
func (w Workflow1[Param]) Register(wr worker.WorkflowRegistry, fn func(workflow.Context, Param) error) {
wr.RegisterWorkflowWithOptions(fn, workflow.RegisterOptions{
Name: w.Name,
})
}
func (w Workflow1[Param]) Run(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions, param Param) error {
r, err := w.Execute(ctx, temporalClient, opts, param)
if err != nil {
return err
}
err = r.Get(ctx, nil)
return err
}
func (w Workflow1[Param]) Execute(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions, param Param) (client.WorkflowRun, error) {
opts.TaskQueue = w.queue.name
if w.queue.namespace.name != temporalClient.namespace {
// The user must provide a client that's connected to the right namespace to be able to start this workflow.
return nil, fmt.Errorf("wrong namespace for client %s vs workflow %s", temporalClient.namespace, w.queue.namespace.name)
}
return temporalClient.Client.ExecuteWorkflow(ctx, opts, w.Name, param)
}
func (w Workflow1[Param]) RunChild(ctx workflow.Context, opts workflow.ChildWorkflowOptions, param Param) error {
err := w.ExecuteChild(ctx, opts, param).Get(ctx, nil)
if err != nil {
return err
}
return nil
}
func (w Workflow1[Param]) ExecuteChild(ctx workflow.Context, opts workflow.ChildWorkflowOptions, param Param) workflow.ChildWorkflowFuture {
opts.TaskQueue = w.queue.name
opts.Namespace = w.queue.namespace.name
return workflow.ExecuteChildWorkflow(workflow.WithChildOptions(ctx, opts), w.Name, param)
}
func (w Workflow1[Param]) SetSchedule(ctx context.Context, temporalClient *Client, opts client.ScheduleOptions, param Param) error {
return setSchedule(ctx, temporalClient, opts, w.Name, w.queue, []any{param})
}
type Workflow1R[Param any, Return any] struct {
Name string
queue *Queue
}
func NewWorkflow1R[
Param any,
Return any,
](queue *Queue, name string,
) Workflow1R[Param, Return] {
queue.registerWorkflow(name, (func(context.Context, Param) (Return, error))(nil))
return Workflow1R[Param, Return]{
Name: name,
queue: queue,
}
}
func (w Workflow1R[Param, Return]) WithImplementation(fn func(workflow.Context, Param) (Return, error)) *WorkflowWithImpl {
return &WorkflowWithImpl{workflowName: w.Name, queue: *w.queue, fn: fn}
}
func (w Workflow1R[Param, Return]) Register(wr worker.WorkflowRegistry, fn func(workflow.Context, Param) (Return, error)) {
wr.RegisterWorkflowWithOptions(fn, workflow.RegisterOptions{
Name: w.Name,
})
}
func (w Workflow1R[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
}
func (w Workflow1R[Param, Return]) Execute(ctx context.Context, temporalClient *Client, opts client.StartWorkflowOptions, param Param) (client.WorkflowRun, error) {
opts.TaskQueue = w.queue.name
if w.queue.namespace.name != temporalClient.namespace {
// The user must provide a client that's connected to the right namespace to be able to start this workflow.
return nil, fmt.Errorf("wrong namespace for client %s vs workflow %s", temporalClient.namespace, w.queue.namespace.name)
}
return temporalClient.Client.ExecuteWorkflow(ctx, opts, w.Name, param)
}
func (w Workflow1R[Param, Return]) RunChild(ctx workflow.Context, opts workflow.ChildWorkflowOptions, param Param) (Return, error) {
var o Return
err := w.ExecuteChild(ctx, opts, param).Get(ctx, &o)
if err != nil {
return o, err
}
return o, nil
}
func (w Workflow1R[Param, Return]) ExecuteChild(ctx workflow.Context, opts workflow.ChildWorkflowOptions, param Param) workflow.ChildWorkflowFuture {
opts.TaskQueue = w.queue.name
opts.Namespace = w.queue.namespace.name
return workflow.ExecuteChildWorkflow(workflow.WithChildOptions(ctx, opts), w.Name, param)
}
func (w Workflow1R[Param, Return]) SetSchedule(ctx context.Context, temporalClient *Client, opts client.ScheduleOptions, param Param) error {
return setSchedule(ctx, temporalClient, opts, w.Name, w.queue, []any{param})
}