lucian
08/01/2025, 4:20 AMMarvin
08/01/2025, 4:20 AMMarvin
08/01/2025, 4:21 AMpython
from prefect import flow, get_run_logger
@flow
def my_flow():
@flow.on_cancellation
def cleanup(flow, flow_run, state):
logger = get_run_logger()
<http://logger.info|logger.info>(f"Flow run {flow_run.id} was cancelled")
# You have access to:
# - flow: the flow object
# - flow_run: the flow run object
# - state: the cancellation state
# Your flow logic here
Key points:
- The cancellation hook receives three parameters: flow
, flow_run
, and state
- You can use get_run_logger()
within the hook to access the logger
- The hook has access to the flow run ID and other metadata through the flow_run
parameter
If you need to access specific context variables or parameters from your flow, you should pass them explicitly to your cancellation hook or store them in a way that's accessible to the hook.
Note: There appears to be some ongoing work and issues related to cancellation hooks in Prefect 3.x (as evidenced by the GitHub issues), so it's important to handle potential edge cases and ensure your cleanup logic is robust.
Would you like me to provide more specific examples based on what you're trying to accomplish in your cancellation hook?lucian
08/01/2025, 4:22 AMMarvin
08/01/2025, 4:22 AMMarvin
08/01/2025, 4:22 AM