Hi all, a couple questions about state handlers. 1. I want to run a function f() when a task finish...
m
Hi all, a couple questions about state handlers. 1. I want to run a function f() when a task finishes successfully on retry. In other words, a task goes from failure —> retrying —> running —> success. How can I capture that scenario in a state handler if I only have new_state and old_state? 2. Where can I find a task’s start and end time from within a state handler? For context, I want a state handler to log this information in the event of a task failure. Thanks so much!
k
Hey @Matthew Seligson, you can pull some things from the context that can help you with number 1. I am thinking you can use the condition in
task_run_count
. I don’t think you can get end time because that doesn’t exist yet until the state handler is done. For start time…it may have to be taken from the GraphQL API. I think for end time, using
datetime.datetime.now()
is probably a good proxy
The query for start time would be like this
Copy code
query {
  task_run_by_pk(where: {id: {_eq: "insert_id"}}) {
    start_time
  }
}
and then you can pull
task_run_id
from context, form the query, and pass that to
client.graphql()
inside the state handler
m
Thanks so much @Kevin Kho, and sorry for the slow response. Regarding the start time, how can I open a feature request to add task start time to the task object or prefect context?
k
I honestly don’t think we’d add it. The context is currently copied a couple of times in a Flow run so everything added has the potential to bloat it. So we only have the most common things available + the things needed to actually run the task
u
ƒcelery
k
Oh you can still open a feature request for this @Matthew Seligson, we can see what the core team says