forked from temporalio/samples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.py
54 lines (45 loc) · 1.69 KB
/
worker.py
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
import argparse
import asyncio
from temporalio.client import Client
from temporalio.worker import Worker
from patching.activities import post_patch_activity, pre_patch_activity
interrupt_event = asyncio.Event()
async def main():
# Import which workflow based on CLI arg
parser = argparse.ArgumentParser(description="Run worker")
parser.add_argument(
"--workflow",
help="Which workflow. Can be 'initial', 'patched', 'patch-deprecated', or 'patch-complete'",
required=True,
)
args = parser.parse_args()
if args.workflow == "initial":
from patching.workflow_1_initial import MyWorkflow
elif args.workflow == "patched":
from patching.workflow_2_patched import MyWorkflow # type: ignore
elif args.workflow == "patch-deprecated":
from patching.workflow_3_patch_deprecated import MyWorkflow # type: ignore
elif args.workflow == "patch-complete":
from patching.workflow_4_patch_complete import MyWorkflow # type: ignore
else:
raise RuntimeError("Unrecognized workflow")
# Connect client
client = await Client.connect("localhost:7233")
# Run a worker for the workflow
async with Worker(
client,
task_queue="patching-task-queue",
workflows=[MyWorkflow],
activities=[pre_patch_activity, post_patch_activity],
):
# Wait until interrupted
print("Worker started")
await interrupt_event.wait()
print("Shutting down")
if __name__ == "__main__":
loop = asyncio.new_event_loop()
try:
loop.run_until_complete(main())
except KeyboardInterrupt:
interrupt_event.set()
loop.run_until_complete(loop.shutdown_asyncgens())