Skip to content

Commit

Permalink
adding template
Browse files Browse the repository at this point in the history
  • Loading branch information
rachfop committed Apr 20, 2023
1 parent 28c0d12 commit 391c6c2
Show file tree
Hide file tree
Showing 7 changed files with 401 additions and 42 deletions.
30 changes: 18 additions & 12 deletions activities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
from temporalio import activity
from obj import BookVacationInput
import asyncio
from dataclasses import dataclass

import random

@dataclass
class BookVacationInput:
book_user_id: str
book_car_id: str
book_hotel_id: str
book_flight_id: str


@activity.defn
Expand All @@ -18,26 +25,25 @@ async def book_hotel(input: BookVacationInput) -> str:

@activity.defn
async def book_flight(input: BookVacationInput) -> str:
seats_available = random.randint(0, 10)
try:
if seats_available < 1:
raise Exception("No seats remaining")
else:
print(f"Booking flight: {input.book_flight_id}")
except Exception:
raise Exception("No seats remaining")
if activity.info().attempt < 5:
activity.heartbeat(
f"Invoking activity, attempt number {activity.info().attempt}"
)
await asyncio.sleep(1)
raise RuntimeError("Service is down")
return f"Booking flight: {input.book_flight_id}\n"


@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}"
return f"Undoing booking of car: {input.book_car_id}\n"


@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}"
return f"Undoing booking of hotel: {input.book_hotel_id}\n"


@activity.defn
Expand Down
16 changes: 8 additions & 8 deletions book_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
from temporalio.common import RetryPolicy

with workflow.unsafe.imports_passed_through():
from activities import book_car, book_hotel, book_flight
from obj import BookVacationInput
from activities import book_car, book_hotel, book_flight, BookVacationInput

ATTEMPTS_FLIGHT = 3

@workflow.defn
class BookWorkflow:
@workflow.run
async def run(self, input: BookVacationInput):
compensations = []

try:
compensations.append("undo_book_car")
await workflow.execute_activity(
output = " " + await workflow.execute_activity(
book_car,
input,
start_to_close_timeout=timedelta(seconds=10),
Expand All @@ -28,7 +28,7 @@ async def run(self, input: BookVacationInput):
),
)
compensations.append("undo_book_hotel")
await workflow.execute_activity(
output += " " + await workflow.execute_activity(
book_hotel,
input,
start_to_close_timeout=timedelta(seconds=10),
Expand All @@ -42,19 +42,19 @@ async def run(self, input: BookVacationInput):
)

compensations.append("undo_book_flight")
await workflow.execute_activity(
output += " " + await workflow.execute_activity(
book_flight,
input,
start_to_close_timeout=timedelta(seconds=10),
task_queue="saga-task-queue",
retry_policy=RetryPolicy(
initial_interval=timedelta(seconds=1),
maximum_interval=timedelta(seconds=1),
maximum_attempts=3,
maximum_attempts=ATTEMPTS_FLIGHT,
non_retryable_error_types=["Exception"],
),
)
return "Voyage booked"
return output
except Exception:
for compensation in reversed(compensations):
await workflow.execute_activity(
Expand Down
8 changes: 0 additions & 8 deletions obj.py

This file was deleted.

175 changes: 174 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
ruff = "^0.0.246"
flask = {extras = ["async"], version = "^2.2.3"}

[tool.poetry.group.dev.dependencies]
temporalio = "^1.1.0"
Expand All @@ -17,10 +18,11 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.poe.tasks]
format = [{cmd = "ruff --fix ./*.py"}]
format = [{cmd = "ruff check --fix ."}]
lint = [{cmd = "ruff check ./*.py"}]


[tool.ruff]
select = ["E", "F"]
line-length = 120
line-length = 120
fix = true
Loading

0 comments on commit 391c6c2

Please sign in to comment.