<@ULVA73B9P> in a cacellation hook i dont have the...
# marvin-ai
l
@Marvin in a cacellation hook i dont have the running context
m
thought for 41.3 seconds
Based on the research and available information, I can help explain why you might not have access to the running context in a cancellation hook and provide a solution. Here's what you need to know: 1. In Prefect 3.x, cancellation hooks are executed when a flow run is cancelled, but they operate in a slightly different context than the main flow execution. 2. To access the flow run context in a cancellation hook, you should define your hook like this:
Copy code
python
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?
l
@Marvin yes
m
thought for 4.3 seconds
Could you tell me more specifically what you're trying to do in your cancellation hook? For example: - Are you trying to clean up resources? - Do you need access to flow parameters? - Are you trying to log specific information? This will help me provide a more targeted and useful example that matches your use case. The implementation details can vary depending on what you're trying to achieve within the cancellation hook. Also, it would be helpful to know if you're using synchronous or asynchronous flows, as the implementation might differ slightly between the two. Could you share these details so I can provide a more specific and relevant example?