-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathbook_workflow.py
57 lines (50 loc) · 1.93 KB
/
book_workflow.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
from datetime import timedelta
from temporalio import workflow
from temporalio.common import RetryPolicy
with workflow.unsafe.imports_passed_through():
from activities import BookVacationInput, book_car, book_flight, book_hotel
@workflow.defn
class BookWorkflow:
@workflow.run
async def run(self, input: BookVacationInput):
compensations = []
try:
compensations.append("undo_book_car")
output = await workflow.execute_activity(
book_car,
input,
start_to_close_timeout=timedelta(seconds=10),
retry_policy=RetryPolicy(
non_retryable_error_types=["Exception"],
),
)
compensations.append("undo_book_hotel")
output += " " + await workflow.execute_activity(
book_hotel,
input,
start_to_close_timeout=timedelta(seconds=10),
retry_policy=RetryPolicy(
non_retryable_error_types=["Exception"],
),
)
compensations.append("undo_book_flight")
output += " " + await workflow.execute_activity(
book_flight,
input,
start_to_close_timeout=timedelta(seconds=10),
retry_policy=RetryPolicy(
initial_interval=timedelta(seconds=1),
maximum_interval=timedelta(seconds=1),
maximum_attempts=input.attempts,
non_retryable_error_types=["Exception"],
),
)
return output
except Exception:
for compensation in reversed(compensations):
await workflow.execute_activity(
compensation,
input,
start_to_close_timeout=timedelta(seconds=10),
)
return "Voyage cancelled"