Skip to content

Commit 0a34792

Browse files
committed
Use an importable batch handler with callback preflight controls
1 parent 65a5b28 commit 0a34792

3 files changed

Lines changed: 53 additions & 7 deletions

File tree

‎examples/CHECKPOINTED_BATCH.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ python -m examples.checkpointed_batch ./receipts-commit after-commit
1818
python -m examples.checkpointed_batch ./receipts-checkpoint after-checkpoint
1919
```
2020

21+
The importable `BatchActivity.execute` handler carries only serializable provider
22+
configuration, so it also passes the SDK's cooperative callback preflight.
2123
The activity processes three items, with a maximum of sixteen bounded items.
2224
Its two attempts, one-second retry delay, ten-second heartbeat timeout,
2325
thirty-second start-to-close timeout and sixty-second schedule-to-close timeout

‎examples/checkpointed_batch.py‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,14 @@ def run(self, ctx: workflow.WorkflowContext, batch: dict[str, Any]) -> Generator
122122
))
123123

124124

125-
async def run(root: Path, failure: str) -> None:
126-
store = await asyncio.to_thread(ReceiptStore.create, root)
125+
@dataclass(frozen=True)
126+
class BatchActivity:
127+
"""Importable handler carrying only the provider's serializable configuration."""
128+
129+
store: ReceiptStore
127130

128131
@activity.defn(name=ACTIVITY)
129-
async def execute(body: dict[str, Any]) -> dict[str, Any]:
132+
async def execute(self, body: dict[str, Any]) -> dict[str, Any]:
130133
ctx = activity.context()
131134
try:
132135
received = ctx.last_heartbeat_details(result_type=Progress)
@@ -136,12 +139,15 @@ async def execute(body: dict[str, Any]) -> dict[str, Any]:
136139
async def acknowledge(progress: Progress) -> None:
137140
await ctx.heartbeat(asdict(progress))
138141

139-
return await process_batch(store, Batch(**body), received, ctx.info.attempt_number, acknowledge)
142+
return await process_batch(self.store, Batch(**body), received, ctx.info.attempt_number, acknowledge)
140143

144+
145+
async def run(root: Path, failure: str) -> None:
146+
store = await asyncio.to_thread(ReceiptStore.create, root)
141147
queue = "checkpointed-batch-" + uuid4().hex
142148
async with Client(os.environ.get("SERVER_URL", "http://127.0.0.1:8080"),
143149
token=os.environ.get("WORKFLOW_TOKEN"), namespace="default", timeout=10) as client:
144-
worker = Worker(client, task_queue=queue, workflows=[BatchWorkflow], activities=[execute])
150+
worker = Worker(client, task_queue=queue, workflows=[BatchWorkflow], activities=[BatchActivity(store).execute])
145151
handle = await client.start_workflow(
146152
workflow_type=ACTIVITY, task_queue=queue, workflow_id=queue,
147153
input=[asdict(Batch("demo-" + uuid4().hex, ["first", "second", "third"], failure))],

‎tests/test_checkpointed_batch_example.py‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
"""Synthetic provider/acknowledgement boundaries; no Server or worker invocation."""
22
from __future__ import annotations
33

4-
from dataclasses import replace
4+
import pickle
5+
from dataclasses import asdict, replace
56
from pathlib import Path
67
from typing import Any
8+
from unittest.mock import AsyncMock
79

810
import pytest
9-
from examples.checkpointed_batch import FAILURES, Batch, Progress, ReceiptStore, process_batch
11+
from examples.checkpointed_batch import ACTIVITY, FAILURES, Batch, BatchActivity, Progress, ReceiptStore, process_batch
1012

13+
from durable_workflow import ActivityInfo, Client, Worker
14+
from durable_workflow._activity_process import CallbackInvocation
1115
from durable_workflow.errors import NonRetryableError
1216

1317

@@ -134,3 +138,37 @@ def test_existing_directory_and_partial_receipt_are_preserved(tmp_path: Path) ->
134138
with pytest.raises(NonRetryableError, match="partial"):
135139
store.commit_or_reconcile(business_id, fingerprint, 0, items[0])
136140
assert path.read_bytes() == b"partial"
141+
142+
143+
def test_actual_handler_survives_cooperative_preflight_and_callback_serialization(tmp_path: Path) -> None:
144+
store = ReceiptStore.create(tmp_path / "provider")
145+
handler = BatchActivity(store).execute
146+
worker = Worker(AsyncMock(spec=Client), task_queue="queue", activities=[handler])
147+
worker._cooperative_cancellation_supported = True
148+
worker._validate_cooperative_activity_handlers()
149+
assert worker.activities[ACTIVITY] == handler
150+
invocation = CallbackInvocation(
151+
handler=worker.activities[ACTIVITY], args=(asdict(batch()),),
152+
info=ActivityInfo("task", ACTIVITY, "attempt", 2, "queue", "worker"), task={},
153+
)
154+
restored = pickle.loads(invocation.serialize())
155+
assert isinstance(restored.handler.__self__, BatchActivity)
156+
assert restored.handler.__self__.store == store
157+
assert restored.args == (asdict(batch()),)
158+
assert inventory(store) == []
159+
160+
161+
def test_nested_handler_is_refused_by_the_same_preflight_and_serializer() -> None:
162+
async def legacy_nested_handler(_body: dict[str, Any]) -> None:
163+
pass
164+
165+
worker = Worker(AsyncMock(spec=Client), task_queue="queue", activities=[legacy_nested_handler])
166+
worker._cooperative_cancellation_supported = True
167+
with pytest.raises(RuntimeError, match="spawn-compatible"):
168+
worker._validate_cooperative_activity_handlers()
169+
invocation = CallbackInvocation(
170+
handler=legacy_nested_handler, args=(),
171+
info=ActivityInfo("task", ACTIVITY, "attempt", 2, "queue", "worker"), task={},
172+
)
173+
with pytest.raises(ValueError, match="spawn-compatible"):
174+
invocation.serialize()

0 commit comments

Comments
 (0)