forked from uber-go/cadence-client
/
internal_workflow_client.go
837 lines (746 loc) · 29.2 KB
/
internal_workflow_client.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
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package internal
import (
"context"
"errors"
"fmt"
"reflect"
"time"
"github.com/pborman/uuid"
"github.com/uber-go/tally"
"go.uber.org/cadence/.gen/go/cadence/workflowserviceclient"
s "go.uber.org/cadence/.gen/go/shared"
"go.uber.org/cadence/encoded"
"go.uber.org/cadence/internal/common"
"go.uber.org/cadence/internal/common/backoff"
"go.uber.org/cadence/internal/common/metrics"
)
// Assert that structs do indeed implement the interfaces
var _ Client = (*workflowClient)(nil)
var _ DomainClient = (*domainClient)(nil)
const (
defaultDecisionTaskTimeoutInSecs = 10
defaultGetHistoryTimeoutInSecs = 25
)
type (
// workflowClient is the client for starting a workflow execution.
workflowClient struct {
workflowService workflowserviceclient.Interface
domain string
metricsScope *metrics.TaggedScope
identity string
dataConverter encoded.DataConverter
}
// domainClient is the client for managing domains.
domainClient struct {
workflowService workflowserviceclient.Interface
metricsScope tally.Scope
identity string
}
// WorkflowRun represents a started non child workflow
WorkflowRun interface {
// GetID return workflow ID, which will be same as StartWorkflowOptions.ID if provided.
GetID() string
// GetRunID return the first started workflow run ID (please see below)
GetRunID() string
// Get will fill the workflow execution result to valuePtr,
// if workflow execution is a success, or return corresponding,
// error. This is a blocking API.
Get(ctx context.Context, valuePtr interface{}) error
// NOTE: if the started workflow return ContinueAsNewError during the workflow execution, the
// return result of GetRunID() will be the started workflow run ID, not the new run ID caused by ContinueAsNewError,
// however, Get(ctx context.Context, valuePtr interface{}) will return result from the run which did not return ContinueAsNewError.
// Say ExecuteWorkflow started a workflow, in its first run, has run ID "run ID 1", and returned ContinueAsNewError,
// the second run has run ID "run ID 2" and return some result other than ContinueAsNewError:
// GetRunID() will always return "run ID 1" and Get(ctx context.Context, valuePtr interface{}) will return the result of second run.
// NOTE: DO NOT USE client.ExecuteWorkflow API INSIDE A WORKFLOW, USE workflow.ExecuteChildWorkflow instead
}
// workflowRunImpl is an implementation of WorkflowRun
workflowRunImpl struct {
workflowFn interface{}
workflowID string
firstRunID string
currentRunID string
iterFn func(ctx context.Context, runID string) HistoryEventIterator
dataConverter encoded.DataConverter
}
// HistoryEventIterator represents the interface for
// history event iterator
HistoryEventIterator interface {
// HasNext return whether this iterator has next value
HasNext() bool
// Next returns the next history events and error
// The errors it can return:
// - EntityNotExistsError
// - BadRequestError
// - InternalServiceError
Next() (*s.HistoryEvent, error)
}
// historyEventIteratorImpl is the implementation of HistoryEventIterator
historyEventIteratorImpl struct {
// whether this iterator is initialized
initialized bool
// local cached histroy events and corresponding comsuming index
nextEventIndex int
events []*s.HistoryEvent
// token to get next page of history events
nexttoken []byte
// err when getting next page of history events
err error
// func which use a next token to get next page of history events
paginate func(nexttoken []byte) (*s.GetWorkflowExecutionHistoryResponse, error)
}
)
// StartWorkflow starts a workflow execution
// The user can use this to start using a functor like.
// Either by
// StartWorkflow(options, "workflowTypeName", arg1, arg2, arg3)
// or
// StartWorkflow(options, workflowExecuteFn, arg1, arg2, arg3)
// The current timeout resolution implementation is in seconds and uses math.Ceil(d.Seconds()) as the duration. But is
// subjected to change in the future.
func (wc *workflowClient) StartWorkflow(
ctx context.Context,
options StartWorkflowOptions,
workflowFunc interface{},
args ...interface{},
) (*WorkflowExecution, error) {
workflowID := options.ID
if len(workflowID) == 0 {
workflowID = uuid.NewRandom().String()
}
if options.TaskList == "" {
return nil, errors.New("missing TaskList")
}
executionTimeout := common.Int32Ceil(options.ExecutionStartToCloseTimeout.Seconds())
if executionTimeout <= 0 {
return nil, errors.New("missing or invalid ExecutionStartToCloseTimeout")
}
decisionTaskTimeout := common.Int32Ceil(options.DecisionTaskStartToCloseTimeout.Seconds())
if decisionTaskTimeout < 0 {
return nil, errors.New("negative DecisionTaskStartToCloseTimeout provided")
}
if decisionTaskTimeout == 0 {
decisionTaskTimeout = defaultDecisionTaskTimeoutInSecs
}
// Validate type and its arguments.
workflowType, input, err := getValidatedWorkflowFunction(workflowFunc, args, wc.dataConverter)
if err != nil {
return nil, err
}
startRequest := &s.StartWorkflowExecutionRequest{
Domain: common.StringPtr(wc.domain),
RequestId: common.StringPtr(uuid.New()),
WorkflowId: common.StringPtr(workflowID),
WorkflowType: workflowTypePtr(*workflowType),
TaskList: common.TaskListPtr(s.TaskList{Name: common.StringPtr(options.TaskList)}),
Input: input,
ExecutionStartToCloseTimeoutSeconds: common.Int32Ptr(executionTimeout),
TaskStartToCloseTimeoutSeconds: common.Int32Ptr(decisionTaskTimeout),
Identity: common.StringPtr(wc.identity),
WorkflowIdReusePolicy: options.WorkflowIDReusePolicy.toThriftPtr(),
}
var response *s.StartWorkflowExecutionResponse
// Start creating workflow request.
err = backoff.Retry(ctx,
func() error {
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
var err1 error
response, err1 = wc.workflowService.StartWorkflowExecution(tchCtx, startRequest, opt...)
return err1
}, serviceOperationRetryPolicy, isServiceTransientError)
if err != nil {
return nil, err
}
if wc.metricsScope != nil {
scope := wc.metricsScope.GetTaggedScope(tagTaskList, options.TaskList, tagWorkflowType, workflowType.Name)
scope.Counter(metrics.WorkflowStartCounter).Inc(1)
}
executionInfo := &WorkflowExecution{
ID: workflowID,
RunID: response.GetRunId()}
return executionInfo, nil
}
// ExecuteWorkflow starts a workflow execution and wait until this workflow reaches the end state, such as
// workflow finished successfully or timeout.
// The user can use this to start using a functor like below and get the workflow execution result, as encoded.Value
// Either by
// RunWorkflow(options, "workflowTypeName", arg1, arg2, arg3)
// or
// RunWorkflow(options, workflowExecuteFn, arg1, arg2, arg3)
// The current timeout resolution implementation is in seconds and uses math.Ceil(d.Seconds()) as the duration. But is
// subjected to change in the future.
// NOTE: the context.Context should have a fairly large timeout, since workflow execution may take a while to be finished
func (wc *workflowClient) ExecuteWorkflow(ctx context.Context, options StartWorkflowOptions, workflow interface{}, args ...interface{}) (WorkflowRun, error) {
// start the workflow execution
var runID string
var workflowID string
executionInfo, err := wc.StartWorkflow(ctx, options, workflow, args...)
if err != nil {
if alreadyStartedErr, ok := err.(*s.WorkflowExecutionAlreadyStartedError); ok {
runID = alreadyStartedErr.GetRunId()
// Assumption is that AlreadyStarted is never returned when options.ID is empty as UUID generated by
// StartWorkflow is not going to collide ever.
workflowID = options.ID
} else {
return nil, err
}
} else {
runID = executionInfo.RunID
workflowID = executionInfo.ID
}
iterFn := func(fnCtx context.Context, fnRunID string) HistoryEventIterator {
return wc.GetWorkflowHistory(fnCtx, workflowID, fnRunID, true, s.HistoryEventFilterTypeCloseEvent)
}
return &workflowRunImpl{
workflowFn: workflow,
workflowID: workflowID,
firstRunID: runID,
currentRunID: runID,
iterFn: iterFn,
dataConverter: wc.dataConverter,
}, nil
}
// SignalWorkflow signals a workflow in execution.
func (wc *workflowClient) SignalWorkflow(ctx context.Context, workflowID string, runID string, signalName string, arg interface{}) error {
input, err := encodeArg(wc.dataConverter, arg)
if err != nil {
return err
}
request := &s.SignalWorkflowExecutionRequest{
Domain: common.StringPtr(wc.domain),
WorkflowExecution: &s.WorkflowExecution{
WorkflowId: common.StringPtr(workflowID),
RunId: getRunID(runID),
},
SignalName: common.StringPtr(signalName),
Input: input,
Identity: common.StringPtr(wc.identity),
}
return backoff.Retry(ctx,
func() error {
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
return wc.workflowService.SignalWorkflowExecution(tchCtx, request, opt...)
}, serviceOperationRetryPolicy, isServiceTransientError)
}
// SignalWithStartWorkflow sends a signal to a running workflow.
// If the workflow is not running or not found, it starts the workflow and then sends the signal in transaction.
func (wc *workflowClient) SignalWithStartWorkflow(ctx context.Context, workflowID string, signalName string, signalArg interface{},
options StartWorkflowOptions, workflowFunc interface{}, workflowArgs ...interface{}) (*WorkflowExecution, error) {
signalInput, err := encodeArg(wc.dataConverter, signalArg)
if err != nil {
return nil, err
}
if workflowID == "" {
workflowID = uuid.NewRandom().String()
}
if options.TaskList == "" {
return nil, errors.New("missing TaskList")
}
executionTimeout := common.Int32Ceil(options.ExecutionStartToCloseTimeout.Seconds())
if executionTimeout <= 0 {
return nil, errors.New("missing or invalid ExecutionStartToCloseTimeout")
}
decisionTaskTimeout := common.Int32Ceil(options.DecisionTaskStartToCloseTimeout.Seconds())
if decisionTaskTimeout < 0 {
return nil, errors.New("negative DecisionTaskStartToCloseTimeout provided")
}
if decisionTaskTimeout == 0 {
decisionTaskTimeout = defaultDecisionTaskTimeoutInSecs
}
// Validate type and its arguments.
workflowType, input, err := getValidatedWorkflowFunction(workflowFunc, workflowArgs, wc.dataConverter)
if err != nil {
return nil, err
}
signalWithStartRequest := &s.SignalWithStartWorkflowExecutionRequest{
Domain: common.StringPtr(wc.domain),
RequestId: common.StringPtr(uuid.New()),
WorkflowId: common.StringPtr(workflowID),
WorkflowType: workflowTypePtr(*workflowType),
TaskList: common.TaskListPtr(s.TaskList{Name: common.StringPtr(options.TaskList)}),
Input: input,
ExecutionStartToCloseTimeoutSeconds: common.Int32Ptr(executionTimeout),
TaskStartToCloseTimeoutSeconds: common.Int32Ptr(decisionTaskTimeout),
SignalName: common.StringPtr(signalName),
SignalInput: signalInput,
Identity: common.StringPtr(wc.identity),
}
var response *s.StartWorkflowExecutionResponse
// Start creating workflow request.
err = backoff.Retry(ctx,
func() error {
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
var err1 error
response, err1 = wc.workflowService.SignalWithStartWorkflowExecution(tchCtx, signalWithStartRequest, opt...)
return err1
}, serviceOperationRetryPolicy, isServiceTransientError)
if err != nil {
return nil, err
}
if wc.metricsScope != nil {
scope := wc.metricsScope.GetTaggedScope(tagTaskList, options.TaskList, tagWorkflowType, workflowType.Name)
scope.Counter(metrics.WorkflowSignalWithStartCounter).Inc(1)
}
executionInfo := &WorkflowExecution{
ID: options.ID,
RunID: response.GetRunId()}
return executionInfo, nil
}
// CancelWorkflow cancels a workflow in execution. It allows workflow to properly clean up and gracefully close.
// workflowID is required, other parameters are optional.
// If runID is omit, it will terminate currently running workflow (if there is one) based on the workflowID.
func (wc *workflowClient) CancelWorkflow(ctx context.Context, workflowID string, runID string) error {
request := &s.RequestCancelWorkflowExecutionRequest{
Domain: common.StringPtr(wc.domain),
WorkflowExecution: &s.WorkflowExecution{
WorkflowId: common.StringPtr(workflowID),
RunId: getRunID(runID),
},
Identity: common.StringPtr(wc.identity),
}
return backoff.Retry(ctx,
func() error {
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
return wc.workflowService.RequestCancelWorkflowExecution(tchCtx, request, opt...)
}, serviceOperationRetryPolicy, isServiceTransientError)
}
// TerminateWorkflow terminates a workflow execution.
// workflowID is required, other parameters are optional.
// If runID is omit, it will terminate currently running workflow (if there is one) based on the workflowID.
func (wc *workflowClient) TerminateWorkflow(ctx context.Context, workflowID string, runID string, reason string, details []byte) error {
request := &s.TerminateWorkflowExecutionRequest{
Domain: common.StringPtr(wc.domain),
WorkflowExecution: &s.WorkflowExecution{
WorkflowId: common.StringPtr(workflowID),
RunId: getRunID(runID),
},
Reason: common.StringPtr(reason),
Identity: common.StringPtr(wc.identity),
}
err := backoff.Retry(ctx,
func() error {
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
return wc.workflowService.TerminateWorkflowExecution(tchCtx, request, opt...)
}, serviceOperationRetryPolicy, isServiceTransientError)
return err
}
// GetWorkflowHistory return a channel which contains the history events of a given workflow
func (wc *workflowClient) GetWorkflowHistory(ctx context.Context, workflowID string, runID string,
isLongPoll bool, filterType s.HistoryEventFilterType) HistoryEventIterator {
domain := wc.domain
paginate := func(nexttoken []byte) (*s.GetWorkflowExecutionHistoryResponse, error) {
request := &s.GetWorkflowExecutionHistoryRequest{
Domain: common.StringPtr(domain),
Execution: &s.WorkflowExecution{
WorkflowId: common.StringPtr(workflowID),
RunId: getRunID(runID),
},
WaitForNewEvent: common.BoolPtr(isLongPoll),
HistoryEventFilterType: &filterType,
NextPageToken: nexttoken,
}
var response *s.GetWorkflowExecutionHistoryResponse
var err error
Loop:
for {
err = backoff.Retry(ctx,
func() error {
var err1 error
tchCtx, cancel, opt := newChannelContext(ctx, func(builder *contextBuilder) {
if isLongPoll {
builder.Timeout = defaultGetHistoryTimeoutInSecs * time.Second
}
})
defer cancel()
response, err1 = wc.workflowService.GetWorkflowExecutionHistory(tchCtx, request, opt...)
return err1
}, serviceOperationRetryPolicy, isServiceTransientError)
if err != nil {
return nil, err
}
if isLongPoll && len(response.History.Events) == 0 && len(response.NextPageToken) != 0 {
request.NextPageToken = response.NextPageToken
continue Loop
}
break Loop
}
return response, nil
}
return &historyEventIteratorImpl{
paginate: paginate,
}
}
// CompleteActivity reports activity completed. activity Execute method can return activity.ErrResultPending to
// indicate the activity is not completed when it's Execute method returns. In that case, this CompleteActivity() method
// should be called when that activity is completed with the actual result and error. If err is nil, activity task
// completed event will be reported; if err is CanceledError, activity task cancelled event will be reported; otherwise,
// activity task failed event will be reported.
func (wc *workflowClient) CompleteActivity(ctx context.Context, taskToken []byte, result interface{}, err error) error {
if taskToken == nil {
return errors.New("invalid task token provided")
}
var data []byte
if result != nil {
var err0 error
data, err0 = encodeArg(wc.dataConverter, result)
if err0 != nil {
return err0
}
}
request := convertActivityResultToRespondRequest(wc.identity, taskToken, data, err, wc.dataConverter)
return reportActivityComplete(ctx, wc.workflowService, request, wc.metricsScope)
}
// CompleteActivityById reports activity completed. Similar to CompleteActivity
// It takes domain name, workflowID, runID, activityID as arguments.
func (wc *workflowClient) CompleteActivityByID(ctx context.Context, domain, workflowID, runID, activityID string,
result interface{}, err error) error {
if activityID == "" || workflowID == "" || domain == "" {
return errors.New("empty activity or workflow id or domainName")
}
var data []byte
if result != nil {
var err0 error
data, err0 = encodeArg(wc.dataConverter, result)
if err0 != nil {
return err0
}
}
request := convertActivityResultToRespondRequestByID(wc.identity, domain, workflowID, runID, activityID, data, err, wc.dataConverter)
return reportActivityCompleteByID(ctx, wc.workflowService, request, wc.metricsScope)
}
// RecordActivityHeartbeat records heartbeat for an activity.
func (wc *workflowClient) RecordActivityHeartbeat(ctx context.Context, taskToken []byte, details ...interface{}) error {
data, err := encodeArgs(wc.dataConverter, details)
if err != nil {
return err
}
return recordActivityHeartbeat(ctx, wc.workflowService, wc.identity, taskToken, data, serviceOperationRetryPolicy)
}
// RecordActivityHeartbeatByID records heartbeat for an activity.
func (wc *workflowClient) RecordActivityHeartbeatByID(ctx context.Context,
domain, workflowID, runID, activityID string, details ...interface{}) error {
data, err := encodeArgs(wc.dataConverter, details)
if err != nil {
return err
}
return recordActivityHeartbeatByID(ctx, wc.workflowService, wc.identity, domain, workflowID, runID, activityID, data, serviceOperationRetryPolicy)
}
// ListClosedWorkflow gets closed workflow executions based on request filters
// The errors it can throw:
// - BadRequestError
// - InternalServiceError
// - EntityNotExistError
func (wc *workflowClient) ListClosedWorkflow(ctx context.Context, request *s.ListClosedWorkflowExecutionsRequest) (*s.ListClosedWorkflowExecutionsResponse, error) {
if len(request.GetDomain()) == 0 {
request.Domain = common.StringPtr(wc.domain)
}
var response *s.ListClosedWorkflowExecutionsResponse
err := backoff.Retry(ctx,
func() error {
var err1 error
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
response, err1 = wc.workflowService.ListClosedWorkflowExecutions(tchCtx, request, opt...)
return err1
}, serviceOperationRetryPolicy, isServiceTransientError)
if err != nil {
return nil, err
}
return response, nil
}
// ListClosedWorkflow gets open workflow executions based on request filters
// The errors it can throw:
// - BadRequestError
// - InternalServiceError
// - EntityNotExistError
func (wc *workflowClient) ListOpenWorkflow(ctx context.Context, request *s.ListOpenWorkflowExecutionsRequest) (*s.ListOpenWorkflowExecutionsResponse, error) {
if len(request.GetDomain()) == 0 {
request.Domain = common.StringPtr(wc.domain)
}
var response *s.ListOpenWorkflowExecutionsResponse
err := backoff.Retry(ctx,
func() error {
var err1 error
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
response, err1 = wc.workflowService.ListOpenWorkflowExecutions(tchCtx, request, opt...)
return err1
}, serviceOperationRetryPolicy, isServiceTransientError)
if err != nil {
return nil, err
}
return response, nil
}
// DescribeWorkflowExecution returns information about the specified workflow execution.
// The errors it can return:
// - BadRequestError
// - InternalServiceError
// - EntityNotExistError
func (wc *workflowClient) DescribeWorkflowExecution(ctx context.Context, workflowID, runID string) (*s.DescribeWorkflowExecutionResponse, error) {
request := &s.DescribeWorkflowExecutionRequest{
Domain: common.StringPtr(wc.domain),
Execution: &s.WorkflowExecution{
WorkflowId: common.StringPtr(workflowID),
RunId: common.StringPtr(runID),
},
}
var response *s.DescribeWorkflowExecutionResponse
err := backoff.Retry(ctx,
func() error {
var err1 error
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
response, err1 = wc.workflowService.DescribeWorkflowExecution(tchCtx, request, opt...)
return err1
}, serviceOperationRetryPolicy, isServiceTransientError)
if err != nil {
return nil, err
}
return response, nil
}
// QueryWorkflow queries a given workflow execution
// workflowID and queryType are required, other parameters are optional.
// - workflow ID of the workflow.
// - runID can be default(empty string). if empty string then it will pick the running execution of that workflow ID.
// - taskList can be default(empty string). If empty string then it will pick the taskList of the running execution of that workflow ID.
// - queryType is the type of the query.
// - args... are the optional query parameters.
// The errors it can return:
// - BadRequestError
// - InternalServiceError
// - EntityNotExistError
// - QueryFailError
func (wc *workflowClient) QueryWorkflow(ctx context.Context, workflowID string, runID string, queryType string, args ...interface{}) (encoded.Value, error) {
var input []byte
if len(args) > 0 {
var err error
if input, err = encodeArgs(wc.dataConverter, args); err != nil {
return nil, err
}
}
request := &s.QueryWorkflowRequest{
Domain: common.StringPtr(wc.domain),
Execution: &s.WorkflowExecution{
WorkflowId: common.StringPtr(workflowID),
RunId: getRunID(runID),
},
Query: &s.WorkflowQuery{
QueryType: common.StringPtr(queryType),
QueryArgs: input,
},
}
var resp *s.QueryWorkflowResponse
err := backoff.Retry(ctx,
func() error {
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
var err error
resp, err = wc.workflowService.QueryWorkflow(tchCtx, request, opt...)
return err
}, serviceOperationRetryPolicy, isServiceTransientError)
if err != nil {
return nil, err
}
return newEncodedValue(resp.QueryResult, wc.dataConverter), nil
}
// DescribeTaskList returns information about the target tasklist, right now this API returns the
// pollers which polled this tasklist in last few minutes.
// - tasklist name of tasklist
// - tasklistType type of tasklist, can be decision or activity
// The errors it can return:
// - BadRequestError
// - InternalServiceError
// - EntityNotExistError
func (wc *workflowClient) DescribeTaskList(ctx context.Context, tasklist string, tasklistType s.TaskListType) (*s.DescribeTaskListResponse, error) {
request := &s.DescribeTaskListRequest{
Domain: common.StringPtr(wc.domain),
TaskList: &s.TaskList{Name: common.StringPtr(tasklist)},
TaskListType: &tasklistType,
}
var resp *s.DescribeTaskListResponse
err := backoff.Retry(ctx,
func() error {
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
var err error
resp, err = wc.workflowService.DescribeTaskList(tchCtx, request, opt...)
return err
}, serviceOperationRetryPolicy, isServiceTransientError)
if err != nil {
return nil, err
}
return resp, nil
}
// Register a domain with cadence server
// The errors it can throw:
// - DomainAlreadyExistsError
// - BadRequestError
// - InternalServiceError
func (dc *domainClient) Register(ctx context.Context, request *s.RegisterDomainRequest) error {
return backoff.Retry(ctx,
func() error {
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
return dc.workflowService.RegisterDomain(tchCtx, request, opt...)
}, serviceOperationRetryPolicy, isServiceTransientError)
}
// Describe a domain. The domain has 3 part of information
// DomainInfo - Which has Name, Status, Description, Owner Email
// DomainConfiguration - Configuration like Workflow Execution Retention Period In Days, Whether to emit metrics.
// ReplicationConfiguration - replication config like clusters and active cluster name
// The errors it can throw:
// - EntityNotExistsError
// - BadRequestError
// - InternalServiceError
func (dc *domainClient) Describe(ctx context.Context, name string) (*s.DescribeDomainResponse, error) {
request := &s.DescribeDomainRequest{
Name: common.StringPtr(name),
}
var response *s.DescribeDomainResponse
err := backoff.Retry(ctx,
func() error {
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
var err error
response, err = dc.workflowService.DescribeDomain(tchCtx, request, opt...)
return err
}, serviceOperationRetryPolicy, isServiceTransientError)
if err != nil {
return nil, err
}
return response, nil
}
// Update a domain.
// The errors it can throw:
// - EntityNotExistsError
// - BadRequestError
// - InternalServiceError
func (dc *domainClient) Update(ctx context.Context, request *s.UpdateDomainRequest) error {
return backoff.Retry(ctx,
func() error {
tchCtx, cancel, opt := newChannelContext(ctx)
defer cancel()
_, err := dc.workflowService.UpdateDomain(tchCtx, request, opt...)
return err
}, serviceOperationRetryPolicy, isServiceTransientError)
}
func getRunID(runID string) *string {
if runID == "" {
// Cadence Server will pick current runID if provided empty.
return nil
}
return common.StringPtr(runID)
}
func (iter *historyEventIteratorImpl) HasNext() bool {
if iter.nextEventIndex < len(iter.events) || iter.err != nil {
return true
} else if !iter.initialized || len(iter.nexttoken) != 0 {
iter.initialized = true
response, err := iter.paginate(iter.nexttoken)
iter.nextEventIndex = 0
if err == nil {
iter.events = response.History.Events
iter.nexttoken = response.NextPageToken
iter.err = nil
} else {
iter.events = nil
iter.nexttoken = nil
iter.err = err
}
if iter.nextEventIndex < len(iter.events) || iter.err != nil {
return true
}
return false
}
return false
}
func (iter *historyEventIteratorImpl) Next() (*s.HistoryEvent, error) {
// if caller call the Next() when iteration is over, just return nil, nil
if !iter.HasNext() {
// debug.PrintStack()
panic("HistoryEventIterator Next() called without checking HasNext()")
}
// we have cached events
if iter.nextEventIndex < len(iter.events) {
index := iter.nextEventIndex
iter.nextEventIndex++
return iter.events[index], nil
} else if iter.err != nil {
// we have err, clear that iter.err and return err
err := iter.err
iter.err = nil
return nil, err
}
panic("HistoryEventIterator Next() should return either a history event or a err")
}
func (workflowRun *workflowRunImpl) GetRunID() string {
return workflowRun.firstRunID
}
func (workflowRun *workflowRunImpl) GetID() string {
return workflowRun.workflowID
}
func (workflowRun *workflowRunImpl) Get(ctx context.Context, valuePtr interface{}) error {
iter := workflowRun.iterFn(ctx, workflowRun.currentRunID)
if !iter.HasNext() {
panic("could not get last history event for workflow")
}
closeEvent, err := iter.Next()
if err != nil {
return err
}
switch closeEvent.GetEventType() {
case s.EventTypeWorkflowExecutionCompleted:
attributes := closeEvent.WorkflowExecutionCompletedEventAttributes
if valuePtr == nil || attributes.Result == nil {
return nil
}
rf := reflect.ValueOf(valuePtr)
if rf.Type().Kind() != reflect.Ptr {
return errors.New("value parameter is not a pointer")
}
err = deSerializeFunctionResult(workflowRun.workflowFn, attributes.Result, valuePtr, workflowRun.dataConverter)
case s.EventTypeWorkflowExecutionFailed:
attributes := closeEvent.WorkflowExecutionFailedEventAttributes
err = constructError(attributes.GetReason(), attributes.Details, workflowRun.dataConverter)
case s.EventTypeWorkflowExecutionCanceled:
attributes := closeEvent.WorkflowExecutionCanceledEventAttributes
details := newEncodedValues(attributes.Details, workflowRun.dataConverter)
err = NewCanceledError(details)
case s.EventTypeWorkflowExecutionTerminated:
err = newTerminatedError()
case s.EventTypeWorkflowExecutionTimedOut:
attributes := closeEvent.WorkflowExecutionTimedOutEventAttributes
err = NewTimeoutError(attributes.GetTimeoutType())
case s.EventTypeWorkflowExecutionContinuedAsNew:
attributes := closeEvent.WorkflowExecutionContinuedAsNewEventAttributes
workflowRun.currentRunID = attributes.GetNewExecutionRunId()
return workflowRun.Get(ctx, valuePtr)
default:
err = fmt.Errorf("Unexpected event type %s when handling workflow execution result", closeEvent.GetEventType())
}
return err
}