/
interceptor.go
328 lines (261 loc) · 12.4 KB
/
interceptor.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
// The MIT License
//
// Copyright (c) 2021 Temporal Technologies Inc. All rights reserved.
//
// 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"
"time"
commonpb "go.temporal.io/api/common/v1"
"go.temporal.io/sdk/converter"
"go.temporal.io/sdk/internal/common/metrics"
"go.temporal.io/sdk/log"
)
// Interceptor is a common interface for all interceptors. See documentation in
// the interceptor package for more details.
type Interceptor interface {
ClientInterceptor
WorkerInterceptor
}
// WorkerInterceptor is a common interface for all interceptors. See
// documentation in the interceptor package for more details.
type WorkerInterceptor interface {
// InterceptActivity is called before each activity interception needed with
// the next interceptor in the chain.
InterceptActivity(ctx context.Context, next ActivityInboundInterceptor) ActivityInboundInterceptor
// InterceptWorkflow is called before each workflow interception needed with
// the next interceptor in the chain.
InterceptWorkflow(ctx Context, next WorkflowInboundInterceptor) WorkflowInboundInterceptor
mustEmbedWorkerInterceptorBase()
}
// ActivityInboundInterceptor is an interface for all activity calls originating
// from the server. See documentation in the interceptor package for more
// details.
type ActivityInboundInterceptor interface {
// Init is the first call of this interceptor. Implementations can change/wrap
// the outbound interceptor before calling Init on the next interceptor.
Init(outbound ActivityOutboundInterceptor) error
// ExecuteActivity is called when an activity is to be run on this worker.
// interceptor.Header will return a non-nil map for this context.
ExecuteActivity(ctx context.Context, in *ExecuteActivityInput) (interface{}, error)
mustEmbedActivityInboundInterceptorBase()
}
// ExecuteActivityInput is the input to ActivityInboundInterceptor.ExecuteActivity.
type ExecuteActivityInput struct {
Args []interface{}
}
// ActivityOutboundInterceptor is an interface for all activity calls
// originating from the SDK. See documentation in the interceptor package for
// more details.
type ActivityOutboundInterceptor interface {
// GetInfo intercepts activity.GetInfo.
GetInfo(ctx context.Context) ActivityInfo
// GetLogger intercepts activity.GetLogger.
GetLogger(ctx context.Context) log.Logger
// GetMetricsHandler intercepts activity.GetMetricsHandler.
GetMetricsHandler(ctx context.Context) metrics.Handler
// RecordHeartbeat intercepts activity.RecordHeartbeat.
RecordHeartbeat(ctx context.Context, details ...interface{})
// HasHeartbeatDetails intercepts activity.HasHeartbeatDetails.
HasHeartbeatDetails(ctx context.Context) bool
// GetHeartbeatDetails intercepts activity.GetHeartbeatDetails.
GetHeartbeatDetails(ctx context.Context, d ...interface{}) error
// GetWorkerStopChannel intercepts activity.GetWorkerStopChannel.
GetWorkerStopChannel(ctx context.Context) <-chan struct{}
mustEmbedActivityOutboundInterceptorBase()
}
// WorkflowInboundInterceptor is an interface for all workflow calls originating
// from the server. See documentation in the interceptor package for more
// details.
type WorkflowInboundInterceptor interface {
// Init is the first call of this interceptor. Implementations can change/wrap
// the outbound interceptor before calling Init on the next interceptor.
Init(outbound WorkflowOutboundInterceptor) error
// ExecuteWorkflow is called when a workflow is to be run on this worker.
// interceptor.WorkflowHeader will return a non-nil map for this context.
ExecuteWorkflow(ctx Context, in *ExecuteWorkflowInput) (interface{}, error)
// HandleSignal is called when a signal is sent to a workflow on this worker.
// interceptor.WorkflowHeader will return a non-nil map for this context.
HandleSignal(ctx Context, in *HandleSignalInput) error
// HandleQuery is called when a query is sent to a workflow on this worker.
// interceptor.WorkflowHeader will return a non-nil map for this context.
HandleQuery(ctx Context, in *HandleQueryInput) (interface{}, error)
mustEmbedWorkflowInboundInterceptorBase()
}
// ExecuteWorkflowInput is the input to
// WorkflowInboundInterceptor.ExecuteWorkflow.
type ExecuteWorkflowInput struct {
Args []interface{}
}
// HandleSignalInput is the input to WorkflowInboundInterceptor.HandleSignal.
type HandleSignalInput struct {
SignalName string
// Arg is the signal argument. It is presented as a primitive payload since
// the type needed for decode is not available at the time of interception.
Arg *commonpb.Payloads
}
// HandleQueryInput is the input to WorkflowInboundInterceptor.HandleQuery.
type HandleQueryInput struct {
QueryType string
Args []interface{}
}
// WorkflowOutboundInterceptor is an interface for all workflow calls
// originating from the SDK. See documentation in the interceptor package for
// more details.
type WorkflowOutboundInterceptor interface {
// Go intercepts workflow.Go.
Go(ctx Context, name string, f func(ctx Context)) Context
// ExecuteActivity intercepts workflow.ExecuteActivity.
// interceptor.WorkflowHeader will return a non-nil map for this context.
ExecuteActivity(ctx Context, activityType string, args ...interface{}) Future
// ExecuteLocalActivity intercepts workflow.ExecuteLocalActivity.
// interceptor.WorkflowHeader will return a non-nil map for this context.
ExecuteLocalActivity(ctx Context, activityType string, args ...interface{}) Future
// ExecuteChildWorkflow intercepts workflow.ExecuteChildWorkflow.
// interceptor.WorkflowHeader will return a non-nil map for this context.
ExecuteChildWorkflow(ctx Context, childWorkflowType string, args ...interface{}) ChildWorkflowFuture
// GetInfo intercepts workflow.GetInfo.
GetInfo(ctx Context) *WorkflowInfo
// GetLogger intercepts workflow.GetLogger.
GetLogger(ctx Context) log.Logger
// GetMetricsHandler intercepts workflow.GetMetricsHandler.
GetMetricsHandler(ctx Context) metrics.Handler
// Now intercepts workflow.Now.
Now(ctx Context) time.Time
// NewTimer intercepts workflow.NewTimer.
NewTimer(ctx Context, d time.Duration) Future
// Sleep intercepts workflow.Sleep.
Sleep(ctx Context, d time.Duration) (err error)
// RequestCancelExternalWorkflow intercepts
// workflow.RequestCancelExternalWorkflow.
RequestCancelExternalWorkflow(ctx Context, workflowID, runID string) Future
// SignalExternalWorkflow intercepts workflow.SignalExternalWorkflow.
// interceptor.WorkflowHeader will return a non-nil map for this context.
SignalExternalWorkflow(ctx Context, workflowID, runID, signalName string, arg interface{}) Future
// SignalChildWorkflow intercepts
// workflow.ChildWorkflowFuture.SignalChildWorkflow.
// interceptor.WorkflowHeader will return a non-nil map for this context.
SignalChildWorkflow(ctx Context, workflowID, signalName string, arg interface{}) Future
// UpsertSearchAttributes intercepts workflow.UpsertSearchAttributes.
UpsertSearchAttributes(ctx Context, attributes map[string]interface{}) error
// GetSignalChannel intercepts workflow.GetSignalChannel.
GetSignalChannel(ctx Context, signalName string) ReceiveChannel
// SideEffect intercepts workflow.SideEffect.
SideEffect(ctx Context, f func(ctx Context) interface{}) converter.EncodedValue
// MutableSideEffect intercepts workflow.MutableSideEffect.
MutableSideEffect(
ctx Context,
id string,
f func(ctx Context) interface{},
equals func(a, b interface{}) bool,
) converter.EncodedValue
// GetVersion intercepts workflow.GetVersion.
GetVersion(ctx Context, changeID string, minSupported, maxSupported Version) Version
// SetQueryHandler intercepts workflow.SetQueryHandler.
SetQueryHandler(ctx Context, queryType string, handler interface{}) error
// IsReplaying intercepts workflow.IsReplaying.
IsReplaying(ctx Context) bool
// HasLastCompletionResult intercepts workflow.HasLastCompletionResult.
HasLastCompletionResult(ctx Context) bool
// GetLastCompletionResult intercepts workflow.GetLastCompletionResult.
GetLastCompletionResult(ctx Context, d ...interface{}) error
// GetLastError intercepts workflow.GetLastError.
GetLastError(ctx Context) error
// NewContinueAsNewError intercepts workflow.NewContinueAsNewError.
// interceptor.WorkflowHeader will return a non-nil map for this context.
NewContinueAsNewError(ctx Context, wfn interface{}, args ...interface{}) error
mustEmbedWorkflowOutboundInterceptorBase()
}
// ClientInterceptor for providing a ClientOutboundInterceptor to intercept
// certain workflow-specific client calls from the SDK. See documentation in the
// interceptor package for more details.
type ClientInterceptor interface {
// This is called on client creation if set via client options
InterceptClient(next ClientOutboundInterceptor) ClientOutboundInterceptor
mustEmbedClientInterceptorBase()
}
// ClientOutboundInterceptor is an interface for certain workflow-specific calls
// originating from the SDK. See documentation in the interceptor package for
// more details.
type ClientOutboundInterceptor interface {
// ExecuteWorkflow intercepts client.Client.ExecuteWorkflow.
// interceptor.Header will return a non-nil map for this context.
ExecuteWorkflow(context.Context, *ClientExecuteWorkflowInput) (WorkflowRun, error)
// SignalWorkflow intercepts client.Client.SignalWorkflow.
// interceptor.Header will return a non-nil map for this context.
SignalWorkflow(context.Context, *ClientSignalWorkflowInput) error
// SignalWithStartWorkflow intercepts client.Client.SignalWithStartWorkflow.
// interceptor.Header will return a non-nil map for this context.
SignalWithStartWorkflow(context.Context, *ClientSignalWithStartWorkflowInput) (WorkflowRun, error)
// CancelWorkflow intercepts client.Client.CancelWorkflow.
CancelWorkflow(context.Context, *ClientCancelWorkflowInput) error
// TerminateWorkflow intercepts client.Client.TerminateWorkflow.
TerminateWorkflow(context.Context, *ClientTerminateWorkflowInput) error
// QueryWorkflow intercepts client.Client.QueryWorkflow.
// interceptor.Header will return a non-nil map for this context.
QueryWorkflow(context.Context, *ClientQueryWorkflowInput) (converter.EncodedValue, error)
mustEmbedClientOutboundInterceptorBase()
}
// ClientExecuteWorkflowInput is the input to
// ClientOutboundInterceptor.ExecuteWorkflow.
type ClientExecuteWorkflowInput struct {
Options *StartWorkflowOptions
WorkflowType string
Args []interface{}
}
// ClientSignalWorkflowInput is the input to
// ClientOutboundInterceptor.SignalWorkflow.
type ClientSignalWorkflowInput struct {
WorkflowID string
RunID string
SignalName string
Arg interface{}
}
// ClientSignalWithStartWorkflowInput is the input to
// ClientOutboundInterceptor.SignalWithStartWorkflow.
type ClientSignalWithStartWorkflowInput struct {
SignalName string
SignalArg interface{}
Options *StartWorkflowOptions
WorkflowType string
Args []interface{}
}
// ClientCancelWorkflowInput is the input to
// ClientOutboundInterceptor.CancelWorkflow.
type ClientCancelWorkflowInput struct {
WorkflowID string
RunID string
}
// ClientTerminateWorkflowInput is the input to
// ClientOutboundInterceptor.TerminateWorkflow.
type ClientTerminateWorkflowInput struct {
WorkflowID string
RunID string
Reason string
Details []interface{}
}
// ClientQueryWorkflowInput is the input to
// ClientOutboundInterceptor.QueryWorkflow.
type ClientQueryWorkflowInput struct {
WorkflowID string
RunID string
QueryType string
Args []interface{}
}