durable-workflow 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- durable_workflow-0.1.0/LICENSE +21 -0
- durable_workflow-0.1.0/PKG-INFO +130 -0
- durable_workflow-0.1.0/README.md +99 -0
- durable_workflow-0.1.0/pyproject.toml +73 -0
- durable_workflow-0.1.0/setup.cfg +4 -0
- durable_workflow-0.1.0/src/durable_workflow/__init__.py +75 -0
- durable_workflow-0.1.0/src/durable_workflow/activity.py +83 -0
- durable_workflow-0.1.0/src/durable_workflow/client.py +854 -0
- durable_workflow-0.1.0/src/durable_workflow/errors.py +142 -0
- durable_workflow-0.1.0/src/durable_workflow/py.typed +0 -0
- durable_workflow-0.1.0/src/durable_workflow/retry_policy.py +85 -0
- durable_workflow-0.1.0/src/durable_workflow/serializer.py +54 -0
- durable_workflow-0.1.0/src/durable_workflow/sync.py +337 -0
- durable_workflow-0.1.0/src/durable_workflow/worker.py +329 -0
- durable_workflow-0.1.0/src/durable_workflow/workflow.py +419 -0
- durable_workflow-0.1.0/src/durable_workflow.egg-info/PKG-INFO +130 -0
- durable_workflow-0.1.0/src/durable_workflow.egg-info/SOURCES.txt +27 -0
- durable_workflow-0.1.0/src/durable_workflow.egg-info/dependency_links.txt +1 -0
- durable_workflow-0.1.0/src/durable_workflow.egg-info/requires.txt +7 -0
- durable_workflow-0.1.0/src/durable_workflow.egg-info/top_level.txt +1 -0
- durable_workflow-0.1.0/tests/test_activity_context.py +246 -0
- durable_workflow-0.1.0/tests/test_client.py +350 -0
- durable_workflow-0.1.0/tests/test_errors.py +80 -0
- durable_workflow-0.1.0/tests/test_replay.py +817 -0
- durable_workflow-0.1.0/tests/test_retry_policy.py +151 -0
- durable_workflow-0.1.0/tests/test_schedules.py +457 -0
- durable_workflow-0.1.0/tests/test_serializer.py +75 -0
- durable_workflow-0.1.0/tests/test_sync.py +143 -0
- durable_workflow-0.1.0/tests/test_worker.py +392 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Durable Workflow
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: durable-workflow
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python SDK for the Durable Workflow server (language-neutral HTTP protocol)
|
|
5
|
+
Author: Durable Workflow Contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/durable-workflow/sdk-python
|
|
8
|
+
Project-URL: Documentation, https://durable-workflow.github.io/docs/2.0/sdks/python/
|
|
9
|
+
Project-URL: Repository, https://github.com/durable-workflow/sdk-python
|
|
10
|
+
Project-URL: Issues, https://github.com/zorporation/durable-workflow/issues
|
|
11
|
+
Keywords: workflow,durable,orchestration,temporal,saga
|
|
12
|
+
Classifier: Development Status :: 3 - Alpha
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Programming Language :: Python :: 3
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
19
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
20
|
+
Classifier: Typing :: Typed
|
|
21
|
+
Requires-Python: >=3.10
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: httpx>=0.27
|
|
25
|
+
Provides-Extra: dev
|
|
26
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
27
|
+
Requires-Dist: pytest-asyncio>=0.23; extra == "dev"
|
|
28
|
+
Requires-Dist: mypy>=1.10; extra == "dev"
|
|
29
|
+
Requires-Dist: ruff>=0.4; extra == "dev"
|
|
30
|
+
Dynamic: license-file
|
|
31
|
+
|
|
32
|
+
# durable-workflow (Python SDK)
|
|
33
|
+
|
|
34
|
+
A Python SDK for the [Durable Workflow server](https://github.com/durable-workflow/server). Speaks the server's language-neutral HTTP/JSON worker protocol — no PHP runtime required.
|
|
35
|
+
|
|
36
|
+
Status: **Alpha**. Supports starting workflows, registering workers, polling workflow + activity tasks, schedules, signals, and completing workflows. Queries, updates, timers, and child workflows are planned.
|
|
37
|
+
|
|
38
|
+
## Install
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install durable-workflow
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Or for development:
|
|
45
|
+
|
|
46
|
+
```bash
|
|
47
|
+
pip install -e '.[dev]'
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
## Quickstart
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
from durable_workflow import Client, Worker, workflow, activity
|
|
54
|
+
|
|
55
|
+
@activity.defn(name="greet")
|
|
56
|
+
def greet(name: str) -> str:
|
|
57
|
+
return f"hello, {name}"
|
|
58
|
+
|
|
59
|
+
@workflow.defn(name="greeter")
|
|
60
|
+
class GreeterWorkflow:
|
|
61
|
+
def run(self, ctx, name):
|
|
62
|
+
result = yield ctx.schedule_activity("greet", [name])
|
|
63
|
+
return result
|
|
64
|
+
|
|
65
|
+
async def main():
|
|
66
|
+
client = Client("http://server:8080", token="dev-token-123", namespace="default")
|
|
67
|
+
worker = Worker(
|
|
68
|
+
client,
|
|
69
|
+
task_queue="python-workers",
|
|
70
|
+
workflows=[GreeterWorkflow],
|
|
71
|
+
activities=[greet],
|
|
72
|
+
)
|
|
73
|
+
handle = await client.start_workflow(
|
|
74
|
+
workflow_type="greeter",
|
|
75
|
+
workflow_id="greet-1",
|
|
76
|
+
task_queue="python-workers",
|
|
77
|
+
input=["world"],
|
|
78
|
+
)
|
|
79
|
+
await worker.run_until(workflow_id="greet-1")
|
|
80
|
+
result = await client.get_result(handle)
|
|
81
|
+
print(result) # "hello, world"
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
## Features
|
|
85
|
+
|
|
86
|
+
- **Async-first**: Built on `httpx` and `asyncio`
|
|
87
|
+
- **Type-safe**: Full type hints, passes `mypy --strict`
|
|
88
|
+
- **Polyglot**: Works alongside PHP workers on the same task queue
|
|
89
|
+
- **HTTP/JSON protocol**: No gRPC, no protobuf dependencies
|
|
90
|
+
- **Codec envelopes**: Proper `{codec: "json", blob: "..."}` serialization for cross-language workflows
|
|
91
|
+
|
|
92
|
+
## Documentation
|
|
93
|
+
|
|
94
|
+
Full documentation is available at [durable-workflow.github.io/docs/2.0/sdks/python/](https://durable-workflow.github.io/docs/2.0/sdks/python/):
|
|
95
|
+
|
|
96
|
+
- [Quickstart](https://durable-workflow.github.io/docs/2.0/sdks/python/quickstart)
|
|
97
|
+
- [Client API](https://durable-workflow.github.io/docs/2.0/sdks/python/client)
|
|
98
|
+
- [Workflow Authoring](https://durable-workflow.github.io/docs/2.0/sdks/python/workflows)
|
|
99
|
+
- [Activity Authoring](https://durable-workflow.github.io/docs/2.0/sdks/python/activities)
|
|
100
|
+
- [Worker Configuration](https://durable-workflow.github.io/docs/2.0/sdks/python/workers)
|
|
101
|
+
- [Error Handling](https://durable-workflow.github.io/docs/2.0/sdks/python/errors)
|
|
102
|
+
- [Schedules (Cron)](https://durable-workflow.github.io/docs/2.0/sdks/python/schedules)
|
|
103
|
+
|
|
104
|
+
## Requirements
|
|
105
|
+
|
|
106
|
+
- Python ≥ 3.10
|
|
107
|
+
- A running [Durable Workflow server](https://github.com/durable-workflow/server)
|
|
108
|
+
|
|
109
|
+
## Development
|
|
110
|
+
|
|
111
|
+
```bash
|
|
112
|
+
# Install dev dependencies
|
|
113
|
+
pip install -e '.[dev]'
|
|
114
|
+
|
|
115
|
+
# Run tests
|
|
116
|
+
pytest
|
|
117
|
+
|
|
118
|
+
# Run integration tests (requires Docker)
|
|
119
|
+
pytest -m integration
|
|
120
|
+
|
|
121
|
+
# Type check
|
|
122
|
+
mypy src/durable_workflow/
|
|
123
|
+
|
|
124
|
+
# Lint
|
|
125
|
+
ruff check src/ tests/
|
|
126
|
+
```
|
|
127
|
+
|
|
128
|
+
## License
|
|
129
|
+
|
|
130
|
+
MIT
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
# durable-workflow (Python SDK)
|
|
2
|
+
|
|
3
|
+
A Python SDK for the [Durable Workflow server](https://github.com/durable-workflow/server). Speaks the server's language-neutral HTTP/JSON worker protocol — no PHP runtime required.
|
|
4
|
+
|
|
5
|
+
Status: **Alpha**. Supports starting workflows, registering workers, polling workflow + activity tasks, schedules, signals, and completing workflows. Queries, updates, timers, and child workflows are planned.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pip install durable-workflow
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Or for development:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
pip install -e '.[dev]'
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
## Quickstart
|
|
20
|
+
|
|
21
|
+
```python
|
|
22
|
+
from durable_workflow import Client, Worker, workflow, activity
|
|
23
|
+
|
|
24
|
+
@activity.defn(name="greet")
|
|
25
|
+
def greet(name: str) -> str:
|
|
26
|
+
return f"hello, {name}"
|
|
27
|
+
|
|
28
|
+
@workflow.defn(name="greeter")
|
|
29
|
+
class GreeterWorkflow:
|
|
30
|
+
def run(self, ctx, name):
|
|
31
|
+
result = yield ctx.schedule_activity("greet", [name])
|
|
32
|
+
return result
|
|
33
|
+
|
|
34
|
+
async def main():
|
|
35
|
+
client = Client("http://server:8080", token="dev-token-123", namespace="default")
|
|
36
|
+
worker = Worker(
|
|
37
|
+
client,
|
|
38
|
+
task_queue="python-workers",
|
|
39
|
+
workflows=[GreeterWorkflow],
|
|
40
|
+
activities=[greet],
|
|
41
|
+
)
|
|
42
|
+
handle = await client.start_workflow(
|
|
43
|
+
workflow_type="greeter",
|
|
44
|
+
workflow_id="greet-1",
|
|
45
|
+
task_queue="python-workers",
|
|
46
|
+
input=["world"],
|
|
47
|
+
)
|
|
48
|
+
await worker.run_until(workflow_id="greet-1")
|
|
49
|
+
result = await client.get_result(handle)
|
|
50
|
+
print(result) # "hello, world"
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Features
|
|
54
|
+
|
|
55
|
+
- **Async-first**: Built on `httpx` and `asyncio`
|
|
56
|
+
- **Type-safe**: Full type hints, passes `mypy --strict`
|
|
57
|
+
- **Polyglot**: Works alongside PHP workers on the same task queue
|
|
58
|
+
- **HTTP/JSON protocol**: No gRPC, no protobuf dependencies
|
|
59
|
+
- **Codec envelopes**: Proper `{codec: "json", blob: "..."}` serialization for cross-language workflows
|
|
60
|
+
|
|
61
|
+
## Documentation
|
|
62
|
+
|
|
63
|
+
Full documentation is available at [durable-workflow.github.io/docs/2.0/sdks/python/](https://durable-workflow.github.io/docs/2.0/sdks/python/):
|
|
64
|
+
|
|
65
|
+
- [Quickstart](https://durable-workflow.github.io/docs/2.0/sdks/python/quickstart)
|
|
66
|
+
- [Client API](https://durable-workflow.github.io/docs/2.0/sdks/python/client)
|
|
67
|
+
- [Workflow Authoring](https://durable-workflow.github.io/docs/2.0/sdks/python/workflows)
|
|
68
|
+
- [Activity Authoring](https://durable-workflow.github.io/docs/2.0/sdks/python/activities)
|
|
69
|
+
- [Worker Configuration](https://durable-workflow.github.io/docs/2.0/sdks/python/workers)
|
|
70
|
+
- [Error Handling](https://durable-workflow.github.io/docs/2.0/sdks/python/errors)
|
|
71
|
+
- [Schedules (Cron)](https://durable-workflow.github.io/docs/2.0/sdks/python/schedules)
|
|
72
|
+
|
|
73
|
+
## Requirements
|
|
74
|
+
|
|
75
|
+
- Python ≥ 3.10
|
|
76
|
+
- A running [Durable Workflow server](https://github.com/durable-workflow/server)
|
|
77
|
+
|
|
78
|
+
## Development
|
|
79
|
+
|
|
80
|
+
```bash
|
|
81
|
+
# Install dev dependencies
|
|
82
|
+
pip install -e '.[dev]'
|
|
83
|
+
|
|
84
|
+
# Run tests
|
|
85
|
+
pytest
|
|
86
|
+
|
|
87
|
+
# Run integration tests (requires Docker)
|
|
88
|
+
pytest -m integration
|
|
89
|
+
|
|
90
|
+
# Type check
|
|
91
|
+
mypy src/durable_workflow/
|
|
92
|
+
|
|
93
|
+
# Lint
|
|
94
|
+
ruff check src/ tests/
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
MIT
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "durable-workflow"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Python SDK for the Durable Workflow server (language-neutral HTTP protocol)"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.10"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Durable Workflow Contributors" },
|
|
14
|
+
]
|
|
15
|
+
keywords = [
|
|
16
|
+
"workflow",
|
|
17
|
+
"durable",
|
|
18
|
+
"orchestration",
|
|
19
|
+
"temporal",
|
|
20
|
+
"saga",
|
|
21
|
+
]
|
|
22
|
+
classifiers = [
|
|
23
|
+
"Development Status :: 3 - Alpha",
|
|
24
|
+
"Intended Audience :: Developers",
|
|
25
|
+
"License :: OSI Approved :: MIT License",
|
|
26
|
+
"Programming Language :: Python :: 3",
|
|
27
|
+
"Programming Language :: Python :: 3.10",
|
|
28
|
+
"Programming Language :: Python :: 3.11",
|
|
29
|
+
"Programming Language :: Python :: 3.12",
|
|
30
|
+
"Topic :: Software Development :: Libraries :: Python Modules",
|
|
31
|
+
"Typing :: Typed",
|
|
32
|
+
]
|
|
33
|
+
dependencies = [
|
|
34
|
+
"httpx>=0.27",
|
|
35
|
+
]
|
|
36
|
+
|
|
37
|
+
[project.optional-dependencies]
|
|
38
|
+
dev = [
|
|
39
|
+
"pytest>=8.0",
|
|
40
|
+
"pytest-asyncio>=0.23",
|
|
41
|
+
"mypy>=1.10",
|
|
42
|
+
"ruff>=0.4",
|
|
43
|
+
]
|
|
44
|
+
|
|
45
|
+
[project.urls]
|
|
46
|
+
Homepage = "https://github.com/durable-workflow/sdk-python"
|
|
47
|
+
Documentation = "https://durable-workflow.github.io/docs/2.0/sdks/python/"
|
|
48
|
+
Repository = "https://github.com/durable-workflow/sdk-python"
|
|
49
|
+
Issues = "https://github.com/zorporation/durable-workflow/issues"
|
|
50
|
+
|
|
51
|
+
[tool.setuptools.packages.find]
|
|
52
|
+
where = ["src"]
|
|
53
|
+
|
|
54
|
+
[tool.pytest.ini_options]
|
|
55
|
+
testpaths = ["tests"]
|
|
56
|
+
asyncio_mode = "auto"
|
|
57
|
+
markers = [
|
|
58
|
+
"integration: marks tests that require a running server (deselect with '-m \"not integration\"')",
|
|
59
|
+
]
|
|
60
|
+
|
|
61
|
+
[tool.mypy]
|
|
62
|
+
python_version = "3.10"
|
|
63
|
+
strict = true
|
|
64
|
+
packages = ["durable_workflow"]
|
|
65
|
+
mypy_path = "src"
|
|
66
|
+
|
|
67
|
+
[tool.ruff]
|
|
68
|
+
target-version = "py310"
|
|
69
|
+
line-length = 120
|
|
70
|
+
src = ["src"]
|
|
71
|
+
|
|
72
|
+
[tool.ruff.lint]
|
|
73
|
+
select = ["E", "F", "I", "UP", "B", "SIM"]
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
from . import activity, sync, workflow
|
|
2
|
+
from .activity import ActivityContext, ActivityInfo
|
|
3
|
+
from .client import (
|
|
4
|
+
Client,
|
|
5
|
+
ScheduleAction,
|
|
6
|
+
ScheduleBackfillResult,
|
|
7
|
+
ScheduleDescription,
|
|
8
|
+
ScheduleHandle,
|
|
9
|
+
ScheduleList,
|
|
10
|
+
ScheduleSpec,
|
|
11
|
+
ScheduleTriggerResult,
|
|
12
|
+
WorkflowExecution,
|
|
13
|
+
WorkflowHandle,
|
|
14
|
+
WorkflowList,
|
|
15
|
+
)
|
|
16
|
+
from .errors import (
|
|
17
|
+
ActivityCancelled,
|
|
18
|
+
ChildWorkflowFailed,
|
|
19
|
+
DurableWorkflowError,
|
|
20
|
+
InvalidArgument,
|
|
21
|
+
NamespaceNotFound,
|
|
22
|
+
NonRetryableError,
|
|
23
|
+
QueryFailed,
|
|
24
|
+
ScheduleAlreadyExists,
|
|
25
|
+
ScheduleNotFound,
|
|
26
|
+
ServerError,
|
|
27
|
+
Unauthorized,
|
|
28
|
+
UpdateRejected,
|
|
29
|
+
WorkflowAlreadyStarted,
|
|
30
|
+
WorkflowCancelled,
|
|
31
|
+
WorkflowFailed,
|
|
32
|
+
WorkflowNotFound,
|
|
33
|
+
WorkflowTerminated,
|
|
34
|
+
)
|
|
35
|
+
from .worker import Worker
|
|
36
|
+
from .workflow import ContinueAsNew, StartChildWorkflow
|
|
37
|
+
|
|
38
|
+
__all__ = [
|
|
39
|
+
"ActivityCancelled",
|
|
40
|
+
"ActivityContext",
|
|
41
|
+
"ActivityInfo",
|
|
42
|
+
"ChildWorkflowFailed",
|
|
43
|
+
"Client",
|
|
44
|
+
"ContinueAsNew",
|
|
45
|
+
"NonRetryableError",
|
|
46
|
+
"ScheduleAction",
|
|
47
|
+
"ScheduleAlreadyExists",
|
|
48
|
+
"ScheduleBackfillResult",
|
|
49
|
+
"ScheduleDescription",
|
|
50
|
+
"ScheduleHandle",
|
|
51
|
+
"ScheduleList",
|
|
52
|
+
"ScheduleNotFound",
|
|
53
|
+
"ScheduleSpec",
|
|
54
|
+
"ScheduleTriggerResult",
|
|
55
|
+
"StartChildWorkflow",
|
|
56
|
+
"Worker",
|
|
57
|
+
"WorkflowExecution",
|
|
58
|
+
"WorkflowHandle",
|
|
59
|
+
"WorkflowList",
|
|
60
|
+
"activity",
|
|
61
|
+
"sync",
|
|
62
|
+
"workflow",
|
|
63
|
+
"DurableWorkflowError",
|
|
64
|
+
"InvalidArgument",
|
|
65
|
+
"NamespaceNotFound",
|
|
66
|
+
"QueryFailed",
|
|
67
|
+
"ServerError",
|
|
68
|
+
"Unauthorized",
|
|
69
|
+
"UpdateRejected",
|
|
70
|
+
"WorkflowAlreadyStarted",
|
|
71
|
+
"WorkflowCancelled",
|
|
72
|
+
"WorkflowFailed",
|
|
73
|
+
"WorkflowNotFound",
|
|
74
|
+
"WorkflowTerminated",
|
|
75
|
+
]
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"""Activity decorator, registry, and execution context."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import contextvars
|
|
5
|
+
from collections.abc import Callable
|
|
6
|
+
from dataclasses import dataclass
|
|
7
|
+
from typing import TYPE_CHECKING, Any
|
|
8
|
+
|
|
9
|
+
from .errors import ActivityCancelled
|
|
10
|
+
|
|
11
|
+
if TYPE_CHECKING:
|
|
12
|
+
from .client import Client
|
|
13
|
+
|
|
14
|
+
_REGISTRY: dict[str, Callable[..., Any]] = {}
|
|
15
|
+
|
|
16
|
+
_current_context: contextvars.ContextVar[ActivityContext | None] = contextvars.ContextVar(
|
|
17
|
+
"activity_context", default=None
|
|
18
|
+
)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
@dataclass(frozen=True)
|
|
22
|
+
class ActivityInfo:
|
|
23
|
+
task_id: str
|
|
24
|
+
activity_type: str
|
|
25
|
+
activity_attempt_id: str
|
|
26
|
+
attempt_number: int
|
|
27
|
+
task_queue: str
|
|
28
|
+
worker_id: str
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class ActivityContext:
|
|
32
|
+
def __init__(
|
|
33
|
+
self,
|
|
34
|
+
*,
|
|
35
|
+
info: ActivityInfo,
|
|
36
|
+
client: Client,
|
|
37
|
+
) -> None:
|
|
38
|
+
self._info = info
|
|
39
|
+
self._client = client
|
|
40
|
+
self._cancel_requested = False
|
|
41
|
+
|
|
42
|
+
@property
|
|
43
|
+
def info(self) -> ActivityInfo:
|
|
44
|
+
return self._info
|
|
45
|
+
|
|
46
|
+
@property
|
|
47
|
+
def is_cancelled(self) -> bool:
|
|
48
|
+
return self._cancel_requested
|
|
49
|
+
|
|
50
|
+
async def heartbeat(self, details: dict[str, Any] | None = None) -> None:
|
|
51
|
+
resp = await self._client.heartbeat_activity_task(
|
|
52
|
+
task_id=self._info.task_id,
|
|
53
|
+
activity_attempt_id=self._info.activity_attempt_id,
|
|
54
|
+
lease_owner=self._info.worker_id,
|
|
55
|
+
details=details,
|
|
56
|
+
)
|
|
57
|
+
if isinstance(resp, dict) and resp.get("cancel_requested"):
|
|
58
|
+
self._cancel_requested = True
|
|
59
|
+
raise ActivityCancelled()
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def context() -> ActivityContext:
|
|
63
|
+
ctx = _current_context.get()
|
|
64
|
+
if ctx is None:
|
|
65
|
+
raise RuntimeError("activity.context() called outside of an activity execution")
|
|
66
|
+
return ctx
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
def _set_context(ctx: ActivityContext | None) -> contextvars.Token[ActivityContext | None]:
|
|
70
|
+
return _current_context.set(ctx)
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def defn(*, name: str) -> Callable[[Callable[..., Any]], Callable[..., Any]]:
|
|
74
|
+
def wrap(fn: Callable[..., Any]) -> Callable[..., Any]:
|
|
75
|
+
fn.__activity_name__ = name # type: ignore[attr-defined]
|
|
76
|
+
_REGISTRY[name] = fn
|
|
77
|
+
return fn
|
|
78
|
+
|
|
79
|
+
return wrap
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def registry() -> dict[str, Callable[..., Any]]:
|
|
83
|
+
return dict(_REGISTRY)
|