|
| 1 | +import asyncio |
| 2 | +from dataclasses import dataclass |
| 3 | +from datetime import timedelta |
| 4 | +from enum import IntEnum |
| 5 | +from typing import List |
| 6 | + |
| 7 | +from temporalio import activity, workflow |
| 8 | +from temporalio.client import Client |
| 9 | +from temporalio.worker import Worker |
| 10 | + |
| 11 | +# Activities that will be called by the workflow |
| 12 | + |
| 13 | + |
| 14 | +@activity.defn |
| 15 | +async def order_apples(amount: int) -> str: |
| 16 | + return f"Ordered {amount} Apples..." |
| 17 | + |
| 18 | + |
| 19 | +@activity.defn |
| 20 | +async def order_bananas(amount: int) -> str: |
| 21 | + return f"Ordered {amount} Bananas..." |
| 22 | + |
| 23 | + |
| 24 | +@activity.defn |
| 25 | +async def order_cherries(amount: int) -> str: |
| 26 | + return f"Ordered {amount} Cherries..." |
| 27 | + |
| 28 | + |
| 29 | +@activity.defn |
| 30 | +async def order_oranges(amount: int) -> str: |
| 31 | + return f"Ordered {amount} Oranges..." |
| 32 | + |
| 33 | + |
| 34 | +# We have to make enumerates IntEnum to be JSON serializable |
| 35 | +class Fruit(IntEnum): |
| 36 | + APPLE = 1 |
| 37 | + BANANA = 2 |
| 38 | + CHERRY = 3 |
| 39 | + ORANGE = 4 |
| 40 | + |
| 41 | + |
| 42 | +@dataclass |
| 43 | +class ShoppingListItem: |
| 44 | + fruit: Fruit |
| 45 | + amount: int |
| 46 | + |
| 47 | + |
| 48 | +@dataclass |
| 49 | +class ShoppingList: |
| 50 | + items: List[ShoppingListItem] |
| 51 | + |
| 52 | + |
| 53 | +# Basic workflow that logs and invokes different activities based on input |
| 54 | +@workflow.defn |
| 55 | +class PurchaseFruitsWorkflow: |
| 56 | + @workflow.run |
| 57 | + async def run(self, list: ShoppingList) -> str: |
| 58 | + # Order each thing on the list |
| 59 | + ordered: List[str] = [] |
| 60 | + for item in list.items: |
| 61 | + if item.fruit is Fruit.APPLE: |
| 62 | + ordered.append( |
| 63 | + await workflow.execute_activity( |
| 64 | + order_apples, |
| 65 | + item.amount, |
| 66 | + start_to_close_timeout=timedelta(seconds=5), |
| 67 | + ) |
| 68 | + ) |
| 69 | + elif item.fruit is Fruit.BANANA: |
| 70 | + ordered.append( |
| 71 | + await workflow.execute_activity( |
| 72 | + order_bananas, |
| 73 | + item.amount, |
| 74 | + start_to_close_timeout=timedelta(seconds=5), |
| 75 | + ) |
| 76 | + ) |
| 77 | + elif item.fruit is Fruit.CHERRY: |
| 78 | + ordered.append( |
| 79 | + await workflow.execute_activity( |
| 80 | + order_cherries, |
| 81 | + item.amount, |
| 82 | + start_to_close_timeout=timedelta(seconds=5), |
| 83 | + ) |
| 84 | + ) |
| 85 | + elif item.fruit is Fruit.ORANGE: |
| 86 | + ordered.append( |
| 87 | + await workflow.execute_activity( |
| 88 | + order_oranges, |
| 89 | + item.amount, |
| 90 | + start_to_close_timeout=timedelta(seconds=5), |
| 91 | + ) |
| 92 | + ) |
| 93 | + else: |
| 94 | + raise ValueError(f"Unrecognized fruit: {item.fruit}") |
| 95 | + return "".join(ordered) |
| 96 | + |
| 97 | + |
| 98 | +async def main(): |
| 99 | + # Start client |
| 100 | + client = await Client.connect("localhost:7233") |
| 101 | + |
| 102 | + # Run a worker for the workflow |
| 103 | + async with Worker( |
| 104 | + client, |
| 105 | + task_queue="hello-activity-choice-task-queue", |
| 106 | + workflows=[PurchaseFruitsWorkflow], |
| 107 | + activities=[order_apples, order_bananas, order_cherries, order_oranges], |
| 108 | + ): |
| 109 | + |
| 110 | + # While the worker is running, use the client to run the workflow and |
| 111 | + # print out its result. Note, in many production setups, the client |
| 112 | + # would be in a completely separate process from the worker. |
| 113 | + result = await client.execute_workflow( |
| 114 | + PurchaseFruitsWorkflow.run, |
| 115 | + ShoppingList( |
| 116 | + [ |
| 117 | + ShoppingListItem(Fruit.APPLE, 8), |
| 118 | + ShoppingListItem(Fruit.BANANA, 5), |
| 119 | + ShoppingListItem(Fruit.CHERRY, 1), |
| 120 | + ShoppingListItem(Fruit.ORANGE, 4), |
| 121 | + ] |
| 122 | + ), |
| 123 | + id="hello-activity-choice-workflow-id", |
| 124 | + task_queue="hello-activity-choice-task-queue", |
| 125 | + ) |
| 126 | + print(f"Order result: {result}") |
| 127 | + |
| 128 | + |
| 129 | +if __name__ == "__main__": |
| 130 | + asyncio.run(main()) |
0 commit comments