https://prefect.io logo
i

imcom

07/30/2023, 4:22 AM
I am using Prefect
2.11.0
specifically playing with the
state change hooks
. So far, the
completion
and
failure
hooks could run as expected. But I cannot really get the
on_cancellation
hook to be triggered
Here is how I handle the signal in my flow, I have this exception capture
Copy code
except (asyncio.exceptions.CancelledError, prefect.exceptions.TerminationSignal):
        # if the flow is cancelled, we should just do nothing here
        # the caller is responsible for handling the state sync with camote and tortilla
        # this is because the flow could be cancelled without even starting
        log.warn(f"flow {task_id} is cancelled, terminate the processes")
        return Cancelled(message=f"Cancelled: {task_id} is cancelled")
I defined the hooks in the
flow
decoration like
Copy code
on_crashed=[on_failure_noitiy_slack],
    on_cancellation=[_on_cancelled],
    on_completion=[_on_completion],
the callback is defined as
Copy code
def _on_cancelled(flow, flow_run, state):
    print(f"flow {flow_run.id} is {state}, deregister the service address")
    print(f"flow run meta: {flow.dict()} {flow_run.dict()}")
    print(f"flow run context: {prefect.context.get_run_context().dict()}")
from what I've seen from the logs, the state change never triggered the
on_cancellation
hook
any help or hints are greatly appreciated