https://prefect.io logo
Title
t

Tim Enders

10/24/2022, 2:08 PM
Trying to calculate if a task has failed and I am doing it all wrong. How would I go about it? Right now I have
isinstance(blah, Failed)
but Failed isn't a type. Here is a code snippet:
if isinstance(bq_result, LoadJob) and bq_result.state == "DONE":
    return Completed(message="Load Finished!")
elif isinstance(bq_result, Failed):
    slack_webhook_block = SlackWebhook.load("data-pipeline-notifications")
    slack_webhook_block.notify("Hello from Prefect 2.0!")
    return bq_result
else:
1
r

Rob Freedy

10/24/2022, 2:45 PM
Hey @Tim Enders!! Are you trying to send a notification on task failure? If so I would recommend taking a look at this discourse post: https://discourse.prefect.io/t/how-to-take-action-on-a-state-change-of-a-task-run-task-level-state-handler/82#prefect-20-1
z

Zanie

10/24/2022, 3:24 PM
Failed
is a convenience function for creating a `State(type=StateType.FAILED)`; you can check if a state is failed with
state.is_failed()
t

Tim Enders

10/24/2022, 6:42 PM
Hmmm, I am getting this exception and I think it is because of the new state handling? Right?
<class 'prefect.exceptions.MissingResult'>: State data is missing. Typically, this occurs when result persistence is disabled and the state has been retrieved from the API.
z

Zanie

10/24/2022, 6:55 PM
Yeah. I can investigate attaching some data to user-returned states, but we use
None
to tell if data has been persisted now.
You can attach
data="something"
to your
Completed
state and it should work. Or
data=LiteralResult(value=None)
t

Tim Enders

10/24/2022, 7:00 PM
Thanks. I am no longer getting the error so I am not sure what exactly happened, but I will remember this when/if if comes up again.