Files
hatchet/examples/python/conditions/worker.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

164 lines
3.5 KiB
Python

# > Create a workflow
import random
from datetime import timedelta
from pydantic import BaseModel
from hatchet_sdk import (
Context,
EmptyModel,
Hatchet,
ParentCondition,
SleepCondition,
UserEventCondition,
or_,
)
hatchet = Hatchet()
class StepOutput(BaseModel):
random_number: int
class RandomSum(BaseModel):
sum: int
task_condition_workflow = hatchet.workflow(name="TaskConditionWorkflow")
# > Add base task
@task_condition_workflow.task()
def start(input: EmptyModel, ctx: Context) -> StepOutput:
return StepOutput(random_number=random.randint(1, 100))
# > Add wait for sleep
@task_condition_workflow.task(
parents=[start], wait_for=[SleepCondition(timedelta(seconds=10))]
)
def wait_for_sleep(input: EmptyModel, ctx: Context) -> StepOutput:
return StepOutput(random_number=random.randint(1, 100))
# > Add skip condition override
@task_condition_workflow.task(
parents=[start, wait_for_sleep],
skip_if=[ParentCondition(parent=start, expression="output.random_number > 0")],
)
def skip_with_multiple_parents(input: EmptyModel, ctx: Context) -> StepOutput:
return StepOutput(random_number=random.randint(1, 100))
# > Add skip on event
@task_condition_workflow.task(
parents=[start],
wait_for=[SleepCondition(timedelta(seconds=30))],
skip_if=[UserEventCondition(event_key="skip_on_event:skip")],
)
def skip_on_event(input: EmptyModel, ctx: Context) -> StepOutput:
return StepOutput(random_number=random.randint(1, 100))
# > Add branching
@task_condition_workflow.task(
parents=[wait_for_sleep],
skip_if=[
ParentCondition(
parent=wait_for_sleep,
expression="output.random_number > 50",
)
],
)
def left_branch(input: EmptyModel, ctx: Context) -> StepOutput:
return StepOutput(random_number=random.randint(1, 100))
@task_condition_workflow.task(
parents=[wait_for_sleep],
skip_if=[
ParentCondition(
parent=wait_for_sleep,
expression="output.random_number <= 50",
)
],
)
def right_branch(input: EmptyModel, ctx: Context) -> StepOutput:
return StepOutput(random_number=random.randint(1, 100))
# > Add wait for event
@task_condition_workflow.task(
parents=[start],
wait_for=[
or_(
SleepCondition(duration=timedelta(minutes=1)),
UserEventCondition(event_key="wait_for_event:start"),
)
],
)
def wait_for_event(input: EmptyModel, ctx: Context) -> StepOutput:
return StepOutput(random_number=random.randint(1, 100))
# > Add sum
@task_condition_workflow.task(
parents=[
start,
wait_for_sleep,
wait_for_event,
skip_on_event,
left_branch,
right_branch,
],
)
def sum(input: EmptyModel, ctx: Context) -> RandomSum:
one = ctx.task_output(start).random_number
two = ctx.task_output(wait_for_event).random_number
three = ctx.task_output(wait_for_sleep).random_number
four = (
ctx.task_output(skip_on_event).random_number
if not ctx.was_skipped(skip_on_event)
else 0
)
five = (
ctx.task_output(left_branch).random_number
if not ctx.was_skipped(left_branch)
else 0
)
six = (
ctx.task_output(right_branch).random_number
if not ctx.was_skipped(right_branch)
else 0
)
return RandomSum(sum=one + two + three + four + five + six)
def main() -> None:
worker = hatchet.worker("dag-worker", workflows=[task_condition_workflow])
worker.start()
if __name__ == "__main__":
main()