<@ULVA73B9P> I have a code snippet like the follow...
# ask-marvin
p
@Marvin I have a code snippet like the following and
my_task_failure_hook
does not get triggered when I run
my_flow
. Any thoughts why?
Copy code
def my_task_failure_hook(task, task_run, state):
    print(f"*******failure hook state: {state}, task run is {task_run} and params are {task_run.parameters}***********")
    
    
@task(retries=5, retry_delay_seconds=3, on_failure=[my_task_failure_hook])
def my_task(parameter):
    raise ValueError

@flow()
def my_flow(parameter):
    parameter = my_task(parameter)
    return parameter
m
It seems like you're using Prefect for your workflow orchestration. The issue you're facing might be due to the way you've defined your failure hook. In Prefect, hooks are functions that get called when certain events happen. For your case, you've defined a failure hook that should be called when a task fails. However, the hook function needs to be defined with a specific signature to work properly. The
my_task_failure_hook
function should be defined with the following signature:
my_task_failure_hook(task: Task, old_state: State, new_state: State) -> State
. The function takes three arguments: the task that is changing state, the state the task is changing from, and the state the task is changing to. The function should return a state which the task will be set to. Here's how you can modify your code:
Copy code
from prefect import task, Flow, State
from prefect.engine.state import Failed

def my_task_failure_hook(task, old_state, new_state):
    if isinstance(new_state, Failed):
        print(f"****failure hook state: {new_state}, task run is {task}********")
    return new_state

@task(retries=5, retry_delay_seconds=3, on_failure=[my_task_failure_hook])
def my_task(parameter):
    raise ValueError

with Flow("my_flow") as my_flow:
    parameter = my_task('some_parameter')
In this code, the
my_task_failure_hook
function checks if the new state is a
Failed
state, and if so, it prints the failure message. The function then returns the new state, which is the state that the task will be set to. Please try this and let me know if it works for you.