Skip to content

Commit 5ab5a39

Browse files
committed
feat: init simple example
1 parent 80c339e commit 5ab5a39

5 files changed

Lines changed: 74 additions & 0 deletions

File tree

simple-examples/__init__.py

Whitespace-only changes.

simple-examples/hatchet.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from hatchet_sdk import Hatchet
2+
from dotenv import load_dotenv
3+
4+
load_dotenv()
5+
6+
# Create a Hatchet instance that will be shared across all workflows
7+
hatchet = Hatchet()

simple-examples/main.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
from .hatchet import hatchet
2+
from .simple import SimpleWorkflow
3+
4+
# This is the entry point for the Hatchet worker process
5+
6+
7+
def start():
8+
worker = hatchet.worker('example-worker')
9+
10+
# Instantiate the workflow and Register the workflow with the worker
11+
simple = SimpleWorkflow()
12+
worker.register_workflow(simple)
13+
14+
# Start the worker and begin listening for events
15+
worker.start()

simple-examples/pyproject.toml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[tool.poetry]
2+
name = "src"
3+
version = "0.0.0"
4+
description = "Easily run background tasks in FastAPI with Hatchet"
5+
authors = []
6+
readme = "README.md"
7+
8+
[tool.poetry.scripts]
9+
api = "src.api.main:start"
10+
hatchet = "src.workflows.main:start"
11+
12+
[tool.poetry.dependencies]
13+
python = "^3.8"
14+
python-dotenv = "^1.0.0"
15+
hatchet-sdk = "^0.9.4"
16+
uvicorn = {extras = ["standard"], version = "^0.27.0"}
17+
fastapi = "^0.109.0"
18+
beautifulsoup4 = "^4.12.3"
19+
requests = "^2.31.0"
20+
urllib3 = "1.26.15"
21+
22+
[build-system]
23+
requires = ["poetry-core"]
24+
build-backend = "poetry.core.masonry.api"
25+

simple-examples/simple.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
from .hatchet import hatchet
2+
from hatchet_sdk import Context
3+
4+
# This is a simple example of a workflow that has 3 steps.
5+
# The workflow is declared decorated with `@hatchet_workflow` and the steps are declared with `@hatchet_step`.
6+
7+
8+
@hatchet.workflow(on_events=["simple:create"])
9+
class SimpleWorkflow:
10+
11+
@hatchet.step()
12+
def step1(self, context: Context):
13+
14+
# The context object is passed to each step and contains the input data for the workflow.
15+
messages = context.workflow_input()['request']['messages']
16+
print("> starting step1", messages)
17+
return {"status": "thinking"}
18+
19+
@hatchet.step(parents=["step1"])
20+
def step2(self, context: Context):
21+
print("starting step2")
22+
return {"status": "writing a response"}
23+
24+
@hatchet.step(parents=["step2"], timeout='5m')
25+
def step3(self, context: Context):
26+
messages = context.workflow_input()['request']['messages']
27+
return {"complete": "true", "status": "idle", "message": "response from step3"}

0 commit comments

Comments
 (0)