How to respond to state changes
Learn how to execute code in response to entering states in your workflows.
Available state change hooks
Type | Flow | Task | Description |
---|---|---|---|
on_completion | ✓ | ✓ | Executes when a flow or task run enters a Completed state. |
on_failure | ✓ | ✓ | Executes when a flow or task run enters a Failed state. |
on_cancellation | ✓ | - | Executes when a flow run enters a Cancelling state. |
on_crashed | ✓ | - | Executes when a flow run enters a Crashed state. |
on_running | ✓ | - | Executes when a flow run enters a Running state. |
Note that the on_rollback
hook for tasks is not a proper state change hook but instead
is a transaction lifecycle hook.
Rollback hooks accept one argument representing the transaction for the task.
Send a notification when a workflow run fails
To send a notification when a flow or task run fails, you can specify a on_failure
hook.
Retries are configured in this example, so the on_failure
hook will not run until all retries
have completed and the flow run enters a Failed
state.
State change hooks execute client side
State change hooks run in the same process as your workflow and execution cannot be guaranteed. For more robust execution of logic in response to state changes, use an Automation.
Pass kwargs
to state change hooks
You can compose the with_options
method to effectively pass arbitrary **kwargs
to your hooks: