-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathactivities.py
82 lines (61 loc) · 2.43 KB
/
activities.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
import asyncio
from dataclasses import dataclass
from temporalio import activity
@dataclass
class BookVacationInput:
book_user_id: str
book_car_id: str
book_hotel_id: str
book_flight_id: str
attempts: int
@activity.defn
async def book_car(input: BookVacationInput) -> str:
await asyncio.sleep(3)
if activity.info().attempt < input.attempts:
activity.heartbeat(
f"Invoking activity, attempt number {activity.info().attempt}"
)
await asyncio.sleep(3)
raise RuntimeError("Car service is down")
if "invalid" in input.book_car_id:
raise Exception("Invalid car booking, rolling back!")
print(f"Booking car: {input.book_car_id}")
return f"Booked car: {input.book_car_id}"
@activity.defn
async def book_hotel(input: BookVacationInput) -> str:
await asyncio.sleep(3)
if activity.info().attempt < input.attempts:
activity.heartbeat(
f"Invoking activity, attempt number {activity.info().attempt}"
)
await asyncio.sleep(3)
raise RuntimeError("Hotel service is down")
if "invalid" in input.book_hotel_id:
raise Exception("Invalid hotel booking, rolling back!")
print(f"Booking hotel: {input.book_hotel_id}")
return f"Booked hotel: {input.book_hotel_id}"
@activity.defn
async def book_flight(input: BookVacationInput) -> str:
await asyncio.sleep(3)
if activity.info().attempt < input.attempts:
activity.heartbeat(
f"Invoking activity, attempt number {activity.info().attempt}"
)
await asyncio.sleep(3)
raise RuntimeError("Flight service is down")
if "invalid" in input.book_flight_id:
raise Exception("Invalid flight booking, rolling back!")
print(f"Booking flight: {input.book_flight_id}")
return f"Booking flight: {input.book_flight_id}"
@activity.defn
async def undo_book_car(input: BookVacationInput) -> str:
print(f"Undoing booking of car: {input.book_car_id}")
return f"Undoing booking of car: {input.book_car_id}"
@activity.defn
async def undo_book_hotel(input: BookVacationInput) -> str:
print(f"Undoing booking of hotel: {input.book_hotel_id}")
return f"Undoing booking of hotel: {input.book_hotel_id}"
@activity.defn
async def undo_book_flight(input: BookVacationInput) -> str:
print(f"Undoing booking of flight: {input.book_flight_id}")
return f"Undoing booking of flight: {input.book_flight_id}"