Question about mapped tasks and custom state handl...
# ask-community
d
Question about mapped tasks and custom state handlers for them - I've got a state handler for when a mapped task fails, however because the object passed to the state handler is a Task and not a TaskRun, the task.name is the generic task name, and not the mapped task name. Similarly in the state handler I don't have the task_run_id either. Is there any way to get the task_run_name in the state handler for a mapped task? Forgot to mention - if I try
object.task_run_name
it prints the format string used to generate the mapped task name, not the actual task name. I'm using the pattern here where my
task_run_name = "{table_name}"
- and I get
{table_name}
output in the state handler, because that's what it's set as at the Task level, rather than it being computed at the TaskRun level
k
Hey @David Elliott, you can use the
map_index
and the
task_full_name
which includes the
map_index
. Would that work for you? You can also use the task result to see the error message. Is the purpose for debugging?
πŸ‘€ 1
d
I can't see those two options in there - I logged the
dir(object)
passed in the state handler and it gave me basically all the elements of a Task (here) which doesn't include map_index or task_full_name, and on trying to access those keys I get a 'no attribute' error. I think it's probably not possible, would be a nice addition though (to pass in the TaskRun rather than (or in addition to) the Task) it's for outputting to slack but with the specific name of the mapped task
k
It’s not in the task, it will be in the context so
Copy code
prefect.context.get("map_index")
inside the state handler.
d
Ahh let me try that πŸ™
You're a star, thank you @Kevin Kho!! I used the
prefect.context.task_run_id
with GQL to pull the
task_run_name
πŸŽ‰ had totally not considered using the context 🀦
πŸ™ 1
k
Thank you! This is better than just having the map_id in my opinion. If you have this, you should be good.