Files
hatchet/examples/python/durable_eviction/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

170 lines
4.7 KiB
Python

from __future__ import annotations
import asyncio
from datetime import timedelta
from typing import Any
from hatchet_sdk import Context, DurableContext, EmptyModel, Hatchet, UserEventCondition
from hatchet_sdk.runnables.eviction import EvictionPolicy
from pydantic import BaseModel
hatchet = Hatchet()
EVICTION_TTL_SECONDS = 5
LONG_SLEEP_SECONDS = 15
EVENT_KEY = "durable-eviction:event"
EVICTION_POLICY = EvictionPolicy(
ttl=timedelta(seconds=EVICTION_TTL_SECONDS),
allow_capacity_eviction=True,
priority=0,
)
@hatchet.task()
async def child_task(input: EmptyModel, ctx: Context) -> dict[str, Any]:
"""Simple child that sleeps long enough for the parent's TTL to fire."""
await asyncio.sleep(LONG_SLEEP_SECONDS)
return {"child_status": "completed"}
@hatchet.durable_task(
execution_timeout=timedelta(minutes=5),
eviction_policy=EVICTION_POLICY,
)
async def evictable_sleep(input: EmptyModel, ctx: DurableContext) -> dict[str, Any]:
"""Sleeps long enough for the TTL-based eviction to kick in."""
await ctx.aio_sleep_for(timedelta(seconds=LONG_SLEEP_SECONDS))
return {"status": "completed"}
@hatchet.durable_task(
execution_timeout=timedelta(minutes=5),
eviction_policy=EVICTION_POLICY,
)
async def evictable_wait_for_event(
input: EmptyModel, ctx: DurableContext
) -> dict[str, Any]:
"""Waits for a user event -- long enough for TTL eviction to fire."""
await ctx.aio_wait_for_event(
EVENT_KEY,
"true",
)
return {"status": "completed"}
@hatchet.durable_task(
execution_timeout=timedelta(minutes=5),
eviction_policy=EVICTION_POLICY,
)
async def evictable_child_spawn(
input: EmptyModel, ctx: DurableContext
) -> dict[str, Any]:
"""Spawns a child workflow whose runtime exceeds the eviction TTL."""
child_result = await child_task.aio_run()
return {"child": child_result, "status": "completed"}
class BulkChildTaskInput(BaseModel):
sleep_for: timedelta
@hatchet.task(
input_validator=BulkChildTaskInput,
)
async def bulk_child_task(
input: BulkChildTaskInput, ctx: Context
) -> dict[str, str | int]:
"""Simple child that sleeps long enough for the parent's TTL to fire."""
await asyncio.sleep(input.sleep_for.total_seconds())
return {"sleep_for": int(input.sleep_for.total_seconds()), "status": "completed"}
@hatchet.durable_task(
execution_timeout=timedelta(minutes=5),
eviction_policy=EVICTION_POLICY,
)
async def evictable_child_bulk_spawn(
input: EmptyModel, ctx: DurableContext
) -> dict[str, Any]:
child_results = await child_task.aio_run_many(
[
bulk_child_task.create_bulk_run_item(
input=BulkChildTaskInput(
sleep_for=timedelta(seconds=(EVICTION_TTL_SECONDS + 5) * (i + 1))
),
key=f"child{i}",
)
for i in range(3)
]
)
return {"child_results": child_results}
@hatchet.durable_task(
execution_timeout=timedelta(minutes=5),
eviction_policy=EVICTION_POLICY,
)
async def multiple_eviction(input: EmptyModel, ctx: DurableContext) -> dict[str, Any]:
"""Sleeps twice, expecting eviction+restore after each sleep."""
await ctx.aio_sleep_for(timedelta(seconds=LONG_SLEEP_SECONDS))
await ctx.aio_sleep_for(timedelta(seconds=LONG_SLEEP_SECONDS))
return {"status": "completed"}
CAPACITY_EVICTION_POLICY = EvictionPolicy(
ttl=None,
allow_capacity_eviction=True,
priority=0,
)
CAPACITY_SLEEP_SECONDS = 20
@hatchet.durable_task(
execution_timeout=timedelta(minutes=5),
eviction_policy=CAPACITY_EVICTION_POLICY,
)
async def capacity_evictable_sleep(
input: EmptyModel, ctx: DurableContext
) -> dict[str, Any]:
"""No TTL -- only evictable via capacity pressure (durable_slots=1)."""
await ctx.aio_sleep_for(timedelta(seconds=CAPACITY_SLEEP_SECONDS))
return {"status": "completed"}
@hatchet.durable_task(
execution_timeout=timedelta(minutes=5),
eviction_policy=EvictionPolicy(
ttl=None,
allow_capacity_eviction=False,
priority=0,
),
)
async def non_evictable_sleep(input: EmptyModel, ctx: DurableContext) -> dict[str, Any]:
"""Has eviction disabled -- should never be evicted."""
await ctx.aio_sleep_for(timedelta(seconds=10))
return {"status": "completed"}
def main() -> None:
worker = hatchet.worker(
"eviction-worker",
workflows=[
evictable_sleep,
evictable_wait_for_event,
evictable_child_spawn,
evictable_child_bulk_spawn,
multiple_eviction,
non_evictable_sleep,
child_task,
bulk_child_task,
],
)
worker.start()
if __name__ == "__main__":
main()