Enqueuing a Workflow Run (Fire and Forget)
This example assumes we have a workflow registered on a running worker.
Another method of triggering a workflow in Hatchet is to enqueue the workflow without waiting for it to complete, sometimes known as “fire and forget”. This pattern is useful for workflows that take a long time to complete or are not critical to the immediate operation of your application.
Some example use cases for fire-and-forget style workflows might be:
- Sending a shipping confirmation email to a user once their order has shipped. This is a truly async task, in the sense that the user is not necessarily using your application when it happens, and the part of the application triggering the workflow does not need to know the result of the work, just that it has been enqueued (assuming that it will complete, of course).
- Triggering a machine learning model training job that can take minutes, hours, or even days to complete. Similarly to above, it’s likely that no part of the application needs to wait on the result of this work, it just needs to “fire and forget” it - meaning that it needs to kick it off, and let it complete whenever it completes.
You can use your Workflow
object to run a workflow and “forget” it by calling the run_no_wait
method. This method enqueue a workflow run and return a WorkflowRunRef
, a reference to that run, without waiting for the result.
from src.workflows import my_workflow, MyWorkflowInputModel
ref = my_workflow.run_no_wait(MyWorkflowInputModel(foo="bar"))
You can also await
the result of aio_run_no_wait
:
ref = await my_workflow.aio_run_no_wait(input=MyWorkflowInputModel(foo="bar"))
Note that the type of input
here is a Pydantic model that matches the input schema of your workflow.
Subscribing to results from an enqueued workflow
Often is is useful to subscribe to the results of a workflow at a later time. The run_no_wait
method returns a WorkflowRunRef
object which includes a listener for the result of the workflow.
Use ref.result()
to block until the result is available:
result = ref.result()
or await aio_result
:
result = await ref.aio_result()
Triggering Runs in the Hatchet Dashboard
In the Hatchet Dashboard, you can trigger and view runs for your workflows.
Navigate to “Workflow Runs” in the left sidebar and click “Trigger Run” at the top right.
You can specify run parameters such as Input, Additional Metadata, and the Scheduled Time.