-
Notifications
You must be signed in to change notification settings - Fork 5
/
activities.py
60 lines (44 loc) · 1.69 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
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:
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:
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:
if activity.info().attempt < input.attempts:
activity.heartbeat(
f"Invoking activity, attempt number {activity.info().attempt}"
)
await asyncio.sleep(1)
raise RuntimeError("Service is down")
elif activity.info().attempt > 3:
raise RuntimeError(
"Too many retries, flight booking not possible at this time!"
)
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}"