Files
hatchet/examples/python/dependency_injection/worker.py
matt f8e5f357d9 Fix: More examples / snippets fixes + rework (#2150)
* feat: start reworking snippets

* feat: start cleaning up gen script

* fix: start updating refs everywhere

* feat: start fixing broken snippet links

* fix: more snippets

* fix: more updates

* chore: lint

* fix: taskfile

* fix: script

* fix: escaping issue + mergent blog

* fix: bunch more

* chore: lint

* fix: implement more of them

* fix: retry

* fix: the rest

* chore: lint

* fix: highlight

* fix: ugh

* fix: start removing dead code from old snippet method

* fix: rest of the refs

* fix: remove all of the rest of the <GithubSnippet uses

* fix: couple more

* fix: last few errors

* fix: handle example writes

* fix: delete to test update

* fix: CI, attempt 1

* feat: helpful error on no snippet

* fix: lint

* chore: rm unused js file

* feat: improve GHA

* debug: run action on branch

* fix: rm pnpm

* fix: try to leave comment instead

* fix: don't run on branch

* fix: factor out GH info

* fix: include code path

* fix: ts
2025-08-17 10:22:16 -05:00

158 lines
3.4 KiB
Python

from typing import Annotated
from pydantic import BaseModel
from hatchet_sdk import Context, Depends, DurableContext, EmptyModel, Hatchet
hatchet = Hatchet(debug=False)
SYNC_DEPENDENCY_VALUE = "sync_dependency_value"
ASYNC_DEPENDENCY_VALUE = "async_dependency_value"
# > Declare dependencies
async def async_dep(input: EmptyModel, ctx: Context) -> str:
return ASYNC_DEPENDENCY_VALUE
def sync_dep(input: EmptyModel, ctx: Context) -> str:
return SYNC_DEPENDENCY_VALUE
class Output(BaseModel):
sync_dep: str
async_dep: str
# > Inject dependencies
@hatchet.task()
async def async_task_with_dependencies(
_i: EmptyModel,
ctx: Context,
async_dep: Annotated[str, Depends(async_dep)],
sync_dep: Annotated[str, Depends(sync_dep)],
) -> Output:
return Output(
sync_dep=sync_dep,
async_dep=async_dep,
)
@hatchet.task()
def sync_task_with_dependencies(
_i: EmptyModel,
ctx: Context,
async_dep: Annotated[str, Depends(async_dep)],
sync_dep: Annotated[str, Depends(sync_dep)],
) -> Output:
return Output(
sync_dep=sync_dep,
async_dep=async_dep,
)
@hatchet.durable_task()
async def durable_async_task_with_dependencies(
_i: EmptyModel,
ctx: DurableContext,
async_dep: Annotated[str, Depends(async_dep)],
sync_dep: Annotated[str, Depends(sync_dep)],
) -> Output:
return Output(
sync_dep=sync_dep,
async_dep=async_dep,
)
@hatchet.durable_task()
def durable_sync_task_with_dependencies(
_i: EmptyModel,
ctx: DurableContext,
async_dep: Annotated[str, Depends(async_dep)],
sync_dep: Annotated[str, Depends(sync_dep)],
) -> Output:
return Output(
sync_dep=sync_dep,
async_dep=async_dep,
)
di_workflow = hatchet.workflow(
name="dependency-injection-workflow",
)
@di_workflow.task()
async def wf_async_task_with_dependencies(
_i: EmptyModel,
ctx: Context,
async_dep: Annotated[str, Depends(async_dep)],
sync_dep: Annotated[str, Depends(sync_dep)],
) -> Output:
return Output(
sync_dep=sync_dep,
async_dep=async_dep,
)
@di_workflow.task()
def wf_sync_task_with_dependencies(
_i: EmptyModel,
ctx: Context,
async_dep: Annotated[str, Depends(async_dep)],
sync_dep: Annotated[str, Depends(sync_dep)],
) -> Output:
return Output(
sync_dep=sync_dep,
async_dep=async_dep,
)
@di_workflow.durable_task()
async def wf_durable_async_task_with_dependencies(
_i: EmptyModel,
ctx: DurableContext,
async_dep: Annotated[str, Depends(async_dep)],
sync_dep: Annotated[str, Depends(sync_dep)],
) -> Output:
return Output(
sync_dep=sync_dep,
async_dep=async_dep,
)
@di_workflow.durable_task()
def wf_durable_sync_task_with_dependencies(
_i: EmptyModel,
ctx: DurableContext,
async_dep: Annotated[str, Depends(async_dep)],
sync_dep: Annotated[str, Depends(sync_dep)],
) -> Output:
return Output(
sync_dep=sync_dep,
async_dep=async_dep,
)
def main() -> None:
worker = hatchet.worker(
"dependency-injection-worker",
workflows=[
async_task_with_dependencies,
sync_task_with_dependencies,
durable_async_task_with_dependencies,
durable_sync_task_with_dependencies,
di_workflow,
],
)
worker.start()
if __name__ == "__main__":
main()