is this the right way to hook a `on_cancellation` ...
# ask-community
t
is this the right way to hook a
on_cancellation
handler? I could cancel the flow in the UI, but the handler doesn't run
Copy code
def on_cancellation(flow, run, state):
    logger = get_run_logger()
    <http://logger.info|logger.info>("cancellation handler running")

@task
def dummy_task():
    while True:
        sleep(1)
        print("sleeping...")

@flow(on_cancellation=[on_cancellation], log_prints=True)
def test_console_flow():
    dummy_task()
b
Hey Tri! I got this to work for me:
Copy code
from prefect import flow
from prefect.logging.loggers import flow_run_logger
import time

def log_something(flow, flow_run, state):
    flow_run_logger(flow_run, flow).info(f"Hey there Tri, flow {flow.name} cancelled")

@flow(on_cancellation=[log_something])
def test_flow():
    time.sleep(30)
    return 42

if __name__=="__main__":
    test_flow.serve(name="my-cancelled-flow")
t
i run your code, and it still give the same error as running mine. I'm running Worker as a local process
prefect.flow_runs.worker - Process 43848 exited with status code: 3221225786; Process was terminated due to a Ctrl+C or Ctrl+Break signal. Typically, this is caused by manual cancellation.