forked from cadence-workflow/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
signal.go
163 lines (144 loc) · 5.73 KB
/
signal.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
// Copyright (c) 2019 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 canary
import (
"context"
"errors"
"fmt"
"go.uber.org/cadence/activity"
"go.uber.org/cadence/workflow"
"go.uber.org/zap"
)
const (
signalName = "signal-name"
signalValue = "canary.signal"
)
func init() {
registerWorkflow(signalWorkflow, wfTypeSignal)
registerWorkflow(signalExternalWorkflow, wfTypeSignalExternal)
registerActivity(signalActivity, activityTypeSignal)
}
// signalWorkflow is the workflow implementation to test SignalWorkflowExecution API
// it simply spins up an activity which sends a signal to the parent
func signalWorkflow(ctx workflow.Context, inputScheduledTimeNanos int64) error {
scheduledTimeNanos := getScheduledTimeFromInputIfNonZero(ctx, inputScheduledTimeNanos)
domain := workflow.GetInfo(ctx).Domain
var err error
profile, err := beginWorkflow(ctx, wfTypeSignal, scheduledTimeNanos)
if err != nil {
return err
}
execInfo := workflow.GetInfo(ctx).WorkflowExecution
sigName := fmt.Sprintf("sig.%v", execInfo.RunID)
sigCh := workflow.GetSignalChannel(ctx, sigName)
aCtx := workflow.WithActivityOptions(ctx, newActivityOptions())
err = workflow.ExecuteActivity(aCtx, activityTypeSignal, workflow.Now(ctx).UnixNano(), execInfo, sigName).Get(ctx, nil)
if err != nil {
workflow.GetLogger(ctx).Error("signal activity failed", zap.Error(err))
return profile.end(err)
}
var sigValue string
sigCh.Receive(ctx, &sigValue)
if sigValue != signalValue {
workflow.GetLogger(ctx).Error("wrong signal value received", zap.String("value", sigValue))
return profile.end(errors.New("invalid signal value"))
}
sigCh.Receive(ctx, &sigValue)
if sigValue != signalValue {
workflow.GetLogger(ctx).Error("wrong signal value received", zap.String("value", sigValue))
return profile.end(errors.New("invalid signal value"))
}
cwo := newChildWorkflowOptions(domain, wfTypeSignalExternal+"-child")
childCtx := workflow.WithChildOptions(ctx, cwo)
childFuture := workflow.ExecuteChildWorkflow(childCtx, wfTypeSignalExternal, workflow.Now(ctx).UnixNano(), sleepDuration)
signalFuture := childFuture.SignalChildWorkflow(childCtx, signalName, signalValue)
err = signalFuture.Get(childCtx, nil)
if err != nil {
return profile.end(err)
}
decisionSignalTestFn := func(useRunID bool) error {
workflowIDSuffix := "-with-run-ID"
if !useRunID {
workflowIDSuffix = "-without-run-ID"
}
cwo = newChildWorkflowOptions(domain, wfTypeSignalExternal+workflowIDSuffix)
childCtx = workflow.WithChildOptions(ctx, cwo)
childFuture = workflow.ExecuteChildWorkflow(childCtx, wfTypeSignalExternal, workflow.Now(ctx).UnixNano(), sleepDuration)
childExecution := &workflow.Execution{}
err = childFuture.GetChildWorkflowExecution().Get(childCtx, childExecution)
if err != nil {
return err
}
if !useRunID {
childExecution.RunID = ""
}
signalFuture = workflow.SignalExternalWorkflow(ctx, childExecution.ID, childExecution.RunID, signalName, signalValue)
err = signalFuture.Get(ctx, nil)
if err != nil {
workflow.GetLogger(ctx).Info("signalWorkflow failed: fail to send siganl to child workflow", zap.Error(err))
return err
}
err = childFuture.Get(childCtx, nil)
if err != nil {
workflow.GetLogger(ctx).Info("signalWorkflow failed: child workflow return err", zap.Error(err))
return err
}
return nil
}
err = decisionSignalTestFn(false)
if err != nil {
return profile.end(err)
}
err = decisionSignalTestFn(true)
return profile.end(err)
}
// signalActivity sends a signal to the parent workflow
// that created this activity
func signalActivity(ctx context.Context, scheduledTimeNanos int64, execInfo workflow.Execution, signalName string) error {
scope := activity.GetMetricsScope(ctx)
var err error
scope, sw := recordActivityStart(scope, activityTypeSignal, scheduledTimeNanos)
defer recordActivityEnd(scope, sw, err)
client := getActivityContext(ctx).cadence
err = client.SignalWorkflow(context.Background(), execInfo.ID, execInfo.RunID, signalName, signalValue)
if err != nil {
return err
}
err = client.SignalWorkflow(context.Background(), execInfo.ID, "", signalName, signalValue)
if err != nil {
return err
}
return nil
}
// signalExternalWorkflow receive a signal from the parent workflow
func signalExternalWorkflow(ctx workflow.Context, scheduledTimeNanos int64) error {
profile, err := beginWorkflow(ctx, wfTypeSignalExternal, scheduledTimeNanos)
if err != nil {
return profile.end(err)
}
var value string
signalCh := workflow.GetSignalChannel(ctx, signalName)
signalCh.Receive(ctx, &value)
if value != signalValue {
workflow.GetLogger(ctx).Error("wrong signal value received", zap.String("value", value))
return profile.end(errors.New("invalid signal value"))
}
return profile.end(nil)
}