forked from temporalio/samples-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello_activity_choice.py
130 lines (106 loc) · 3.75 KB
/
hello_activity_choice.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import asyncio
from dataclasses import dataclass
from datetime import timedelta
from enum import IntEnum
from typing import List
from temporalio import activity, workflow
from temporalio.client import Client
from temporalio.worker import Worker
# Activities that will be called by the workflow
@activity.defn
async def order_apples(amount: int) -> str:
return f"Ordered {amount} Apples..."
@activity.defn
async def order_bananas(amount: int) -> str:
return f"Ordered {amount} Bananas..."
@activity.defn
async def order_cherries(amount: int) -> str:
return f"Ordered {amount} Cherries..."
@activity.defn
async def order_oranges(amount: int) -> str:
return f"Ordered {amount} Oranges..."
# We have to make enumerates IntEnum to be JSON serializable
class Fruit(IntEnum):
APPLE = 1
BANANA = 2
CHERRY = 3
ORANGE = 4
@dataclass
class ShoppingListItem:
fruit: Fruit
amount: int
@dataclass
class ShoppingList:
items: List[ShoppingListItem]
# Basic workflow that logs and invokes different activities based on input
@workflow.defn
class PurchaseFruitsWorkflow:
@workflow.run
async def run(self, list: ShoppingList) -> str:
# Order each thing on the list
ordered: List[str] = []
for item in list.items:
if item.fruit is Fruit.APPLE:
ordered.append(
await workflow.execute_activity(
order_apples,
item.amount,
start_to_close_timeout=timedelta(seconds=5),
)
)
elif item.fruit is Fruit.BANANA:
ordered.append(
await workflow.execute_activity(
order_bananas,
item.amount,
start_to_close_timeout=timedelta(seconds=5),
)
)
elif item.fruit is Fruit.CHERRY:
ordered.append(
await workflow.execute_activity(
order_cherries,
item.amount,
start_to_close_timeout=timedelta(seconds=5),
)
)
elif item.fruit is Fruit.ORANGE:
ordered.append(
await workflow.execute_activity(
order_oranges,
item.amount,
start_to_close_timeout=timedelta(seconds=5),
)
)
else:
raise ValueError(f"Unrecognized fruit: {item.fruit}")
return "".join(ordered)
async def main():
# Start client
client = await Client.connect("localhost:7233")
# Run a worker for the workflow
async with Worker(
client,
task_queue="hello-activity-choice-task-queue",
workflows=[PurchaseFruitsWorkflow],
activities=[order_apples, order_bananas, order_cherries, order_oranges],
):
# While the worker is running, use the client to run the workflow and
# print out its result. Note, in many production setups, the client
# would be in a completely separate process from the worker.
result = await client.execute_workflow(
PurchaseFruitsWorkflow.run,
ShoppingList(
[
ShoppingListItem(Fruit.APPLE, 8),
ShoppingListItem(Fruit.BANANA, 5),
ShoppingListItem(Fruit.CHERRY, 1),
ShoppingListItem(Fruit.ORANGE, 4),
]
),
id="hello-activity-choice-workflow-id",
task_queue="hello-activity-choice-task-queue",
)
print(f"Order result: {result}")
if __name__ == "__main__":
asyncio.run(main())