Repository navigation
Expand file tree
/
Copy pathsticky_execution.py
More file actions
43 lines (33 loc) · 1.53 KB
/
Copy pathsticky_execution.py
File metadata and controls
43 lines (33 loc) · 1.53 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
"""Run a workflow with bounded, optional history retention.
SERVER_URL=http://localhost:8080 WORKFLOW_TOKEN=dev-token python examples/sticky_execution.py
"""
from __future__ import annotations
import asyncio
import json
import os
import uuid
from typing import Any
from durable_workflow import Client, Worker, workflow
@workflow.defn(name="examples.python-sticky")
class StickyExampleWorkflow:
def run(self, ctx: Any) -> Any:
value = yield ctx.side_effect(lambda: "recorded once")
yield ctx.start_timer(1)
yield ctx.start_timer(1)
return value
async def main() -> None:
async with Client(
os.environ.get("SERVER_URL", "http://localhost:8080"),
token=os.environ["WORKFLOW_TOKEN"], namespace=os.environ.get("DW_NAMESPACE", "default"),
) as client:
queue = f"sticky-example-{uuid.uuid4().hex[:8]}"
worker = Worker(client, task_queue=queue, workflows=[StickyExampleWorkflow],
sticky_cache_capacity=100, sticky_cache_max_bytes=16 * 1024 * 1024,
sticky_cache_ttl_seconds=300)
handle = await client.start_workflow(workflow_type="examples.python-sticky", task_queue=queue,
workflow_id=f"sticky-{uuid.uuid4().hex}")
await worker.run_until(workflow_id=handle.workflow_id, timeout=60)
assert await handle.result(timeout=10) == "recorded once"
print(json.dumps(worker.sticky_cache_metrics(), sort_keys=True))
if __name__ == "__main__":
asyncio.run(main())