forked from dapr/python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworkflow_runtime.py
More file actions
508 lines (413 loc) · 19.4 KB
/
Copy pathworkflow_runtime.py
File metadata and controls
508 lines (413 loc) · 19.4 KB
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
# -*- coding: utf-8 -*-
"""
Copyright 2023 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import inspect
import time
from functools import wraps
from typing import Any, Awaitable, Callable, Optional, Sequence, TypeVar, Union
import grpc
from dapr.clients import DaprInternalError
from dapr.clients.grpc.interceptors import DaprClientTimeoutInterceptor
from dapr.clients.http.client import DAPR_API_TOKEN_HEADER
from dapr.conf import settings
from dapr.conf.helpers import GrpcEndpoint
from dapr.ext.workflow._durabletask import task, worker
from dapr.ext.workflow._durabletask.internal.shared import is_async_callable as _is_async_callable
from dapr.ext.workflow.dapr_workflow_context import DaprWorkflowContext
from dapr.ext.workflow.logger import Logger, LoggerOptions
from dapr.ext.workflow.util import getAddress
from dapr.ext.workflow.workflow_activity_context import Activity, WorkflowActivityContext
from dapr.ext.workflow.workflow_context import Workflow
from . import _model_protocol
T = TypeVar('T')
TInput = TypeVar('TInput')
TOutput = TypeVar('TOutput')
ClientInterceptor = Union[
grpc.UnaryUnaryClientInterceptor,
grpc.UnaryStreamClientInterceptor,
grpc.StreamUnaryClientInterceptor,
grpc.StreamStreamClientInterceptor,
]
# Durabletask returns decoded JSON, so we type the input as ``object | None`` and let the
# wrapper narrow it via the activity's declared model.
SyncActivityWrapper = Callable[[task.ActivityContext, object | None], object]
AsyncActivityWrapper = Callable[[task.ActivityContext, object | None], Awaitable[object]]
ActivityWrapper = SyncActivityWrapper | AsyncActivityWrapper
def _coerce_activity_input(inp: object | None, input_model: type | None) -> object | None:
"""Coerce the raw input to the activity's declared model, if it has one."""
if inp is None or input_model is None or isinstance(inp, input_model):
return inp
return _model_protocol.coerce_to_model(inp, input_model)
def _make_activity_wrapper(fn: Activity, logger: Logger) -> ActivityWrapper:
"""Wrap a user activity for the durabletask worker.
Returns:
An ``async def`` wrapper for async activities, a plain ``def`` for sync.
"""
accepts_input, input_model = _model_protocol.resolve_input(fn)
def _call_args(ctx: task.ActivityContext, inp: object | None) -> tuple:
wf_ctx = WorkflowActivityContext(ctx)
if not accepts_input:
return (wf_ctx,)
return (wf_ctx, _coerce_activity_input(inp, input_model))
def _log_failure(ctx: task.ActivityContext, exc: Exception) -> None:
activity_id = getattr(ctx, 'task_id', 'unknown')
logger.warning(f'Activity execution failed - task_id: {activity_id}, error: {exc}')
is_async = _is_async_callable(fn)
activity_name = getattr(fn, '__name__', repr(fn))
kind = 'async' if is_async else 'sync'
logger.debug(f"Registering activity '{activity_name}' on the {kind} dispatch path.")
if is_async:
async def async_activity_wrapper(
ctx: task.ActivityContext, inp: object | None = None
) -> object:
try:
return await fn(*_call_args(ctx, inp))
except Exception as exc:
_log_failure(ctx, exc)
raise
return async_activity_wrapper
def sync_activity_wrapper(ctx: task.ActivityContext, inp: object | None = None) -> object:
try:
return fn(*_call_args(ctx, inp))
except Exception as exc:
_log_failure(ctx, exc)
raise
return sync_activity_wrapper
class WorkflowRuntime:
"""WorkflowRuntime is the entry point for registering workflows and activities."""
def __init__(
self,
host: Optional[str] = None,
port: Optional[str] = None,
logger_options: Optional[LoggerOptions] = None,
interceptors: Optional[Sequence[ClientInterceptor]] = None,
maximum_concurrent_activity_work_items: Optional[int] = None,
maximum_concurrent_orchestration_work_items: Optional[int] = None,
maximum_thread_pool_workers: Optional[int] = None,
worker_ready_timeout: Optional[float] = None,
):
self._logger = Logger('WorkflowRuntime', logger_options)
self._worker_ready_timeout = 30.0 if worker_ready_timeout is None else worker_ready_timeout
metadata = tuple()
if settings.DAPR_API_TOKEN:
metadata = ((DAPR_API_TOKEN_HEADER, settings.DAPR_API_TOKEN),)
address = getAddress(host, port)
try:
uri = GrpcEndpoint(address)
except ValueError as error:
raise DaprInternalError(f'{error}') from error
options = self._logger.get_options()
all_interceptors = []
if interceptors:
all_interceptors.extend(interceptors)
all_interceptors.append(DaprClientTimeoutInterceptor())
self.__worker = worker.TaskHubGrpcWorker(
host_address=uri.endpoint,
metadata=metadata,
secure_channel=uri.tls,
log_handler=options.log_handler,
log_formatter=options.log_formatter,
interceptors=all_interceptors,
concurrency_options=worker.ConcurrencyOptions(
maximum_concurrent_activity_work_items=maximum_concurrent_activity_work_items,
maximum_concurrent_orchestration_work_items=maximum_concurrent_orchestration_work_items,
maximum_thread_pool_workers=maximum_thread_pool_workers,
),
)
def register_workflow(self, fn: Workflow, *, name: Optional[str] = None):
effective_name = name or fn.__name__
self._logger.info(f"Registering workflow '{effective_name}' with runtime")
accepts_input, input_model = _model_protocol.resolve_input(fn)
def orchestrationWrapper(ctx: task.OrchestrationContext, inp: Optional[TInput] = None):
"""Responsible to call Workflow function in orchestrationWrapper"""
instance_id = getattr(ctx, 'instance_id', 'unknown')
try:
daprWfContext = DaprWorkflowContext(ctx, self._logger.get_options())
if not accepts_input:
result = fn(daprWfContext)
else:
if (
(inp is not None)
and (input_model is not None)
and not isinstance(inp, input_model)
):
inp = _model_protocol.coerce_to_model(inp, input_model)
result = fn(daprWfContext, inp)
return result
except Exception as e:
self._logger.exception(
f'Workflow execution failed - instance_id: {instance_id}, error: {e}'
)
raise
if hasattr(fn, '_workflow_registered'):
# whenever a workflow is registered, it has a _dapr_alternate_name attribute
alt_name = fn.__dict__['_dapr_alternate_name']
raise ValueError(f'Workflow {fn.__name__} already registered as {alt_name}')
if hasattr(fn, '_dapr_alternate_name'):
alt_name = fn._dapr_alternate_name
if name is not None:
m = f'Workflow {fn.__name__} already has an alternate name {alt_name}'
raise ValueError(m)
else:
fn.__dict__['_dapr_alternate_name'] = name if name else fn.__name__
self.__worker._registry.add_named_orchestrator(
fn.__dict__['_dapr_alternate_name'], orchestrationWrapper
)
fn.__dict__['_workflow_registered'] = True
def register_versioned_workflow(
self, fn: Workflow, *, name: str, version_name: Optional[str] = None, is_latest: bool
):
effective_name = name or fn.__name__
self._logger.info(
f"Registering version {version_name} of workflow '{effective_name}' with runtime"
)
accepts_input, input_model = _model_protocol.resolve_input(fn)
def orchestrationWrapper(ctx: task.OrchestrationContext, inp: Optional[TInput] = None):
"""Responsible to call Workflow function in orchestrationWrapper"""
daprWfContext = DaprWorkflowContext(ctx, self._logger.get_options())
if not accepts_input:
return fn(daprWfContext)
if (inp is not None) and (input_model is not None) and not isinstance(inp, input_model):
inp = _model_protocol.coerce_to_model(inp, input_model)
return fn(daprWfContext, inp)
if hasattr(fn, '_workflow_registered'):
# whenever a workflow is registered, it has a _dapr_alternate_name attribute
alt_name = fn.__dict__['_dapr_alternate_name']
raise ValueError(f'Workflow {fn.__name__} already registered as {alt_name}')
if hasattr(fn, '_dapr_alternate_name'):
alt_name = fn._dapr_alternate_name
if name is not None:
m = f'Workflow {fn.__name__} already has an alternate name {alt_name}'
raise ValueError(m)
else:
fn.__dict__['_dapr_alternate_name'] = name
actual_version_name = version_name if version_name is not None else fn.__name__
self.__worker._registry.add_named_orchestrator(
name,
orchestrationWrapper,
version_name=actual_version_name,
is_latest=is_latest,
)
fn.__dict__['_workflow_registered'] = True
def register_activity(self, fn: Activity, *, name: Optional[str] = None):
"""Register a workflow activity. ``def`` and ``async def`` are both supported.
Async activities run on the worker's event loop. Sync activities run in the
thread pool sized by ``maximum_thread_pool_workers``.
"""
effective_name = name or fn.__name__
self._logger.info(f"Registering activity '{effective_name}' with runtime")
activity_wrapper = _make_activity_wrapper(fn, self._logger)
if hasattr(fn, '_activity_registered'):
# whenever an activity is registered, it has a _dapr_alternate_name attribute
alt_name = fn.__dict__['_dapr_alternate_name']
raise ValueError(f'Activity {fn.__name__} already registered as {alt_name}')
if hasattr(fn, '_dapr_alternate_name'):
alt_name = fn._dapr_alternate_name
if name is not None:
m = f'Activity {fn.__name__} already has an alternate name {alt_name}'
raise ValueError(m)
else:
fn.__dict__['_dapr_alternate_name'] = name if name else fn.__name__
self.__worker._registry.add_named_activity(
fn.__dict__['_dapr_alternate_name'], activity_wrapper
)
fn.__dict__['_activity_registered'] = True
def wait_for_worker_ready(self, timeout: float = 30.0) -> bool:
"""
Wait for the worker's gRPC stream to become ready to receive work items.
This method polls the worker's is_worker_ready() method until it returns True
or the timeout is reached.
Args:
timeout: Maximum time in seconds to wait for the worker to be ready.
Defaults to 30 seconds.
Returns:
True if the worker's gRPC stream is ready to receive work items, False if timeout.
"""
if not hasattr(self.__worker, 'is_worker_ready'):
return False
elapsed = 0.0
poll_interval = 0.1 # 100ms
while elapsed < timeout:
if self.__worker.is_worker_ready():
return True
time.sleep(poll_interval)
elapsed += poll_interval
self._logger.warning(
f'WorkflowRuntime worker readiness check timed out after {timeout} seconds'
)
return False
def start(self):
"""Starts the listening for work items on a background thread.
This method waits for the worker's gRPC stream to be fully initialized
before returning, ensuring that workflows can be scheduled immediately
after start() completes.
"""
try:
try:
self.__worker.start()
except Exception as start_error:
self._logger.exception(f'WorkflowRuntime worker did not start: {start_error}')
raise
# Verify the worker and its stream reader are ready
if hasattr(self.__worker, 'is_worker_ready'):
try:
is_ready = self.wait_for_worker_ready(timeout=self._worker_ready_timeout)
if not is_ready:
raise RuntimeError('WorkflowRuntime worker and its stream are not ready')
else:
self._logger.debug(
'WorkflowRuntime worker is ready and its stream can receive work items'
)
except Exception as ready_error:
self._logger.exception(
f'WorkflowRuntime wait_for_worker_ready() raised exception: {ready_error}'
)
raise ready_error
else:
self._logger.warning(
'Unable to verify stream readiness. Workflows scheduled immediately may not be received.'
)
except Exception:
raise
def shutdown(self):
"""Stops the listening for work items on a background thread."""
try:
self.__worker.stop()
except Exception:
raise
def versioned_workflow(
self,
__fn: Workflow = None,
*,
name: str,
version_name: Optional[str] = None,
is_latest: bool,
):
def wrapper(fn: Workflow):
self.register_versioned_workflow(
fn, name=name, version_name=version_name, is_latest=is_latest
)
@wraps(fn)
def innerfn():
return fn
if hasattr(fn, '_dapr_alternate_name'):
innerfn.__dict__['_dapr_alternate_name'] = fn.__dict__['_dapr_alternate_name']
else:
innerfn.__dict__['_dapr_alternate_name'] = name
innerfn.__signature__ = inspect.signature(fn)
return innerfn
if __fn:
# This case is true when the decorator is used without arguments
# and the function to be decorated is passed as the first argument.
return wrapper(__fn)
return wrapper
def workflow(self, __fn: Workflow = None, *, name: Optional[str] = None):
"""Decorator to register a workflow function.
This example shows how to register a workflow function with a name:
from dapr.ext.workflow import WorkflowRuntime
wfr = WorkflowRuntime()
@wfr.workflow(name="add")
def add(ctx, x: int, y: int) -> int:
return x + y
This example shows how to register a workflow function without
an alternate name:
from dapr.ext.workflow import WorkflowRuntime
wfr = WorkflowRuntime()
@wfr.workflow
def add(ctx, x: int, y: int) -> int:
return x + y
Args:
name (Optional[str], optional): Name to identify the workflow function as in
the workflow runtime. Defaults to None.
"""
def wrapper(fn: Workflow):
self.register_workflow(fn, name=name)
@wraps(fn)
def innerfn():
return fn
if hasattr(fn, '_dapr_alternate_name'):
innerfn.__dict__['_dapr_alternate_name'] = fn.__dict__['_dapr_alternate_name']
else:
innerfn.__dict__['_dapr_alternate_name'] = name if name else fn.__name__
innerfn.__signature__ = inspect.signature(fn)
return innerfn
if __fn:
# This case is true when the decorator is used without arguments
# and the function to be decorated is passed as the first argument.
return wrapper(__fn)
return wrapper
def activity(self, __fn: Activity = None, *, name: Optional[str] = None):
"""Decorator to register an activity function.
This example shows how to register an activity function with an alternate name:
from dapr.ext.workflow import WorkflowRuntime
wfr = WorkflowRuntime()
@wfr.activity(name="add")
def add(ctx, x: int, y: int) -> int:
return x + y
This example shows how to register an activity function without an alternate name:
from dapr.ext.workflow import WorkflowRuntime
wfr = WorkflowRuntime()
@wfr.activity
def add(ctx, x: int, y: int) -> int:
return x + y
Args:
name (Optional[str], optional): Name to identify the activity function as in
the workflow runtime. Defaults to None.
"""
def wrapper(fn: Activity):
self.register_activity(fn, name=name)
@wraps(fn)
def innerfn():
return fn
if hasattr(fn, '_dapr_alternate_name'):
innerfn.__dict__['_dapr_alternate_name'] = fn.__dict__['_dapr_alternate_name']
else:
innerfn.__dict__['_dapr_alternate_name'] = name if name else fn.__name__
innerfn.__signature__ = inspect.signature(fn)
return innerfn
if __fn:
# This case is true when the decorator is used without arguments
# and the function to be decorated is passed as the first argument.
return wrapper(__fn)
return wrapper
def alternate_name(name: Optional[str] = None):
"""Decorator to register a workflow or activity function with an alternate name.
This example shows how to register a workflow function with an alternate name:
from dapr.ext.workflow import WorkflowRuntime
wfr = WorkflowRuntime()
@wfr.workflow
@alternate_name(add")
def add(ctx, x: int, y: int) -> int:
return x + y
Args:
name (Optional[str], optional): Name to identify the workflow or activity function as in
the workflow runtime. Defaults to None.
"""
def wrapper(fn: Any):
if hasattr(fn, '_dapr_alternate_name'):
raise ValueError(
f'Function {fn.__name__} already has an alternate name {fn._dapr_alternate_name}'
)
fn.__dict__['_dapr_alternate_name'] = name if name else fn.__name__
if _is_async_callable(fn):
@wraps(fn)
async def innerfn(*args, **kwargs):
return await fn(*args, **kwargs)
else:
@wraps(fn)
def innerfn(*args, **kwargs):
return fn(*args, **kwargs)
innerfn.__dict__['_dapr_alternate_name'] = name if name else fn.__name__
innerfn.__signature__ = inspect.signature(fn)
return innerfn
return wrapper