duroxide 0.1.27__tar.gz → 0.2.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.
- {duroxide-0.1.27 → duroxide-0.2.0}/.agents/skills/duroxide-python-orchestrations/SKILL.md +290 -290
- {duroxide-0.1.27 → duroxide-0.2.0}/.agents/skills/pyo3-interop/SKILL.md +268 -268
- duroxide-0.2.0/.agents/skills/release-preparation/SKILL.md +61 -0
- {duroxide-0.1.27 → duroxide-0.2.0}/.env.example +2 -2
- {duroxide-0.1.27 → duroxide-0.2.0}/.github/copilot-instructions.md +222 -220
- duroxide-0.2.0/.github/dependabot.yml +11 -0
- duroxide-0.2.0/.github/workflows/build.yml +197 -0
- {duroxide-0.1.27 → duroxide-0.2.0}/.gitignore +43 -43
- {duroxide-0.1.27 → duroxide-0.2.0}/CHANGELOG.md +306 -242
- {duroxide-0.1.27 → duroxide-0.2.0}/CODE_OF_CONDUCT.md +9 -9
- {duroxide-0.1.27 → duroxide-0.2.0}/CONTRIBUTING.md +31 -31
- {duroxide-0.1.27 → duroxide-0.2.0}/Cargo.lock +205 -198
- {duroxide-0.1.27 → duroxide-0.2.0}/Cargo.toml +3 -3
- {duroxide-0.1.27 → duroxide-0.2.0}/LICENSE +20 -20
- {duroxide-0.1.27 → duroxide-0.2.0}/PKG-INFO +328 -321
- {duroxide-0.1.27 → duroxide-0.2.0}/README.md +326 -320
- duroxide-0.2.0/RELEASE_POLICY.md +28 -0
- {duroxide-0.1.27 → duroxide-0.2.0}/SECURITY.md +13 -13
- {duroxide-0.1.27 → duroxide-0.2.0}/ci/smoke/README.md +16 -16
- {duroxide-0.1.27 → duroxide-0.2.0}/ci/smoke/run-local.ps1 +32 -32
- {duroxide-0.1.27 → duroxide-0.2.0}/ci/smoke/run-local.sh +33 -33
- {duroxide-0.1.27 → duroxide-0.2.0}/ci/smoke/run-registry.ps1 +38 -38
- {duroxide-0.1.27 → duroxide-0.2.0}/ci/smoke/run-registry.sh +43 -43
- {duroxide-0.1.27 → duroxide-0.2.0}/ci/smoke/smoke.py +69 -69
- {duroxide-0.1.27 → duroxide-0.2.0}/docs/architecture.md +465 -465
- {duroxide-0.1.27 → duroxide-0.2.0}/docs/user-guide.md +826 -789
- {duroxide-0.1.27 → duroxide-0.2.0}/pyproject.toml +33 -32
- {duroxide-0.1.27 → duroxide-0.2.0}/python/duroxide/__init__.py +643 -643
- {duroxide-0.1.27 → duroxide-0.2.0}/python/duroxide/context.py +581 -570
- {duroxide-0.1.27 → duroxide-0.2.0}/python/duroxide/driver.py +212 -212
- {duroxide-0.1.27 → duroxide-0.2.0}/src/client.rs +700 -700
- {duroxide-0.1.27 → duroxide-0.2.0}/src/handlers.rs +1024 -1052
- {duroxide-0.1.27 → duroxide-0.2.0}/src/lib.rs +213 -215
- {duroxide-0.1.27 → duroxide-0.2.0}/src/pg_provider.rs +166 -166
- {duroxide-0.1.27 → duroxide-0.2.0}/src/provider.rs +74 -74
- {duroxide-0.1.27 → duroxide-0.2.0}/src/runtime.rs +422 -362
- {duroxide-0.1.27 → duroxide-0.2.0}/src/types.rs +317 -317
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/scenarios/__init__.py +2 -2
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/scenarios/test_toygres.py +852 -852
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_admin_api.py +554 -554
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_advanced_features.py +546 -546
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_async_blocks.py +555 -555
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_copilot_chat.py +116 -116
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_e2e.py +1273 -1273
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_init_tracing.py +41 -41
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_kv_store.py +218 -218
- duroxide-0.2.0/tests/test_lock_options.py +116 -0
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_races.py +557 -393
- duroxide-0.2.0/tests/test_replay_isolation.py +198 -0
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_sessions.py +421 -421
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_tags.py +523 -523
- {duroxide-0.1.27 → duroxide-0.2.0}/tests/test_typed_apis.py +366 -366
- duroxide-0.1.27/.agents/skills/pypi-publish/SKILL.md +0 -208
- duroxide-0.1.27/.github/workflows/publish.yml +0 -256
|
@@ -1,290 +1,290 @@
|
|
|
1
|
-
---
|
|
2
|
-
name: duroxide-python-orchestrations
|
|
3
|
-
description: Writing durable workflows in Python using duroxide-python. Use when creating orchestrations, activities, writing tests, or when the user mentions generator workflows, yield patterns, or duroxide-python development.
|
|
4
|
-
---
|
|
5
|
-
|
|
6
|
-
# Duroxide-Python Orchestration Development
|
|
7
|
-
|
|
8
|
-
## Core Rule: Yield vs Regular Functions
|
|
9
|
-
|
|
10
|
-
| Context | Syntax | Why |
|
|
11
|
-
|---------|--------|-----|
|
|
12
|
-
| Orchestrations | `def` + `yield` (generator) | Rust replay engine needs step-by-step control |
|
|
13
|
-
| Activities | `def` (regular function) | Run once, result cached — no replay constraints |
|
|
14
|
-
| Orchestration tracing | Direct call (no yield) | Fire-and-forget, delegates to Rust |
|
|
15
|
-
|
|
16
|
-
```python
|
|
17
|
-
# ✅ Orchestration: generator, yield for durable operations
|
|
18
|
-
@runtime.register_orchestration("MyWorkflow")
|
|
19
|
-
def my_workflow(ctx, input):
|
|
20
|
-
ctx.trace_info("starting") # no yield
|
|
21
|
-
result = yield ctx.schedule_activity("Work", input) # yield
|
|
22
|
-
return result
|
|
23
|
-
|
|
24
|
-
# ✅ Activity: regular function for I/O
|
|
25
|
-
@runtime.register_activity("Work")
|
|
26
|
-
def work(ctx, input):
|
|
27
|
-
ctx.trace_info(f"processing {input}") # no yield
|
|
28
|
-
data = requests.get(input["url"]).json() # sync I/O
|
|
29
|
-
return data
|
|
30
|
-
```
|
|
31
|
-
|
|
32
|
-
**Never use `async def` with `yield` for orchestrations** — async generators break the replay model.
|
|
33
|
-
|
|
34
|
-
## Orchestration Context API
|
|
35
|
-
|
|
36
|
-
### Scheduling (MUST yield)
|
|
37
|
-
|
|
38
|
-
```python
|
|
39
|
-
def my_orch(ctx, input):
|
|
40
|
-
# Activity
|
|
41
|
-
result = yield ctx.schedule_activity("Name", input)
|
|
42
|
-
|
|
43
|
-
# Activity with retry
|
|
44
|
-
result = yield ctx.schedule_activity_with_retry("Name", input, {
|
|
45
|
-
"max_attempts": 3,
|
|
46
|
-
"backoff": "exponential",
|
|
47
|
-
"timeout_ms": 5000,
|
|
48
|
-
"total_timeout_ms": 30000,
|
|
49
|
-
})
|
|
50
|
-
|
|
51
|
-
# Timer (durable delay)
|
|
52
|
-
yield ctx.schedule_timer(60000) # 1 minute
|
|
53
|
-
|
|
54
|
-
# External event
|
|
55
|
-
event_data = yield ctx.wait_for_event("approval")
|
|
56
|
-
|
|
57
|
-
# Sub-orchestration (waits for completion)
|
|
58
|
-
child_result = yield ctx.schedule_sub_orchestration("Child", child_input)
|
|
59
|
-
|
|
60
|
-
# Sub-orchestration with explicit ID
|
|
61
|
-
child_result = yield ctx.schedule_sub_orchestration_with_id("Child", "child-1", child_input)
|
|
62
|
-
|
|
63
|
-
# Fire-and-forget orchestration (returns immediately)
|
|
64
|
-
yield ctx.start_orchestration("BackgroundWork", "bg-1", bg_input)
|
|
65
|
-
|
|
66
|
-
# Deterministic values
|
|
67
|
-
now = yield ctx.utc_now() # timestamp in ms
|
|
68
|
-
guid = yield ctx.new_guid() # deterministic UUID
|
|
69
|
-
|
|
70
|
-
# Continue as new (restart with fresh history)
|
|
71
|
-
yield ctx.continue_as_new(new_input)
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
### Composition (MUST yield)
|
|
75
|
-
|
|
76
|
-
```python
|
|
77
|
-
# Fan-out / fan-in — wait for ALL tasks (supports all task types)
|
|
78
|
-
results = yield ctx.all([
|
|
79
|
-
ctx.schedule_activity("TaskA", input_a),
|
|
80
|
-
ctx.schedule_activity("TaskB", input_b),
|
|
81
|
-
ctx.schedule_timer(5000), # timers work too
|
|
82
|
-
ctx.wait_for_event("approval"), # waits work too
|
|
83
|
-
])
|
|
84
|
-
# results = [result_a, result_b, {"ok": None}, {"ok": event_data}]
|
|
85
|
-
|
|
86
|
-
# Race — wait for FIRST of 2 tasks (supports all task types)
|
|
87
|
-
winner = yield ctx.race(
|
|
88
|
-
ctx.schedule_activity("FastService", input),
|
|
89
|
-
ctx.schedule_timer(5000),
|
|
90
|
-
)
|
|
91
|
-
# winner = {"index": 0|1, "value": ...}
|
|
92
|
-
```
|
|
93
|
-
|
|
94
|
-
**`ctx.race()` supports exactly 2 tasks** (maps to Rust `select2`). Nesting `all()`/`race()` inside `all()` or `race()` is **not supported** — the runtime rejects it.
|
|
95
|
-
|
|
96
|
-
### Cooperative Activity Cancellation
|
|
97
|
-
|
|
98
|
-
```python
|
|
99
|
-
@runtime.register_activity("LongTask")
|
|
100
|
-
def long_task(ctx, input):
|
|
101
|
-
for i in range(1000):
|
|
102
|
-
if ctx.is_cancelled():
|
|
103
|
-
ctx.trace_info("cancelled, cleaning up")
|
|
104
|
-
return {"status": "cancelled"}
|
|
105
|
-
time.sleep(0.1) # do work
|
|
106
|
-
return {"status": "done"}
|
|
107
|
-
```
|
|
108
|
-
|
|
109
|
-
`ctx.is_cancelled()` checks whether the orchestration no longer needs the activity result (e.g., lost a race). Detection latency is `worker_lock_timeout_ms / 2` (default 30s → ~15s).
|
|
110
|
-
|
|
111
|
-
### Tracing (NO yield — fire-and-forget)
|
|
112
|
-
|
|
113
|
-
```python
|
|
114
|
-
ctx.trace_info("message") # suppressed during replay automatically
|
|
115
|
-
ctx.trace_warn("message")
|
|
116
|
-
ctx.trace_error("message")
|
|
117
|
-
ctx.trace_debug("message")
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
Tracing delegates to the Rust `OrchestrationContext` which has the `is_replaying` guard. **Do not use `print()`** in orchestrations — it will duplicate on replay.
|
|
121
|
-
|
|
122
|
-
## Activity Context API
|
|
123
|
-
|
|
124
|
-
```python
|
|
125
|
-
@runtime.register_activity("MyActivity")
|
|
126
|
-
def my_activity(ctx, input):
|
|
127
|
-
# Available fields
|
|
128
|
-
ctx.instance_id
|
|
129
|
-
ctx.execution_id
|
|
130
|
-
ctx.orchestration_name
|
|
131
|
-
ctx.orchestration_version
|
|
132
|
-
ctx.activity_name
|
|
133
|
-
ctx.worker_id
|
|
134
|
-
|
|
135
|
-
# Cooperative cancellation
|
|
136
|
-
if ctx.is_cancelled():
|
|
137
|
-
ctx.trace_info("cancelled")
|
|
138
|
-
return {"status": "cancelled"}
|
|
139
|
-
|
|
140
|
-
# Tracing (delegates to Rust ActivityContext — full structured fields)
|
|
141
|
-
ctx.trace_info(f"processing {input['id']}")
|
|
142
|
-
ctx.trace_warn("slow response")
|
|
143
|
-
ctx.trace_error("connection failed")
|
|
144
|
-
ctx.trace_debug("raw payload: ...")
|
|
145
|
-
|
|
146
|
-
# Activities can do anything — I/O, HTTP, DB, etc.
|
|
147
|
-
data = requests.get(input["url"]).json()
|
|
148
|
-
return data
|
|
149
|
-
```
|
|
150
|
-
|
|
151
|
-
## Determinism Rules
|
|
152
|
-
|
|
153
|
-
Orchestrations **must be deterministic** — the replay engine re-executes from the beginning on every dispatch:
|
|
154
|
-
|
|
155
|
-
| ✅ Safe | ❌ Breaks Replay |
|
|
156
|
-
|---------|-----------------|
|
|
157
|
-
| `yield ctx.utc_now()` | `time.time()` |
|
|
158
|
-
| `yield ctx.new_guid()` | `uuid.uuid4()` |
|
|
159
|
-
| `ctx.trace_info()` | `print()` (duplicates) |
|
|
160
|
-
| `yield ctx.schedule_timer(ms)` | `time.sleep()` |
|
|
161
|
-
| Pure computation, conditionals | `requests.get()`, file I/O, DB queries |
|
|
162
|
-
| `json.loads()`, `json.dumps()` | `os.environ["X"]` (may change) |
|
|
163
|
-
|
|
164
|
-
**All I/O belongs in activities**, not orchestrations.
|
|
165
|
-
|
|
166
|
-
## Common Patterns
|
|
167
|
-
|
|
168
|
-
### Error Handling
|
|
169
|
-
|
|
170
|
-
```python
|
|
171
|
-
def my_orch(ctx, input):
|
|
172
|
-
try:
|
|
173
|
-
result = yield ctx.schedule_activity("RiskyOp", input)
|
|
174
|
-
return result
|
|
175
|
-
except Exception as e:
|
|
176
|
-
ctx.trace_error(f"failed: {e}")
|
|
177
|
-
yield ctx.schedule_activity("Cleanup", {"error": str(e)})
|
|
178
|
-
return {"status": "failed"}
|
|
179
|
-
```
|
|
180
|
-
|
|
181
|
-
### Eternal Orchestration (continue-as-new)
|
|
182
|
-
|
|
183
|
-
```python
|
|
184
|
-
def monitor(ctx, input):
|
|
185
|
-
state = input.get("state", {"iteration": 0})
|
|
186
|
-
health = yield ctx.schedule_activity("CheckHealth", input["target"])
|
|
187
|
-
ctx.trace_info(f"check #{state['iteration']}: {health['status']}")
|
|
188
|
-
yield ctx.schedule_timer(30000)
|
|
189
|
-
yield ctx.continue_as_new({
|
|
190
|
-
"target": input["target"],
|
|
191
|
-
"state": {"iteration": state["iteration"] + 1},
|
|
192
|
-
})
|
|
193
|
-
```
|
|
194
|
-
|
|
195
|
-
### Race with Timeout
|
|
196
|
-
|
|
197
|
-
```python
|
|
198
|
-
def my_orch(ctx, input):
|
|
199
|
-
winner = yield ctx.race(
|
|
200
|
-
ctx.schedule_activity("SlowOperation", input),
|
|
201
|
-
ctx.schedule_timer(10000),
|
|
202
|
-
)
|
|
203
|
-
if winner["index"] == 1:
|
|
204
|
-
ctx.trace_warn("operation timed out")
|
|
205
|
-
return {"status": "timeout"}
|
|
206
|
-
return {"status": "ok", "result": winner["value"]}
|
|
207
|
-
```
|
|
208
|
-
|
|
209
|
-
### Versioned Orchestrations
|
|
210
|
-
|
|
211
|
-
```python
|
|
212
|
-
@runtime.register_orchestration("MyWorkflow")
|
|
213
|
-
def my_workflow_v1(ctx, input):
|
|
214
|
-
ctx.trace_info("[v1.0.0] starting")
|
|
215
|
-
return (yield ctx.schedule_activity("Work", input))
|
|
216
|
-
|
|
217
|
-
@runtime.register_orchestration_versioned("MyWorkflow", "1.0.1")
|
|
218
|
-
def my_workflow_v2(ctx, input):
|
|
219
|
-
ctx.trace_info("[v1.0.1] starting")
|
|
220
|
-
yield ctx.schedule_activity("Validate", input)
|
|
221
|
-
return (yield ctx.schedule_activity("Work", input))
|
|
222
|
-
```
|
|
223
|
-
|
|
224
|
-
## Writing Tests
|
|
225
|
-
|
|
226
|
-
Tests use pytest:
|
|
227
|
-
|
|
228
|
-
```python
|
|
229
|
-
import time, pytest
|
|
230
|
-
from duroxide import SqliteProvider, PostgresProvider, Client, Runtime, PyRuntimeOptions
|
|
231
|
-
|
|
232
|
-
@pytest.fixture(scope="module")
|
|
233
|
-
def provider():
|
|
234
|
-
db_url = os.environ.get("DATABASE_URL")
|
|
235
|
-
if not db_url:
|
|
236
|
-
pytest.skip("DATABASE_URL not set")
|
|
237
|
-
return PostgresProvider.connect_with_schema(db_url, "my_test_schema")
|
|
238
|
-
|
|
239
|
-
def test_my_feature(provider):
|
|
240
|
-
client = Client(provider)
|
|
241
|
-
runtime = Runtime(provider, PyRuntimeOptions(dispatcher_poll_interval_ms=50))
|
|
242
|
-
|
|
243
|
-
runtime.register_activity("Echo", lambda ctx, inp: inp)
|
|
244
|
-
|
|
245
|
-
@runtime.register_orchestration("MyWorkflow")
|
|
246
|
-
def my_workflow(ctx, input):
|
|
247
|
-
return (yield ctx.schedule_activity("Echo", input))
|
|
248
|
-
|
|
249
|
-
runtime.start()
|
|
250
|
-
try:
|
|
251
|
-
client.start_orchestration("test-1", "MyWorkflow", "hello")
|
|
252
|
-
result = client.wait_for_orchestration("test-1", 10_000)
|
|
253
|
-
assert result.status == "Completed"
|
|
254
|
-
assert result.output == "hello"
|
|
255
|
-
finally:
|
|
256
|
-
runtime.shutdown(100)
|
|
257
|
-
```
|
|
258
|
-
|
|
259
|
-
### Test Commands
|
|
260
|
-
|
|
261
|
-
```bash
|
|
262
|
-
source .venv/bin/activate
|
|
263
|
-
maturin develop # rebuild after Rust changes
|
|
264
|
-
pytest -v # all 54 tests
|
|
265
|
-
pytest tests/test_e2e.py -v # e2e (27 tests)
|
|
266
|
-
pytest tests/test_races.py -v # race/join (7 tests)
|
|
267
|
-
pytest tests/test_admin_api.py -v # admin API (14 tests)
|
|
268
|
-
pytest tests/scenarios/ -v # scenario tests (6 tests)
|
|
269
|
-
```
|
|
270
|
-
|
|
271
|
-
### Test Tips
|
|
272
|
-
|
|
273
|
-
- Use `SqliteProvider.in_memory()` for fast isolated tests (SQLite smoketest only)
|
|
274
|
-
- All PG tests need `DATABASE_URL` in `.env` (loaded by `python-dotenv`)
|
|
275
|
-
- Each test file uses a separate PG schema for isolation
|
|
276
|
-
- Use short `runtime.shutdown(100)` timeout — it waits the full duration
|
|
277
|
-
- Set `RUST_LOG=info` and use `pytest -s` to see traces in test output
|
|
278
|
-
- Use `worker_lock_timeout_ms=2000` in tests needing fast activity cancellation detection
|
|
279
|
-
|
|
280
|
-
## Logging Control
|
|
281
|
-
|
|
282
|
-
```bash
|
|
283
|
-
RUST_LOG=info pytest -s # All INFO
|
|
284
|
-
RUST_LOG=duroxide::orchestration=debug pytest -s # Orchestration DEBUG
|
|
285
|
-
RUST_LOG=duroxide::activity=info pytest -s # Activity INFO only
|
|
286
|
-
```
|
|
287
|
-
|
|
288
|
-
Traces include structured fields automatically:
|
|
289
|
-
- **Orchestration**: `instance_id`, `execution_id`, `orchestration_name`, `orchestration_version`
|
|
290
|
-
- **Activity**: above + `activity_name`, `activity_id`, `worker_id`
|
|
1
|
+
---
|
|
2
|
+
name: duroxide-python-orchestrations
|
|
3
|
+
description: Writing durable workflows in Python using duroxide-python. Use when creating orchestrations, activities, writing tests, or when the user mentions generator workflows, yield patterns, or duroxide-python development.
|
|
4
|
+
---
|
|
5
|
+
|
|
6
|
+
# Duroxide-Python Orchestration Development
|
|
7
|
+
|
|
8
|
+
## Core Rule: Yield vs Regular Functions
|
|
9
|
+
|
|
10
|
+
| Context | Syntax | Why |
|
|
11
|
+
|---------|--------|-----|
|
|
12
|
+
| Orchestrations | `def` + `yield` (generator) | Rust replay engine needs step-by-step control |
|
|
13
|
+
| Activities | `def` (regular function) | Run once, result cached — no replay constraints |
|
|
14
|
+
| Orchestration tracing | Direct call (no yield) | Fire-and-forget, delegates to Rust |
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
# ✅ Orchestration: generator, yield for durable operations
|
|
18
|
+
@runtime.register_orchestration("MyWorkflow")
|
|
19
|
+
def my_workflow(ctx, input):
|
|
20
|
+
ctx.trace_info("starting") # no yield
|
|
21
|
+
result = yield ctx.schedule_activity("Work", input) # yield
|
|
22
|
+
return result
|
|
23
|
+
|
|
24
|
+
# ✅ Activity: regular function for I/O
|
|
25
|
+
@runtime.register_activity("Work")
|
|
26
|
+
def work(ctx, input):
|
|
27
|
+
ctx.trace_info(f"processing {input}") # no yield
|
|
28
|
+
data = requests.get(input["url"]).json() # sync I/O
|
|
29
|
+
return data
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**Never use `async def` with `yield` for orchestrations** — async generators break the replay model.
|
|
33
|
+
|
|
34
|
+
## Orchestration Context API
|
|
35
|
+
|
|
36
|
+
### Scheduling (MUST yield)
|
|
37
|
+
|
|
38
|
+
```python
|
|
39
|
+
def my_orch(ctx, input):
|
|
40
|
+
# Activity
|
|
41
|
+
result = yield ctx.schedule_activity("Name", input)
|
|
42
|
+
|
|
43
|
+
# Activity with retry
|
|
44
|
+
result = yield ctx.schedule_activity_with_retry("Name", input, {
|
|
45
|
+
"max_attempts": 3,
|
|
46
|
+
"backoff": "exponential",
|
|
47
|
+
"timeout_ms": 5000,
|
|
48
|
+
"total_timeout_ms": 30000,
|
|
49
|
+
})
|
|
50
|
+
|
|
51
|
+
# Timer (durable delay)
|
|
52
|
+
yield ctx.schedule_timer(60000) # 1 minute
|
|
53
|
+
|
|
54
|
+
# External event
|
|
55
|
+
event_data = yield ctx.wait_for_event("approval")
|
|
56
|
+
|
|
57
|
+
# Sub-orchestration (waits for completion)
|
|
58
|
+
child_result = yield ctx.schedule_sub_orchestration("Child", child_input)
|
|
59
|
+
|
|
60
|
+
# Sub-orchestration with explicit ID
|
|
61
|
+
child_result = yield ctx.schedule_sub_orchestration_with_id("Child", "child-1", child_input)
|
|
62
|
+
|
|
63
|
+
# Fire-and-forget orchestration (returns immediately)
|
|
64
|
+
yield ctx.start_orchestration("BackgroundWork", "bg-1", bg_input)
|
|
65
|
+
|
|
66
|
+
# Deterministic values
|
|
67
|
+
now = yield ctx.utc_now() # timestamp in ms
|
|
68
|
+
guid = yield ctx.new_guid() # deterministic UUID
|
|
69
|
+
|
|
70
|
+
# Continue as new (restart with fresh history)
|
|
71
|
+
yield ctx.continue_as_new(new_input)
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
### Composition (MUST yield)
|
|
75
|
+
|
|
76
|
+
```python
|
|
77
|
+
# Fan-out / fan-in — wait for ALL tasks (supports all task types)
|
|
78
|
+
results = yield ctx.all([
|
|
79
|
+
ctx.schedule_activity("TaskA", input_a),
|
|
80
|
+
ctx.schedule_activity("TaskB", input_b),
|
|
81
|
+
ctx.schedule_timer(5000), # timers work too
|
|
82
|
+
ctx.wait_for_event("approval"), # waits work too
|
|
83
|
+
])
|
|
84
|
+
# results = [result_a, result_b, {"ok": None}, {"ok": event_data}]
|
|
85
|
+
|
|
86
|
+
# Race — wait for FIRST of 2 tasks (supports all task types)
|
|
87
|
+
winner = yield ctx.race(
|
|
88
|
+
ctx.schedule_activity("FastService", input),
|
|
89
|
+
ctx.schedule_timer(5000),
|
|
90
|
+
)
|
|
91
|
+
# winner = {"index": 0|1, "value": ...}
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
**`ctx.race()` supports exactly 2 tasks** (maps to Rust `select2`). Nesting `all()`/`race()` inside `all()` or `race()` is **not supported** — the runtime rejects it.
|
|
95
|
+
|
|
96
|
+
### Cooperative Activity Cancellation
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
@runtime.register_activity("LongTask")
|
|
100
|
+
def long_task(ctx, input):
|
|
101
|
+
for i in range(1000):
|
|
102
|
+
if ctx.is_cancelled():
|
|
103
|
+
ctx.trace_info("cancelled, cleaning up")
|
|
104
|
+
return {"status": "cancelled"}
|
|
105
|
+
time.sleep(0.1) # do work
|
|
106
|
+
return {"status": "done"}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`ctx.is_cancelled()` checks whether the orchestration no longer needs the activity result (e.g., lost a race). Detection latency is `worker_lock_timeout_ms / 2` (default 30s → ~15s).
|
|
110
|
+
|
|
111
|
+
### Tracing (NO yield — fire-and-forget)
|
|
112
|
+
|
|
113
|
+
```python
|
|
114
|
+
ctx.trace_info("message") # suppressed during replay automatically
|
|
115
|
+
ctx.trace_warn("message")
|
|
116
|
+
ctx.trace_error("message")
|
|
117
|
+
ctx.trace_debug("message")
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
Tracing delegates to the Rust `OrchestrationContext` which has the `is_replaying` guard. **Do not use `print()`** in orchestrations — it will duplicate on replay.
|
|
121
|
+
|
|
122
|
+
## Activity Context API
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
@runtime.register_activity("MyActivity")
|
|
126
|
+
def my_activity(ctx, input):
|
|
127
|
+
# Available fields
|
|
128
|
+
ctx.instance_id
|
|
129
|
+
ctx.execution_id
|
|
130
|
+
ctx.orchestration_name
|
|
131
|
+
ctx.orchestration_version
|
|
132
|
+
ctx.activity_name
|
|
133
|
+
ctx.worker_id
|
|
134
|
+
|
|
135
|
+
# Cooperative cancellation
|
|
136
|
+
if ctx.is_cancelled():
|
|
137
|
+
ctx.trace_info("cancelled")
|
|
138
|
+
return {"status": "cancelled"}
|
|
139
|
+
|
|
140
|
+
# Tracing (delegates to Rust ActivityContext — full structured fields)
|
|
141
|
+
ctx.trace_info(f"processing {input['id']}")
|
|
142
|
+
ctx.trace_warn("slow response")
|
|
143
|
+
ctx.trace_error("connection failed")
|
|
144
|
+
ctx.trace_debug("raw payload: ...")
|
|
145
|
+
|
|
146
|
+
# Activities can do anything — I/O, HTTP, DB, etc.
|
|
147
|
+
data = requests.get(input["url"]).json()
|
|
148
|
+
return data
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
## Determinism Rules
|
|
152
|
+
|
|
153
|
+
Orchestrations **must be deterministic** — the replay engine re-executes from the beginning on every dispatch:
|
|
154
|
+
|
|
155
|
+
| ✅ Safe | ❌ Breaks Replay |
|
|
156
|
+
|---------|-----------------|
|
|
157
|
+
| `yield ctx.utc_now()` | `time.time()` |
|
|
158
|
+
| `yield ctx.new_guid()` | `uuid.uuid4()` |
|
|
159
|
+
| `ctx.trace_info()` | `print()` (duplicates) |
|
|
160
|
+
| `yield ctx.schedule_timer(ms)` | `time.sleep()` |
|
|
161
|
+
| Pure computation, conditionals | `requests.get()`, file I/O, DB queries |
|
|
162
|
+
| `json.loads()`, `json.dumps()` | `os.environ["X"]` (may change) |
|
|
163
|
+
|
|
164
|
+
**All I/O belongs in activities**, not orchestrations.
|
|
165
|
+
|
|
166
|
+
## Common Patterns
|
|
167
|
+
|
|
168
|
+
### Error Handling
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
def my_orch(ctx, input):
|
|
172
|
+
try:
|
|
173
|
+
result = yield ctx.schedule_activity("RiskyOp", input)
|
|
174
|
+
return result
|
|
175
|
+
except Exception as e:
|
|
176
|
+
ctx.trace_error(f"failed: {e}")
|
|
177
|
+
yield ctx.schedule_activity("Cleanup", {"error": str(e)})
|
|
178
|
+
return {"status": "failed"}
|
|
179
|
+
```
|
|
180
|
+
|
|
181
|
+
### Eternal Orchestration (continue-as-new)
|
|
182
|
+
|
|
183
|
+
```python
|
|
184
|
+
def monitor(ctx, input):
|
|
185
|
+
state = input.get("state", {"iteration": 0})
|
|
186
|
+
health = yield ctx.schedule_activity("CheckHealth", input["target"])
|
|
187
|
+
ctx.trace_info(f"check #{state['iteration']}: {health['status']}")
|
|
188
|
+
yield ctx.schedule_timer(30000)
|
|
189
|
+
yield ctx.continue_as_new({
|
|
190
|
+
"target": input["target"],
|
|
191
|
+
"state": {"iteration": state["iteration"] + 1},
|
|
192
|
+
})
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
### Race with Timeout
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
def my_orch(ctx, input):
|
|
199
|
+
winner = yield ctx.race(
|
|
200
|
+
ctx.schedule_activity("SlowOperation", input),
|
|
201
|
+
ctx.schedule_timer(10000),
|
|
202
|
+
)
|
|
203
|
+
if winner["index"] == 1:
|
|
204
|
+
ctx.trace_warn("operation timed out")
|
|
205
|
+
return {"status": "timeout"}
|
|
206
|
+
return {"status": "ok", "result": winner["value"]}
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
### Versioned Orchestrations
|
|
210
|
+
|
|
211
|
+
```python
|
|
212
|
+
@runtime.register_orchestration("MyWorkflow")
|
|
213
|
+
def my_workflow_v1(ctx, input):
|
|
214
|
+
ctx.trace_info("[v1.0.0] starting")
|
|
215
|
+
return (yield ctx.schedule_activity("Work", input))
|
|
216
|
+
|
|
217
|
+
@runtime.register_orchestration_versioned("MyWorkflow", "1.0.1")
|
|
218
|
+
def my_workflow_v2(ctx, input):
|
|
219
|
+
ctx.trace_info("[v1.0.1] starting")
|
|
220
|
+
yield ctx.schedule_activity("Validate", input)
|
|
221
|
+
return (yield ctx.schedule_activity("Work", input))
|
|
222
|
+
```
|
|
223
|
+
|
|
224
|
+
## Writing Tests
|
|
225
|
+
|
|
226
|
+
Tests use pytest:
|
|
227
|
+
|
|
228
|
+
```python
|
|
229
|
+
import time, pytest
|
|
230
|
+
from duroxide import SqliteProvider, PostgresProvider, Client, Runtime, PyRuntimeOptions
|
|
231
|
+
|
|
232
|
+
@pytest.fixture(scope="module")
|
|
233
|
+
def provider():
|
|
234
|
+
db_url = os.environ.get("DATABASE_URL")
|
|
235
|
+
if not db_url:
|
|
236
|
+
pytest.skip("DATABASE_URL not set")
|
|
237
|
+
return PostgresProvider.connect_with_schema(db_url, "my_test_schema")
|
|
238
|
+
|
|
239
|
+
def test_my_feature(provider):
|
|
240
|
+
client = Client(provider)
|
|
241
|
+
runtime = Runtime(provider, PyRuntimeOptions(dispatcher_poll_interval_ms=50))
|
|
242
|
+
|
|
243
|
+
runtime.register_activity("Echo", lambda ctx, inp: inp)
|
|
244
|
+
|
|
245
|
+
@runtime.register_orchestration("MyWorkflow")
|
|
246
|
+
def my_workflow(ctx, input):
|
|
247
|
+
return (yield ctx.schedule_activity("Echo", input))
|
|
248
|
+
|
|
249
|
+
runtime.start()
|
|
250
|
+
try:
|
|
251
|
+
client.start_orchestration("test-1", "MyWorkflow", "hello")
|
|
252
|
+
result = client.wait_for_orchestration("test-1", 10_000)
|
|
253
|
+
assert result.status == "Completed"
|
|
254
|
+
assert result.output == "hello"
|
|
255
|
+
finally:
|
|
256
|
+
runtime.shutdown(100)
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
### Test Commands
|
|
260
|
+
|
|
261
|
+
```bash
|
|
262
|
+
source .venv/bin/activate
|
|
263
|
+
maturin develop # rebuild after Rust changes
|
|
264
|
+
pytest -v # all 54 tests
|
|
265
|
+
pytest tests/test_e2e.py -v # e2e (27 tests)
|
|
266
|
+
pytest tests/test_races.py -v # race/join (7 tests)
|
|
267
|
+
pytest tests/test_admin_api.py -v # admin API (14 tests)
|
|
268
|
+
pytest tests/scenarios/ -v # scenario tests (6 tests)
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
### Test Tips
|
|
272
|
+
|
|
273
|
+
- Use `SqliteProvider.in_memory()` for fast isolated tests (SQLite smoketest only)
|
|
274
|
+
- All PG tests need `DATABASE_URL` in `.env` (loaded by `python-dotenv`)
|
|
275
|
+
- Each test file uses a separate PG schema for isolation
|
|
276
|
+
- Use short `runtime.shutdown(100)` timeout — it waits the full duration
|
|
277
|
+
- Set `RUST_LOG=info` and use `pytest -s` to see traces in test output
|
|
278
|
+
- Use `worker_lock_timeout_ms=2000` in tests needing fast activity cancellation detection
|
|
279
|
+
|
|
280
|
+
## Logging Control
|
|
281
|
+
|
|
282
|
+
```bash
|
|
283
|
+
RUST_LOG=info pytest -s # All INFO
|
|
284
|
+
RUST_LOG=duroxide::orchestration=debug pytest -s # Orchestration DEBUG
|
|
285
|
+
RUST_LOG=duroxide::activity=info pytest -s # Activity INFO only
|
|
286
|
+
```
|
|
287
|
+
|
|
288
|
+
Traces include structured fields automatically:
|
|
289
|
+
- **Orchestration**: `instance_id`, `execution_id`, `orchestration_name`, `orchestration_version`
|
|
290
|
+
- **Activity**: above + `activity_name`, `activity_id`, `worker_id`
|