forked from cadence-workflow/cadence-python-client
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactivity.py
More file actions
364 lines (291 loc) · 10.5 KB
/
activity.py
File metadata and controls
364 lines (291 loc) · 10.5 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
import inspect
from abc import ABC, abstractmethod
from contextlib import contextmanager
from contextvars import ContextVar
from dataclasses import dataclass
from datetime import timedelta, datetime
from typing import (
Iterator,
TypedDict,
Unpack,
Callable,
ParamSpec,
TypeVar,
overload,
Awaitable,
Self,
Protocol,
cast,
Union,
Any,
Concatenate,
Generic,
Type,
)
from cadence import Client
from cadence._internal.activity._definition import (
AsyncImpl,
SyncImpl,
AsyncMethodImpl,
SyncMethodImpl,
)
from cadence._internal.fn_signature import FnSignature
from cadence.workflow import ActivityOptions
@dataclass(frozen=True)
class ActivityInfo:
task_token: bytes
workflow_type: str
workflow_domain: str
workflow_id: str
workflow_run_id: str
activity_id: str
activity_type: str
task_list: str
heartbeat_timeout: timedelta
scheduled_timestamp: datetime
started_timestamp: datetime
start_to_close_timeout: timedelta
attempt: int
def client() -> Client:
return ActivityContext.get().client()
def in_activity() -> bool:
return ActivityContext.is_set()
def info() -> ActivityInfo:
return ActivityContext.get().info()
def heartbeat(*details: Any) -> None:
"""Send a heartbeat for the current activity."""
ActivityContext.get().heartbeat(*details)
def heartbeat_details(*types: Type) -> list[Any]:
"""Return heartbeat details from the previous attempt.
Pass type hints to decode the values into specific Python types:
step, total = activity.heartbeat_details(int, int)
Without type hints, returns raw JSON-decoded values.
"""
return ActivityContext.get().heartbeat_details(*types)
class ActivityContext(ABC):
_var: ContextVar["ActivityContext"] = ContextVar("activity")
@abstractmethod
def info(self) -> ActivityInfo: ...
@abstractmethod
def client(self) -> Client: ...
@abstractmethod
def heartbeat(self, *details: Any) -> None: ...
@abstractmethod
def heartbeat_details(self, *types: Type) -> list[Any]: ...
@contextmanager
def _activate(self) -> Iterator[None]:
token = ActivityContext._var.set(self)
yield None
ActivityContext._var.reset(token)
@staticmethod
def is_set() -> bool:
return ActivityContext._var.get(None) is not None
@staticmethod
def get() -> "ActivityContext":
return ActivityContext._var.get()
class ActivityDefinitionOptions(TypedDict, total=False):
name: str
T = TypeVar("T", contravariant=True)
P = ParamSpec("P")
R = TypeVar("R", covariant=True)
class ActivityDefinition(Protocol[P, R]):
@property
def name(self) -> str: ...
def with_options(self, **kwargs: Unpack[ActivityOptions]) -> Self: ...
async def execute(self, *args: P.args, **kwargs: P.kwargs) -> R: ...
class _SyncActivityDefinition(ActivityDefinition[P, R], Protocol):
def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ...
class _AsyncActivityDefinition(ActivityDefinition[P, R], Protocol):
async def __call__(self, *args: P.args, **kwargs: P.kwargs) -> R: ...
class _SyncActivityMethodDefinition(ActivityDefinition[P, R], Protocol[T, P, R]):
def __call__(self, original_self: T, *args: P.args, **kwargs: P.kwargs) -> R: ...
@overload
def __get__(
self, instance: None, owner: Type[T]
) -> "_SyncActivityMethodDefinition[T, P, R]": ...
@overload
def __get__(self, instance: T, owner: Type[T]) -> _SyncActivityDefinition[P, R]: ...
def __get__(
self, instance: T | None, owner: Type[T]
) -> _SyncActivityDefinition[P, R] | Self: ...
class _AsyncActivityMethodDefinition(ActivityDefinition[P, R], Protocol[T, P, R]):
async def __call__(
self, original_self: T, *args: P.args, **kwargs: P.kwargs
) -> R: ...
@overload
def __get__(
self, instance: None, owner: Type[T]
) -> "_AsyncActivityMethodDefinition[T, P, R]": ...
@overload
def __get__(
self, instance: T, owner: Type[T]
) -> _AsyncActivityDefinition[P, R]: ...
def __get__(
self, instance: T | None, owner: Type[T]
) -> _AsyncActivityDefinition[P, R] | Self: ...
_T1 = TypeVar("_T1", contravariant=True)
_P1 = ParamSpec("_P1")
_R1 = TypeVar("_R1")
_T2 = TypeVar("_T2", contravariant=True)
_P2 = ParamSpec("_P2")
_R2 = TypeVar("_R2")
class ActivityDecorator:
def __init__(
self,
options: ActivityDefinitionOptions,
callback_fn: Callable[[ActivityDefinition[Any, Any]], None] | None = None,
) -> None:
self._options = options
self._callback_fn = callback_fn
@overload
def __call__(
self, fn: Callable[P, Awaitable[R]]
) -> _AsyncActivityDefinition[P, R]: ...
@overload
def __call__(self, fn: Callable[P, R]) -> _SyncActivityDefinition[P, R]: ...
def __call__(
self, fn: Union[Callable[_P1, Awaitable[_R1]], Callable[_P2, _R2]]
) -> Union[_AsyncActivityDefinition[_P1, _R1], _SyncActivityDefinition[_P2, _R2]]:
name = self._options.get("name", fn.__qualname__)
if inspect.iscoroutinefunction(fn) or inspect.iscoroutinefunction(fn.__call__): # type: ignore
async_fn = cast(Callable[_P1, Awaitable[_R1]], fn)
async_def: _AsyncActivityDefinition[_P1, _R1] = AsyncImpl[_P1, _R1](
async_fn, name, FnSignature.of(async_fn)
)
if self._callback_fn is not None:
self._callback_fn(async_def)
return async_def
sync_fn = cast(Callable[_P2, _R2], fn)
sync_def: _SyncActivityDefinition[_P2, _R2] = SyncImpl[_P2, _R2](
sync_fn, name, FnSignature.of(fn)
)
if self._callback_fn is not None:
self._callback_fn(sync_def)
return sync_def
@overload
def defn(fn: Callable[P, Awaitable[R]]) -> _AsyncActivityDefinition[P, R]: ...
@overload
def defn(fn: Callable[P, R]) -> _SyncActivityDefinition[P, R]: ...
@overload
def defn(**kwargs: Unpack[ActivityDefinitionOptions]) -> ActivityDecorator: ...
def defn(
fn: Union[Callable[_P1, _R1], Callable[_P2, Awaitable[_R2]], None] = None,
**kwargs: Unpack[ActivityDefinitionOptions],
) -> Union[
ActivityDecorator,
_SyncActivityDefinition[_P1, _R1],
_AsyncActivityDefinition[_P2, _R2],
]:
options = ActivityDefinitionOptions(**kwargs)
if fn is not None:
return ActivityDecorator(options)(fn)
return ActivityDecorator(options)
class _ActivityMethodDecorator:
def __init__(
self,
options: ActivityDefinitionOptions,
callback_fn: Callable[[ActivityDefinition[Any, Any]], None] | None = None,
) -> None:
self._options = options
self._callback_fn = callback_fn
@overload
def __call__(
self, fn: Callable[Concatenate[T, P], Awaitable[R]]
) -> _AsyncActivityMethodDefinition[T, P, R]: ...
@overload
def __call__(
self, fn: Callable[Concatenate[T, P], R]
) -> _SyncActivityMethodDefinition[T, P, R]: ...
def __call__(
self,
fn: Union[
Callable[Concatenate[_T1, _P1], Awaitable[_R1]],
Callable[Concatenate[_T2, _P2], _R2],
],
) -> Union[
_AsyncActivityMethodDefinition[_T1, _P1, _R1],
_SyncActivityMethodDefinition[_T2, _P2, _R2],
]:
name = self._options.get("name", fn.__qualname__)
if inspect.iscoroutinefunction(fn) or inspect.iscoroutinefunction(fn.__call__): # type: ignore
async_fn = cast(Callable[Concatenate[_T1, _P1], Awaitable[_R1]], fn)
async_def: _AsyncActivityMethodDefinition[_T1, _P1, _R1] = AsyncMethodImpl[
_T1, _P1, _R1
](async_fn, name, FnSignature.of(async_fn))
if self._callback_fn is not None:
self._callback_fn(async_def)
return async_def
sync_fn = cast(Callable[Concatenate[_T2, _P2], _R2], fn)
sync_def: _SyncActivityMethodDefinition[_T2, _P2, _R2] = SyncMethodImpl[
_T2, _P2, _R2
](sync_fn, name, FnSignature.of(fn))
if self._callback_fn is not None:
self._callback_fn(sync_def)
return sync_def
@overload
def method(
fn: Callable[Concatenate[T, P], Awaitable[R]],
) -> _AsyncActivityMethodDefinition[T, P, R]: ...
@overload
def method(
fn: Callable[Concatenate[T, P], R],
) -> _SyncActivityMethodDefinition[T, P, R]: ...
@overload
def method(**kwargs: Unpack[ActivityDefinitionOptions]) -> _ActivityMethodDecorator: ...
def method(
fn: Union[
Callable[Concatenate[_T1, _P1], _R1],
Callable[Concatenate[_T2, _P2], Awaitable[_R2]],
None,
] = None,
**kwargs: Unpack[ActivityDefinitionOptions],
) -> Union[
_ActivityMethodDecorator,
_SyncActivityMethodDefinition[_T1, _P1, _R1],
_AsyncActivityMethodDefinition[_T2, _P2, _R2],
]:
options = ActivityDefinitionOptions(**kwargs)
if fn is not None:
return _ActivityMethodDecorator(options)(fn)
return _ActivityMethodDecorator(options)
class _OverrideDecorator(Generic[T, P, R]):
def __init__(self, definition: ActivityDefinition[P, R]):
self._definition = definition
@overload
def __call__(
self, fn: Callable[Concatenate[Any, P], Awaitable[R]]
) -> _AsyncActivityMethodDefinition[T, P, R]: ...
@overload
def __call__(
self, fn: Callable[Concatenate[Any, P], R]
) -> _SyncActivityMethodDefinition[T, P, R]: ...
def __call__(
self,
fn: Union[
Callable[Concatenate[Any, P], R],
Callable[Concatenate[Any, P], Awaitable[R]],
],
) -> Union[
_SyncActivityMethodDefinition[T, P, R], _AsyncActivityMethodDefinition[T, P, R]
]:
return self._definition.rebind(fn) # type: ignore
@overload
def override(
definition: _AsyncActivityMethodDefinition[T, P, R],
) -> _OverrideDecorator[T, P, R]: ...
@overload
def override(
definition: _SyncActivityMethodDefinition[T, P, R],
) -> _OverrideDecorator[T, P, R]: ...
def override(
definition: Union[
_AsyncActivityMethodDefinition[T, _P1, _R1],
_SyncActivityMethodDefinition[T, _P2, _R2],
],
) -> Union[_OverrideDecorator[T, _P1, _R1], _OverrideDecorator[T, _P2, _R2]]:
if inspect.iscoroutinefunction(definition.__call__): # type: ignore
async_def = cast(ActivityDefinition[_P1, _R1], definition)
return _OverrideDecorator[T, _P1, _R1](async_def)
else:
sync_def = cast(ActivityDefinition[_P2, _R2], definition)
return _OverrideDecorator[T, _P2, _R2](sync_def)