Files
hatchet/examples/python/fastapi_blog/trigger.py
T
matt a6650ab84c [Python] Refactor: v2.0.0 Prep (#3165)
* refactor: overloads for run methods, deprecate _no_wait flavors

* refactor: same thing for run_many flavors

* fix: use gather_max_concurrency for gathering run results

* refactor: deprecate a bunch of stuff on the context and core hatchet client

* refactor: runs client deprecations

* refactor: add deprecation warning to go duration string durations

* refactor: durable tasks must be async

* chore: changelog

* fix: copilot comments

* fix: couple more

* chore: rm `debug=True` from all the examples

* chore: more debug params

* fix: more deprecations

* fix: more warnings

* fix: non-utc timezones

* chore: deprecate more internal stuff

* fix: a bunch more internal-only stuff, remove non-v2 listener logic

* fix: test

* chore: make a bunch more things internal

* feat: priority enum

* refactor: top-level `types` directory

* refactor: start reworking labels

* fix: some type checker issues

* fix: rm transform method in favor of instance method

* fix: internal worker label types

* fix: more types

* refactor: finish labels

* fix: labels

* chore: gen

* fix: rm internal glue pydantic model

* fix: removed `owned_loop`, register workflows on worker start instead of init

* fix: deprecate ctx getter in favor of property

* refactor: more label cleanup, prepare to remove worker context

* fix: more deprecations

* refactor: get rid of a pydantic a few places we don't need validation

* refactor: plan to remove `BulkPushEventOptions`

* chore: changelog

* chore: changelog

* refactor: trigger types

* fix: pydantic model default

* fix: instrumentor types

* refactor: add `seen_at` to event

* refactor: remove some more protobuf types

* fix: rm unneeded ts_to_iso

* refactor: clean up more examples

* fix: more warnings

* chore: gen

* chore: more warnings

* fix: one more

* fix: warning, namespace

* fix: linters

* fix: double import

* fix: ugh, cursor

* fix: clean up a bunch of suboptimal tests

* fix: overload signatures

* chore: gen

* chore: revert opts change

* chore: one more revert

* feat: start reworking option passing to remove pydantic models

* refactor: worker opt

* fix: type cleanup

* refactor: keep working out signature details

* fix: changelog

* fix: deprecate some streaming methods

* fix: linters

* fix: rebase

* chore: rm some unused stuff

* chore: rm more unused stuff

* fix: rm more uses of `options`

* fix: more deprecation warnings

* fix: instrumentor wrapping

* fix: add test for instrumentor signature

* chore: deprecate upsert labels on the worker context thingy

* fix: deprecate more stuff on the worker context

* feat: add `worker_labels_dict` property

* fix: label types for workers

* chore: update changelog

* fix: version

* refactor: durable_eviction -> eviction_policy

* fix: lint

* fix: instrumentor not passing options properly

* fix: un-remove

* fix: priority

* chore: version

* fix: improve warning log
2026-04-03 16:30:56 -04:00

95 lines
2.0 KiB
Python

from types import TracebackType
from fastapi import BackgroundTasks, FastAPI
from pydantic import BaseModel
from hatchet_sdk import Context, Hatchet
class Session:
## simulate async db session
async def __aenter__(self) -> "Session":
return self
async def __aexit__(
self,
type_: type[BaseException] | None,
value: BaseException | None,
traceback: TracebackType | None,
) -> None:
pass
class User:
def __init__(self, id: int, email: str):
self.id = id
self.email = email
async def get_user(db: Session, user_id: int) -> User:
return User(user_id, "test@example.com")
async def create_user(db: Session) -> User:
return User(1, "test@example.com")
async def send_welcome_email(email: str) -> None:
print(f"Sending welcome email to {email}")
app = FastAPI()
hatchet = Hatchet()
# > FastAPI Background Tasks
async def send_welcome_email_task_bg(user_id: int) -> None:
async with Session() as db:
user = await get_user(db, user_id)
await send_welcome_email(user.email)
@app.post("/user")
async def post__create_user__background_tasks(
background_tasks: BackgroundTasks,
) -> User:
async with Session() as db:
user = await create_user(db)
background_tasks.add_task(send_welcome_email_task_bg, user.id)
return user
# > Hatchet Task
class WelcomeEmailInput(BaseModel):
user_id: int
@hatchet.task(input_validator=WelcomeEmailInput)
async def send_welcome_email_task_hatchet(
input: WelcomeEmailInput, _ctx: Context
) -> None:
async with Session() as db:
user = await get_user(db, input.user_id)
await send_welcome_email(user.email)
@app.post("/user")
async def post__create_user__hatchet() -> User:
async with Session() as db:
user = await create_user(db)
await send_welcome_email_task_hatchet.aio_run(
WelcomeEmailInput(user_id=user.id),
wait_for_result=False,
)
return user