Files
hatchet/python-sdk/examples/cancellation/worker.py
Gabe Ruttner 5066547ce6 feat: cancel in progress (#325)
* chore: bad project path

* fix: remove tickerId for replay

* feat: cancel from request

* feat: cancel button in UI

* chore: rm comment

* fix: build error

* chore: reason case

* chore: reason string

* fix: linting

---------

Co-authored-by: gabriel ruttner <gabe@hatchet.run>
2024-04-02 01:16:27 +00:00

29 lines
723 B
Python

from hatchet_sdk import Hatchet, Context
from dotenv import load_dotenv
import asyncio
load_dotenv()
hatchet = Hatchet(debug=True)
@hatchet.workflow(on_events=["user:create"])
class CancelWorkflow:
def __init__(self):
self.my_value = "test"
@hatchet.step(timeout='10s', retries=1)
async def step1(self, context: Context):
i = 0
while not context.exit_flag.is_set() and i < 20:
print(f"Waiting for cancellation {i}")
await asyncio.sleep(1)
i += 1
if context.exit_flag.is_set():
print("Cancelled")
workflow = CancelWorkflow()
worker = hatchet.worker('test-worker', max_runs=4)
worker.register_workflow(workflow)
worker.start()