Skip to content

Commit

Permalink
demo instructions
Browse files Browse the repository at this point in the history
  • Loading branch information
rachfop committed Apr 21, 2023
1 parent 391c6c2 commit 8d83a7f
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 17 deletions.
17 changes: 17 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,23 @@ poetry run python run_workflow.py
![](stat/../static/webui_failure.png)
![](static/failure.gif)


### Demo: Recover forward

Enter your booking information in the Flask app <http://127.0.0.1:5000>. then see the tasks in the Web UI at <http://localhost:8233/>.
Select your running or completed Workflow ID.
Under **Recent** events, select the failed Activity, `book_flight` (in compact view).
Under **ActivityTaskStarted** you'll see the Attempts (5), and the stack trace message letting you know the last failed attempt.

### Demo: Recover backwards

In the `book_workflow.py` modify the global variable `ATTEMPTS_FLIGHT = 5` to `ATTEMPTS_FLIGHT = 2`, so that the `book_flight` Activity attempts a retry twice.
Renter your booking information in the Flask app <http://127.0.0.1:5000>, then see the tasks in the Web UI at <http://localhost:8233/>.
Select your running or completed Workflow ID.
Under **Recent** events, select the failed Activity, `book_flight` (in compact view).
Under **ActivityTaskStarted** you'll see the Attempts (2), and the stack trace message letting you know the last failed attempt.
Then notice how the Workflow executes the compensations.

## Design

The booking saga is implemented using the Temporal Workflow framework, which provides a robust and fault-tolerant platform for coordinating distributed transactions.
Expand Down
5 changes: 3 additions & 2 deletions activities.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
from temporalio import activity
import asyncio
from dataclasses import dataclass

from temporalio import activity


@dataclass
class BookVacationInput:
Expand All @@ -25,7 +26,7 @@ async def book_hotel(input: BookVacationInput) -> str:

@activity.defn
async def book_flight(input: BookVacationInput) -> str:
if activity.info().attempt < 5:
if activity.info().attempt < 4:
activity.heartbeat(
f"Invoking activity, attempt number {activity.info().attempt}"
)
Expand Down
10 changes: 6 additions & 4 deletions book_workflow.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
from temporalio import workflow
from datetime import timedelta

from temporalio import workflow
from temporalio.common import RetryPolicy

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

ATTEMPTS_FLIGHT = 5

ATTEMPTS_FLIGHT = 3

@workflow.defn
class BookWorkflow:
Expand All @@ -15,7 +17,7 @@ async def run(self, input: BookVacationInput):

try:
compensations.append("undo_book_car")
output = " " + await workflow.execute_activity(
output = await workflow.execute_activity(
book_car,
input,
start_to_close_timeout=timedelta(seconds=10),
Expand Down
5 changes: 3 additions & 2 deletions run_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

from temporalio.client import Client
from temporalio.worker import Worker

from activities import (
book_car,
book_hotel,
book_flight,
book_hotel,
undo_book_car,
undo_book_hotel,
undo_book_flight,
undo_book_hotel,
)
from book_workflow import BookWorkflow

Expand Down
10 changes: 6 additions & 4 deletions run_workflow.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import uuid

from flask import Flask, render_template, request
from temporalio.client import Client

from activities import BookVacationInput

# Import the workflow from the previous code
from book_workflow import BookWorkflow
from activities import BookVacationInput
from flask import Flask, request, render_template
import uuid

app = Flask(__name__)

Expand All @@ -16,7 +18,7 @@ async def display_form():

@app.route("/book", methods=["POST"])
async def book_vacation():
user_id = f'{request.form.get("name")}-{str(uuid.uuid4( ))}'
user_id = f'{request.form.get("name").replace(" ", "-").lower()}-{str(uuid.uuid4().int)[:6]}'
car = request.form.get("car")
hotel = request.form.get("hotel")
flight = request.form.get("flight")
Expand Down
24 changes: 19 additions & 5 deletions templates/book_vacation.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Book a Vacation</title>
<title>Temporal Book a Vacation</title>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<style>
body {
font-family: Arial, Helvetica, sans-serif;
Expand All @@ -13,7 +15,16 @@
text-align: center;
margin-top: 30px;
}

h2 {
color: #222222;
text-align: center;
margin-top: 30px;
}
p {
color: #444444;
text-align: center;
margin-bottom: 10px;
}
form {
margin: 0 auto;
width: 50%;
Expand Down Expand Up @@ -89,7 +100,10 @@
</style>
</head>
<body>
<h1>Book a Vacation</h1>
<h1>Temporal Book a Vacation</h1>
<h2>GitHub: <a href="https://github.com/temporalio">github.com/temporalio</a></h2>
<h2>Docs: <a href="https://docs.temporal.io">docs.temporal.io</a></h2>
<p>Eliminate complex error or retry logic, avoid callbacks, and ensure that every workflow you start, completes. Temporal delivers durable execution for your services and applications.</p>
<form method="post" action="/book">
<label for="name">Name:</label>
<input
Expand All @@ -106,9 +120,9 @@ <h1>Book a Vacation</h1>
type="text"
name="car"
id="car"
value="Toyota Supra"
value="Ford 150"
required
placeholder="Toyota Supra"
placeholder="Ford 150"
/><br />

<label for="hotel">Hotel:</label>
Expand Down

0 comments on commit 8d83a7f

Please sign in to comment.