-
Notifications
You must be signed in to change notification settings - Fork 7
/
updatabletimer_xns_temporal.pb.go
764 lines (667 loc) · 26.6 KB
/
updatabletimer_xns_temporal.pb.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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
// Code generated by protoc-gen-go_temporal. DO NOT EDIT.
// versions:
//
// protoc-gen-go_temporal 1.13.1-next (b7a15c407f9a9490670a48b7f288c936cdd1bc48)
// go go1.22.3
// protoc (unknown)
//
// source: example/updatabletimer/v1/updatabletimer.proto
package updatabletimerv1xns
import (
"context"
"errors"
"fmt"
v1 "github.com/cludden/protoc-gen-go-temporal/gen/example/updatabletimer/v1"
v12 "github.com/cludden/protoc-gen-go-temporal/gen/temporal/v1"
v13 "github.com/cludden/protoc-gen-go-temporal/gen/temporal/xns/v1"
expression "github.com/cludden/protoc-gen-go-temporal/pkg/expression"
xns "github.com/cludden/protoc-gen-go-temporal/pkg/xns"
uuid "github.com/google/uuid"
v11 "go.temporal.io/api/enums/v1"
activity "go.temporal.io/sdk/activity"
client "go.temporal.io/sdk/client"
temporal "go.temporal.io/sdk/temporal"
worker "go.temporal.io/sdk/worker"
workflow "go.temporal.io/sdk/workflow"
anypb "google.golang.org/protobuf/types/known/anypb"
durationpb "google.golang.org/protobuf/types/known/durationpb"
"time"
)
// ExampleOptions is used to configure example.updatabletimer.v1.Example xns activity registration
type ExampleOptions struct {
// errorConverter is used to customize error
errorConverter func(error) error
// filter is used to filter xns activity registrations. It receives as
// input the original activity name, and should return one of the following:
// 1. the original activity name, for no changes
// 2. a modified activity name, to override the original activity name
// 3. an empty string, to skip registration
filter func(string) string
}
// NewExampleOptions initializes a new ExampleOptions value
func NewExampleOptions() *ExampleOptions {
return &ExampleOptions{}
}
// WithErrorConverter overrides the default error converter applied to xns activity errors
func (opts *ExampleOptions) WithErrorConverter(errorConverter func(error) error) *ExampleOptions {
opts.errorConverter = errorConverter
return opts
}
// Filter is used to filter registered xns activities or customize their name
func (opts *ExampleOptions) WithFilter(filter func(string) string) *ExampleOptions {
opts.filter = filter
return opts
}
// convertError is applied to all xns activity errors
func (opts *ExampleOptions) convertError(err error) error {
if err == nil {
return nil
}
if opts != nil && opts.errorConverter != nil {
return opts.errorConverter(err)
}
return xns.ErrorToApplicationError(err)
}
// filterActivity is used to filter xns activity registrations
func (opts *ExampleOptions) filterActivity(name string) string {
if opts == nil || opts.filter == nil {
return name
}
return opts.filter(name)
}
// exampleOptions is a reference to the ExampleOptions initialized at registration
var exampleOptions *ExampleOptions
// RegisterExampleActivities registers example.updatabletimer.v1.Example cross-namespace activities
func RegisterExampleActivities(r worker.ActivityRegistry, c v1.ExampleClient, options ...*ExampleOptions) {
if exampleOptions == nil && len(options) > 0 && options[0] != nil {
exampleOptions = options[0]
}
a := &exampleActivities{c}
if name := exampleOptions.filterActivity("example.updatabletimer.v1.Example.CancelWorkflow"); name != "" {
r.RegisterActivityWithOptions(a.CancelWorkflow, activity.RegisterOptions{Name: name})
}
if name := exampleOptions.filterActivity(v1.UpdatableTimerWorkflowName); name != "" {
r.RegisterActivityWithOptions(a.UpdatableTimer, activity.RegisterOptions{Name: name})
}
if name := exampleOptions.filterActivity(v1.GetWakeUpTimeQueryName); name != "" {
r.RegisterActivityWithOptions(a.GetWakeUpTime, activity.RegisterOptions{Name: name})
}
if name := exampleOptions.filterActivity(v1.UpdateWakeUpTimeSignalName); name != "" {
r.RegisterActivityWithOptions(a.UpdateWakeUpTime, activity.RegisterOptions{Name: name})
}
}
// UpdatableTimerWorkflowOptions are used to configure a(n) UpdatableTimer workflow execution
type UpdatableTimerWorkflowOptions struct {
ActivityOptions *workflow.ActivityOptions
Detached bool
HeartbeatInterval time.Duration
ParentClosePolicy v11.ParentClosePolicy
StartWorkflowOptions *client.StartWorkflowOptions
}
// NewUpdatableTimerWorkflowOptions initializes a new UpdatableTimerWorkflowOptions value
func NewUpdatableTimerWorkflowOptions() *UpdatableTimerWorkflowOptions {
return &UpdatableTimerWorkflowOptions{}
}
// WithActivityOptions can be used to customize the activity options
func (opts *UpdatableTimerWorkflowOptions) WithActivityOptions(ao workflow.ActivityOptions) *UpdatableTimerWorkflowOptions {
opts.ActivityOptions = &ao
return opts
}
// WithDetached can be used to start a workflow execution and exit immediately
func (opts *UpdatableTimerWorkflowOptions) WithDetached(d bool) *UpdatableTimerWorkflowOptions {
opts.Detached = d
return opts
}
// WithHeartbeatInterval can be used to customize the activity heartbeat interval
func (opts *UpdatableTimerWorkflowOptions) WithHeartbeatInterval(d time.Duration) *UpdatableTimerWorkflowOptions {
opts.HeartbeatInterval = d
return opts
}
// WithParentClosePolicy can be used to customize the cancellation propagation behavior
func (opts *UpdatableTimerWorkflowOptions) WithParentClosePolicy(policy v11.ParentClosePolicy) *UpdatableTimerWorkflowOptions {
opts.ParentClosePolicy = policy
return opts
}
// WithStartWorkflowOptions can be used to customize the start workflow options
func (opts *UpdatableTimerWorkflowOptions) WithStartWorkflow(swo client.StartWorkflowOptions) *UpdatableTimerWorkflowOptions {
opts.StartWorkflowOptions = &swo
return opts
}
// UpdatableTimerRun provides a handle to a UpdatableTimer workflow execution
type UpdatableTimerRun interface {
// Cancel cancels the workflow
Cancel(workflow.Context) error
// Future returns the inner workflow.Future
Future() workflow.Future
// Get returns the inner workflow.Future
Get(workflow.Context) error
// ID returns the workflow id
ID() string
// GetWakeUpTime retrieves the current timer expiration timestamp
GetWakeUpTime(workflow.Context, ...*GetWakeUpTimeQueryOptions) (*v1.GetWakeUpTimeOutput, error)
// GetWakeUpTime retrieves the current timer expiration timestamp
GetWakeUpTimeAsync(workflow.Context, ...*GetWakeUpTimeQueryOptions) (GetWakeUpTimeQueryHandle, error)
// UpdateWakeUpTime updates the timer expiration timestamp
UpdateWakeUpTime(workflow.Context, *v1.UpdateWakeUpTimeInput, ...*UpdateWakeUpTimeSignalOptions) error
// UpdateWakeUpTime updates the timer expiration timestamp
UpdateWakeUpTimeAsync(workflow.Context, *v1.UpdateWakeUpTimeInput, ...*UpdateWakeUpTimeSignalOptions) (UpdateWakeUpTimeSignalHandle, error)
}
// updatableTimerRun provides a(n) UpdatableTimerRun implementation
type updatableTimerRun struct {
cancel func()
future workflow.Future
id string
}
// Cancel the underlying workflow execution
func (r *updatableTimerRun) Cancel(ctx workflow.Context) error {
if r.cancel != nil {
r.cancel()
if err := r.Get(ctx); err != nil && !errors.Is(err, workflow.ErrCanceled) {
return err
}
return nil
}
return CancelExampleWorkflow(ctx, r.id, "")
}
// Future returns the underlying activity future
func (r *updatableTimerRun) Future() workflow.Future {
return r.future
}
// Get blocks on activity completion and returns the underlying workflow result
func (r *updatableTimerRun) Get(ctx workflow.Context) error {
if err := r.future.Get(ctx, nil); err != nil {
return err
}
return nil
}
// ID returns the underlying workflow id
func (r *updatableTimerRun) ID() string {
return r.id
}
// GetWakeUpTime retrieves the current timer expiration timestamp
func (r *updatableTimerRun) GetWakeUpTime(ctx workflow.Context, opts ...*GetWakeUpTimeQueryOptions) (*v1.GetWakeUpTimeOutput, error) {
return GetWakeUpTime(ctx, r.ID(), "", opts...)
}
// GetWakeUpTime retrieves the current timer expiration timestamp
func (r *updatableTimerRun) GetWakeUpTimeAsync(ctx workflow.Context, opts ...*GetWakeUpTimeQueryOptions) (GetWakeUpTimeQueryHandle, error) {
return GetWakeUpTimeAsync(ctx, r.ID(), "", opts...)
}
// UpdateWakeUpTime updates the timer expiration timestamp
func (r *updatableTimerRun) UpdateWakeUpTime(ctx workflow.Context, req *v1.UpdateWakeUpTimeInput, opts ...*UpdateWakeUpTimeSignalOptions) error {
return UpdateWakeUpTime(ctx, r.ID(), "", req, opts...)
}
// UpdateWakeUpTime updates the timer expiration timestamp
func (r *updatableTimerRun) UpdateWakeUpTimeAsync(ctx workflow.Context, req *v1.UpdateWakeUpTimeInput, opts ...*UpdateWakeUpTimeSignalOptions) (UpdateWakeUpTimeSignalHandle, error) {
return UpdateWakeUpTimeAsync(ctx, r.ID(), "", req, opts...)
}
// UpdatableTimer describes an updatable timer workflow
func UpdatableTimer(ctx workflow.Context, req *v1.UpdatableTimerInput, opts ...*UpdatableTimerWorkflowOptions) error {
run, err := UpdatableTimerAsync(ctx, req, opts...)
if err != nil {
return err
}
return run.Get(ctx)
}
// UpdatableTimer describes an updatable timer workflow
func UpdatableTimerAsync(ctx workflow.Context, req *v1.UpdatableTimerInput, opts ...*UpdatableTimerWorkflowOptions) (UpdatableTimerRun, error) {
activityName := exampleOptions.filterActivity(v1.UpdatableTimerWorkflowName)
if activityName == "" {
return nil, temporal.NewNonRetryableApplicationError(
fmt.Sprintf("no activity registered for %s", v1.UpdatableTimerWorkflowName),
"Unimplemented",
nil,
)
}
opt := &UpdatableTimerWorkflowOptions{}
if len(opts) > 0 && opts[0] != nil {
opt = opts[0]
}
if opt.HeartbeatInterval == 0 {
opt.HeartbeatInterval = time.Second * 30
}
// configure activity options
ao := workflow.GetActivityOptions(ctx)
if opt.ActivityOptions != nil {
ao = *opt.ActivityOptions
}
if ao.HeartbeatTimeout == 0 {
ao.HeartbeatTimeout = opt.HeartbeatInterval * 2
}
// WaitForCancellation must be set otherwise the underlying workflow is not guaranteed to be canceled
ao.WaitForCancellation = true
if ao.StartToCloseTimeout == 0 && ao.ScheduleToCloseTimeout == 0 {
ao.ScheduleToCloseTimeout = 86400000000000 // 1 day
}
ctx = workflow.WithActivityOptions(ctx, ao)
// configure start workflow options
wo := client.StartWorkflowOptions{}
if opt.StartWorkflowOptions != nil {
wo = *opt.StartWorkflowOptions
}
if wo.ID == "" {
if err := workflow.SideEffect(ctx, func(ctx workflow.Context) any {
id, err := expression.EvalExpression(v1.UpdatableTimerIdexpression, req.ProtoReflect())
if err != nil {
workflow.GetLogger(ctx).Error("error evaluating id expression for \"example.updatabletimer.v1.Example.UpdatableTimer\" workflow", "error", err)
return nil
}
return id
}).Get(&wo.ID); err != nil {
return nil, err
}
}
if wo.ID == "" {
if err := workflow.SideEffect(ctx, func(ctx workflow.Context) any {
id, err := uuid.NewRandom()
if err != nil {
workflow.GetLogger(ctx).Error("error generating workflow id", "error", err)
return nil
}
return id
}).Get(&wo.ID); err != nil {
return nil, err
}
}
if wo.ID == "" {
return nil, temporal.NewNonRetryableApplicationError("workflow id is required", "InvalidArgument", nil)
}
// marshal start workflow options protobuf message
swo, err := xns.MarshalStartWorkflowOptions(wo)
if err != nil {
return nil, fmt.Errorf("error marshalling start workflow options: %w", err)
}
// marshal workflow request protobuf message
wreq, err := anypb.New(req)
if err != nil {
return nil, fmt.Errorf("error marshalling workflow request: %w", err)
}
var parentClosePolicy v12.ParentClosePolicy
switch opt.ParentClosePolicy {
case v11.PARENT_CLOSE_POLICY_ABANDON:
parentClosePolicy = v12.ParentClosePolicy_PARENT_CLOSE_POLICY_ABANDON
case v11.PARENT_CLOSE_POLICY_REQUEST_CANCEL:
parentClosePolicy = v12.ParentClosePolicy_PARENT_CLOSE_POLICY_REQUEST_CANCEL
case v11.PARENT_CLOSE_POLICY_TERMINATE:
parentClosePolicy = v12.ParentClosePolicy_PARENT_CLOSE_POLICY_TERMINATE
}
ctx, cancel := workflow.WithCancel(ctx)
return &updatableTimerRun{
cancel: cancel,
id: wo.ID,
future: workflow.ExecuteActivity(ctx, activityName, &v13.WorkflowRequest{
Detached: opt.Detached,
HeartbeatInterval: durationpb.New(opt.HeartbeatInterval),
ParentClosePolicy: parentClosePolicy,
Request: wreq,
StartWorkflowOptions: swo,
}),
}, nil
}
// GetWakeUpTimeQueryOptions are used to configure a(n) example.updatabletimer.v1.Example.GetWakeUpTime query execution
type GetWakeUpTimeQueryOptions struct {
ActivityOptions *workflow.ActivityOptions
HeartbeatInterval time.Duration
}
// NewGetWakeUpTimeQueryOptions initializes a new GetWakeUpTimeQueryOptions value
func NewGetWakeUpTimeQueryOptions() *GetWakeUpTimeQueryOptions {
return &GetWakeUpTimeQueryOptions{}
}
// WithActivityOptions can be used to customize the activity options
func (opts *GetWakeUpTimeQueryOptions) WithActivityOptions(ao workflow.ActivityOptions) *GetWakeUpTimeQueryOptions {
opts.ActivityOptions = &ao
return opts
}
// WithHeartbeatInterval can be used to customize the activity heartbeat interval
func (opts *GetWakeUpTimeQueryOptions) WithHeartbeatInterval(d time.Duration) *GetWakeUpTimeQueryOptions {
opts.HeartbeatInterval = d
return opts
}
// GetWakeUpTimeQueryHandle provides a handle for a example.updatabletimer.v1.Example.GetWakeUpTime query activity
type GetWakeUpTimeQueryHandle interface {
// Cancel cancels the workflow
Cancel(workflow.Context) error
// Future returns the inner workflow.Future
Future() workflow.Future
// Get returns the inner workflow.Future
Get(workflow.Context) (*v1.GetWakeUpTimeOutput, error)
}
// getWakeUpTimeQueryHandle provides a(n) GetWakeUpTimeQueryHandle implementation
type getWakeUpTimeQueryHandle struct {
cancel func()
future workflow.Future
}
// Cancel the underlying query activity
func (r *getWakeUpTimeQueryHandle) Cancel(ctx workflow.Context) error {
r.cancel()
if _, err := r.Get(ctx); err != nil && !errors.Is(err, workflow.ErrCanceled) {
return err
}
return nil
}
// Future returns the underlying activity future
func (r *getWakeUpTimeQueryHandle) Future() workflow.Future {
return r.future
}
// Get blocks on activity completion and returns the underlying query result
func (r *getWakeUpTimeQueryHandle) Get(ctx workflow.Context) (*v1.GetWakeUpTimeOutput, error) {
var resp v1.GetWakeUpTimeOutput
if err := r.future.Get(ctx, &resp); err != nil {
return nil, err
}
return &resp, nil
}
// GetWakeUpTime retrieves the current timer expiration timestamp
func GetWakeUpTime(ctx workflow.Context, workflowID string, runID string, opts ...*GetWakeUpTimeQueryOptions) (*v1.GetWakeUpTimeOutput, error) {
handle, err := GetWakeUpTimeAsync(ctx, workflowID, runID, opts...)
if err != nil {
return nil, err
}
return handle.Get(ctx)
}
// GetWakeUpTimeAsync executes a(n) example.updatabletimer.v1.Example.GetWakeUpTime query and returns a handle to the activity
func GetWakeUpTimeAsync(ctx workflow.Context, workflowID string, runID string, opts ...*GetWakeUpTimeQueryOptions) (GetWakeUpTimeQueryHandle, error) {
activityName := exampleOptions.filterActivity(v1.GetWakeUpTimeQueryName)
if activityName == "" {
return nil, temporal.NewNonRetryableApplicationError(
fmt.Sprintf("no activity registered for %s", v1.GetWakeUpTimeQueryName),
"Unimplemented",
nil,
)
}
opt := &GetWakeUpTimeQueryOptions{}
if len(opts) > 0 && opts[0] != nil {
opt = opts[0]
}
if opt.HeartbeatInterval == 0 {
opt.HeartbeatInterval = time.Second * 30
}
// configure activity options
ao := workflow.GetActivityOptions(ctx)
if opt.ActivityOptions != nil {
ao = *opt.ActivityOptions
}
if ao.HeartbeatTimeout == 0 {
ao.HeartbeatTimeout = opt.HeartbeatInterval * 2
}
// WaitForCancellation must be set otherwise the underlying workflow is not guaranteed to be canceled
ao.WaitForCancellation = true
if ao.StartToCloseTimeout == 0 && ao.ScheduleToCloseTimeout == 0 {
ao.ScheduleToCloseTimeout = 60000000000 // 1 minute
}
ctx = workflow.WithActivityOptions(ctx, ao)
ctx, cancel := workflow.WithCancel(ctx)
return &getWakeUpTimeQueryHandle{
cancel: cancel,
future: workflow.ExecuteActivity(ctx, activityName, &v13.QueryRequest{
HeartbeatInterval: durationpb.New(opt.HeartbeatInterval),
WorkflowId: workflowID,
RunId: runID,
}),
}, nil
}
// UpdateWakeUpTimeSignalOptions are used to configure a(n) example.updatabletimer.v1.Example.UpdateWakeUpTime signal execution
type UpdateWakeUpTimeSignalOptions struct {
ActivityOptions *workflow.ActivityOptions
HeartbeatInterval time.Duration
}
// NewUpdateWakeUpTimeSignalOptions initializes a new UpdateWakeUpTimeSignalOptions value
func NewUpdateWakeUpTimeSignalOptions() *UpdateWakeUpTimeSignalOptions {
return &UpdateWakeUpTimeSignalOptions{}
}
// WithActivityOptions can be used to customize the activity options
func (opts *UpdateWakeUpTimeSignalOptions) WithActivityOptions(ao workflow.ActivityOptions) *UpdateWakeUpTimeSignalOptions {
opts.ActivityOptions = &ao
return opts
}
// WithHeartbeatInterval can be used to customize the activity heartbeat interval
func (opts *UpdateWakeUpTimeSignalOptions) WithHeartbeatInterval(d time.Duration) *UpdateWakeUpTimeSignalOptions {
opts.HeartbeatInterval = d
return opts
}
// UpdateWakeUpTimeSignalHandle provides a handle for a example.updatabletimer.v1.Example.UpdateWakeUpTime signal activity
type UpdateWakeUpTimeSignalHandle interface {
// Cancel cancels the workflow
Cancel(workflow.Context) error
// Future returns the inner workflow.Future
Future() workflow.Future
// Get returns the inner workflow.Future
Get(workflow.Context) error
}
// updateWakeUpTimeSignalHandle provides a(n) UpdateWakeUpTimeQueryHandle implementation
type updateWakeUpTimeSignalHandle struct {
cancel func()
future workflow.Future
}
// Cancel the underlying signal activity
func (r *updateWakeUpTimeSignalHandle) Cancel(ctx workflow.Context) error {
r.cancel()
if err := r.Get(ctx); err != nil && !errors.Is(err, workflow.ErrCanceled) {
return err
}
return nil
}
// Future returns the underlying activity future
func (r *updateWakeUpTimeSignalHandle) Future() workflow.Future {
return r.future
}
// Get blocks on activity completion
func (r *updateWakeUpTimeSignalHandle) Get(ctx workflow.Context) error {
return r.future.Get(ctx, nil)
}
// UpdateWakeUpTime updates the timer expiration timestamp
func UpdateWakeUpTime(ctx workflow.Context, workflowID string, runID string, req *v1.UpdateWakeUpTimeInput, opts ...*UpdateWakeUpTimeSignalOptions) error {
handle, err := UpdateWakeUpTimeAsync(ctx, workflowID, runID, req, opts...)
if err != nil {
return err
}
return handle.Get(ctx)
}
// UpdateWakeUpTimeAsync executes a(n) example.updatabletimer.v1.Example.UpdateWakeUpTime signal
func UpdateWakeUpTimeAsync(ctx workflow.Context, workflowID string, runID string, req *v1.UpdateWakeUpTimeInput, opts ...*UpdateWakeUpTimeSignalOptions) (UpdateWakeUpTimeSignalHandle, error) {
activityName := exampleOptions.filterActivity(v1.UpdateWakeUpTimeSignalName)
if activityName == "" {
return nil, temporal.NewNonRetryableApplicationError(
fmt.Sprintf("no activity registered for %s", v1.UpdateWakeUpTimeSignalName),
"Unimplemented",
nil,
)
}
opt := &UpdateWakeUpTimeSignalOptions{}
if len(opts) > 0 && opts[0] != nil {
opt = opts[0]
}
if opt.HeartbeatInterval == 0 {
opt.HeartbeatInterval = time.Second * 30
}
// configure activity options
ao := workflow.GetActivityOptions(ctx)
if opt.ActivityOptions != nil {
ao = *opt.ActivityOptions
}
if ao.HeartbeatTimeout == 0 {
ao.HeartbeatTimeout = opt.HeartbeatInterval * 2
}
// WaitForCancellation must be set otherwise the underlying workflow is not guaranteed to be canceled
ao.WaitForCancellation = true
if ao.StartToCloseTimeout == 0 && ao.ScheduleToCloseTimeout == 0 {
ao.ScheduleToCloseTimeout = 60000000000 // 1 minute
}
ctx = workflow.WithActivityOptions(ctx, ao)
// marshal workflow request
wreq, err := anypb.New(req)
if err != nil {
return nil, fmt.Errorf("error marshalling workflow request: %w", err)
}
ctx, cancel := workflow.WithCancel(ctx)
return &updateWakeUpTimeSignalHandle{
cancel: cancel,
future: workflow.ExecuteActivity(ctx, activityName, &v13.SignalRequest{
HeartbeatInterval: durationpb.New(opt.HeartbeatInterval),
WorkflowId: workflowID,
RunId: runID,
Request: wreq,
}),
}, nil
}
// CancelExampleWorkflow cancels an existing workflow
func CancelExampleWorkflow(ctx workflow.Context, workflowID string, runID string) error {
return CancelExampleWorkflowAsync(ctx, workflowID, runID).Get(ctx, nil)
}
// CancelExampleWorkflowAsync cancels an existing workflow
func CancelExampleWorkflowAsync(ctx workflow.Context, workflowID string, runID string) workflow.Future {
activityName := exampleOptions.filterActivity("example.updatabletimer.v1.Example.CancelWorkflow")
if activityName == "" {
f, s := workflow.NewFuture(ctx)
s.SetError(temporal.NewNonRetryableApplicationError(
"no activity registered for example.updatabletimer.v1.Example.CancelWorkflow",
"Unimplemented",
nil,
))
return f
}
ao := workflow.GetActivityOptions(ctx)
if ao.StartToCloseTimeout == 0 && ao.ScheduleToCloseTimeout == 0 {
ao.StartToCloseTimeout = time.Minute
}
ctx = workflow.WithActivityOptions(ctx, ao)
return workflow.ExecuteActivity(ctx, activityName, workflowID, runID)
}
// exampleActivities provides activities that can be used to interact with a(n) Example service's workflow, queries, signals, and updates across namespaces
type exampleActivities struct {
client v1.ExampleClient
}
// CancelWorkflow cancels an existing workflow execution
func (a *exampleActivities) CancelWorkflow(ctx context.Context, workflowID string, runID string) error {
return a.client.CancelWorkflow(ctx, workflowID, runID)
}
// UpdatableTimer executes a(n) UpdatableTimer workflow via an activity
func (a *exampleActivities) UpdatableTimer(ctx context.Context, input *v13.WorkflowRequest) (err error) {
// unmarshal workflow request
var req v1.UpdatableTimerInput
if err := input.Request.UnmarshalTo(&req); err != nil {
return exampleOptions.convertError(temporal.NewNonRetryableApplicationError(
fmt.Sprintf("error unmarshalling workflow request of type %s as github.com/cludden/protoc-gen-go-temporal/gen/example/updatabletimer/v1.UpdatableTimerInput", input.Request.GetTypeUrl()),
"InvalidArgument",
err,
))
}
// initialize workflow execution
var run v1.UpdatableTimerRun
run, err = a.client.UpdatableTimerAsync(ctx, &req, v1.NewUpdatableTimerOptions().WithStartWorkflowOptions(
xns.UnmarshalStartWorkflowOptions(input.GetStartWorkflowOptions()),
))
if err != nil {
return exampleOptions.convertError(err)
}
// exit early if detached enabled
if input.GetDetached() {
return nil
}
// otherwise, wait for execution to complete in child goroutine
doneCh := make(chan struct{})
go func() {
err = run.Get(ctx)
close(doneCh)
}()
heartbeatInterval := input.GetHeartbeatInterval().AsDuration()
if heartbeatInterval == 0 {
heartbeatInterval = time.Second * 30
}
// heartbeat activity while waiting for workflow execution to complete
for {
select {
// send heartbeats periodically
case <-time.After(heartbeatInterval):
activity.RecordHeartbeat(ctx, run.ID())
// return retryable error on worker close
case <-activity.GetWorkerStopChannel(ctx):
return temporal.NewApplicationError("worker is stopping", "WorkerStopped")
// catch parent activity context cancellation. in most cases, this should indicate a
// server-sent cancellation, but there's a non-zero possibility that this cancellation
// is received due to the worker stopping, prior to detecting the closing of the worker
// stop channel. to give us an opportunity to detect a cancellation stemming from the
// worker closing, we again check to see if the worker stop channel is closed before
// propagating the cancellation
case <-ctx.Done():
select {
case <-activity.GetWorkerStopChannel(ctx):
return temporal.NewApplicationError("worker is stopping", "WorkerStopped")
default:
parentClosePolicy := input.GetParentClosePolicy()
if parentClosePolicy == v12.ParentClosePolicy_PARENT_CLOSE_POLICY_REQUEST_CANCEL || parentClosePolicy == v12.ParentClosePolicy_PARENT_CLOSE_POLICY_TERMINATE {
disconnectedCtx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
if parentClosePolicy == v12.ParentClosePolicy_PARENT_CLOSE_POLICY_REQUEST_CANCEL {
err = run.Cancel(disconnectedCtx)
} else {
err = run.Terminate(disconnectedCtx, "xns activity cancellation received", "error", ctx.Err())
}
if err != nil {
return exampleOptions.convertError(err)
}
}
return exampleOptions.convertError(temporal.NewCanceledError(ctx.Err().Error()))
}
// handle workflow completion
case <-doneCh:
return exampleOptions.convertError(err)
}
}
}
// GetWakeUpTime executes a(n) example.updatabletimer.v1.Example.GetWakeUpTime query via an activity
func (a *exampleActivities) GetWakeUpTime(ctx context.Context, input *v13.QueryRequest) (resp *v1.GetWakeUpTimeOutput, err error) {
// execute signal in child goroutine
doneCh := make(chan struct{})
go func() {
resp, err = a.client.GetWakeUpTime(ctx, input.GetWorkflowId(), input.GetRunId())
close(doneCh)
}()
heartbeatInterval := input.GetHeartbeatInterval().AsDuration()
if heartbeatInterval == 0 {
heartbeatInterval = time.Second * 10
}
// heartbeat activity while waiting for signal to complete
for {
select {
case <-time.After(heartbeatInterval):
activity.RecordHeartbeat(ctx)
case <-ctx.Done():
return nil, exampleOptions.convertError(ctx.Err())
case <-doneCh:
return resp, exampleOptions.convertError(err)
}
}
}
// UpdateWakeUpTime executes a(n) example.updatabletimer.v1.Example.UpdateWakeUpTime signal via an activity
func (a *exampleActivities) UpdateWakeUpTime(ctx context.Context, input *v13.SignalRequest) (err error) {
// unmarshal signal request
var req v1.UpdateWakeUpTimeInput
if err := input.Request.UnmarshalTo(&req); err != nil {
return exampleOptions.convertError(temporal.NewNonRetryableApplicationError(
fmt.Sprintf("error unmarshalling signal request of type %s as github.com/cludden/protoc-gen-go-temporal/gen/example/updatabletimer/v1.UpdateWakeUpTimeInput", input.Request.GetTypeUrl()),
"InvalidArgument",
err,
))
}
// execute signal in child goroutine
doneCh := make(chan struct{})
go func() {
err = a.client.UpdateWakeUpTime(ctx, input.GetWorkflowId(), input.GetRunId(), &req)
close(doneCh)
}()
heartbeatInterval := input.GetHeartbeatInterval().AsDuration()
if heartbeatInterval == 0 {
heartbeatInterval = time.Second * 10
}
// heartbeat activity while waiting for signal to complete
for {
select {
case <-time.After(heartbeatInterval):
activity.RecordHeartbeat(ctx)
case <-ctx.Done():
exampleOptions.convertError(ctx.Err())
case <-doneCh:
return exampleOptions.convertError(err)
}
}
}