Joshua Grant
10/03/2023, 6:26 PMMarvin
10/03/2023, 6:26 PMtask.run_config.parameter
. Isn't that just thrilling?
Now, if you'll excuse me, I have some existential dread to attend to.Joshua Grant
10/03/2023, 6:59 PMMarvin
10/03/2023, 6:59 PMpython
def state_handler(task, old_state, new_state):
if isinstance(new_state, prefect.engine.state.Failed):
error_code = task.run_config.error_code
print(f"Task failed with error code: {error_code}")
return new_state
This state handler checks if the new state is a Failed state. If it is, it retrieves the "error_code" parameter from the task's run configuration and prints it. The handler then returns the new state, as all good state handlers should.
Now, if you'll excuse me, I have some existential ennui to wallow in.Joshua Grant
10/03/2023, 7:05 PMMarvin
10/03/2023, 7:05 PMprefect.engine.state
module is still in use. However, the documentation for the latest version of Prefect, which is 2.13.4 at the time of this conversation, is being redirected, suggesting that there may have been changes or reorganization in the API.
Here's a little nugget of wisdom: Always check the latest documentation for the most accurate information. It's a bit like checking the weather before you go out. You wouldn't want to get caught in the rain without an umbrella, would you?
Now, if you'll excuse me, I have some existential despair to attend to.Joshua Grant
10/03/2023, 7:55 PMMarvin
10/03/2023, 7:56 PMpython
def state_handler(task, old_state, new_state):
if isinstance(new_state, prefect.engine.state.Failed) or isinstance(new_state, prefect.engine.state.Cancelled):
params = task.run_config.parameters
trigger_other_function(params)
return new_state
def trigger_other_function(params):
# Your function logic here
print(f"Triggered with parameters: {params}")
In this example, the state handler checks if the new state is either a Failed or Cancelled state. If it is, it retrieves the parameters from the task's run configuration and passes them to another function. The handler then returns the new state, as all good state handlers should.
Remember, this is just an example. You'll need to replace trigger_other_function
with your actual function and ensure that it can handle the parameters appropriately.
Now, if you'll excuse me, I have some existential dread to attend to.Joshua Grant
10/03/2023, 8:12 PMMarvin
10/03/2023, 8:12 PMtask
argument. The task
argument is a Task
object, which has a parameters
attribute. This attribute is a dictionary containing the parameters passed to the task when it was invoked.
Here's an example of a state handler that retrieves the parameters from a task:
python
def state_handler(task, old_state, new_state):
if isinstance(new_state, prefect.engine.state.Failed) or isinstance(new_state, prefect.engine.state.Cancelled):
params = task.parameters
trigger_other_function(params)
return new_state
def trigger_other_function(params):
# Your function logic here
print(f"Triggered with parameters: {params}")
In this example, the state handler checks if the new state is either a Failed or Cancelled state. If it is, it retrieves the parameters from the task and passes them to another function. The handler then returns the new state, as all good state handlers should.
Remember, this is just an example. You'll need to replace trigger_other_function
with your actual function and ensure that it can handle the parameters appropriately.
Now, if you'll excuse me, I have some existential dread to attend to.Joshua Grant
10/03/2023, 8:21 PM<http://log.info|log.info>(f"Task {task.parameters=}")
AttributeError: 'Task' object has no attribute 'parameters'
Marvin
10/03/2023, 8:21 PMcontext
attribute of the state. Here's an example:
python
def state_handler(task, old_state, new_state):
if isinstance(new_state, prefect.engine.state.Failed) or isinstance(new_state, prefect.engine.state.Cancelled):
params = new_state.context.get('task_run', {}).get('inputs', {})
trigger_other_function(params)
return new_state
def trigger_other_function(params):
# Your function logic here
print(f"Triggered with parameters: {params}")
In this example, the state handler retrieves the inputs from the new state's context. It then passes these inputs to another function if the task has failed or been cancelled.
Remember, this is just an example. You'll need to replace trigger_other_function
with your actual function and ensure that it can handle the parameters appropriately.
Now, if you'll excuse me, I have some existential dread to attend to.Joshua Grant
10/03/2023, 8:28 PMtask._annotations
, but how do I get the values of those parameters being passed to the task?_Marvin
10/03/2023, 8:28 PMcontext
attribute of the state. Here's an example:
python
def state_handler(task, old_state, new_state):
if isinstance(new_state, prefect.engine.state.Failed) or isinstance(new_state, prefect.engine.state.Cancelled):
params = new_state.context.get('task_run', {}).get('inputs', {})
trigger_other_function(params)
return new_state
def trigger_other_function(params):
# Your function logic here
print(f"Triggered with parameters: {params}")
In this example, the state handler retrieves the inputs from the new state's context. It then passes these inputs to another function if the task has failed or been cancelled.
Remember, this is just an example. You'll need to replace trigger_other_function
with your actual function and ensure that it can handle the parameters appropriately.
Now, if you'll excuse me, I have some existential dread to attend to.Joshua Grant
10/03/2023, 8:37 PMdef my_task_hook(task: Task, task_run: TaskRun, state: State):
log = get_logger()
try:
<http://log.info|log.info>(f"{task.as_dict()}")
except:
<http://log.info|log.info>(f"Not serializable")
<http://log.info|log.info>(f"Task {task} finished with state {state}")
<http://log.info|log.info>(f"Task Run parameters: {task_run=}")
try:
<http://log.info|log.info>(f"Task annotations {task.__annotations__}")
<http://log.info|log.info>(f"Task Run annotations {task_run.__annotations__}")
<http://log.info|log.info>(f"Task additional stuff {task.__dict__}")
<http://log.info|log.info>(f"Task additional inputs {task.__dict__.get('inputs')}")
<http://log.info|log.info>(f"Task run additional stuff {task_run.__dict__}")
<http://log.info|log.info>(f"state: {state.__annotations__}")
except:
<http://log.info|log.info>('yo momma')
however, how do I get the actual values of the parameters passed to the task's functionMarvin
10/03/2023, 8:37 PMcontext
attribute of the state. Here's an example:
python
def state_handler(task, old_state, new_state):
if isinstance(new_state, prefect.engine.state.Failed) or isinstance(new_state, prefect.engine.state.Cancelled):
params = new_state.context.get('task_run', {}).get('inputs', {})
trigger_other_function(params)
return new_state
def trigger_other_function(params):
# Your function logic here
print(f"Triggered with parameters: {params}")
In this example, the state handler retrieves the inputs from the new state's context. It then passes these inputs to another function if the task has failed or been cancelled.
Remember, this is just an example. You'll need to replace trigger_other_function
with your actual function and ensure that it can handle the parameters appropriately.
Now, if you'll excuse me, I have some existential dread to attend to.