Hi All, what is the best way to send email notific...
# ask-community
m
Hi All, what is the best way to send email notification when flow is completed. Is it state handler?
a
@Milly gupta Yes. You can use flow state handler to send a notification. Prefect also gives you few default notifiers like
gmail_notifier
etc. which can be directly used as a
state_hanfler
argument in your flow definition. You can choose the type of state on which notification should be send. this can be done by passing State value to
only_states
argument in
gmail_notifier
See more about it here: https://docs.prefect.io/api/latest/utilities/notifications.html?#functions You can also write your own flow state handler as per your need (In case you need to add more business logic to your flow state handler) see this doc, which explains how to do so: https://docs.prefect.io/core/concepts/notifications.html#state-handlers
m
Yeah and what's the best way to get flow details in the state handler for e.g when it finished, flow id, flow url?
Prefect context will only be available in the Prefect task.
@Kevin Kho What do you think?
a
You can get the flow details from
prefect.context
and in case you need more detail by flow run wise, you can do an API call through graphql query
m
Thanks Abhishek. Can I access prefect context in state handler?
Yeah graphql is another option, for that I would need to create a Prefect client to make the API calls.
a
Yes you can. also as state handler takes 3 arguments • flow/task object • old state • new state you can get the data from these objects as well. here is an example of one of my flow state handler
let me share the gist
give me a min
m
Sure. I couldn't find end time for finished state.
i use a helper function to query the graphql api
m
Ah this is really helpful
a
see my comment in the gist, it has an implementation of calling graphql query
you need to add import statement
from prefect import Client
i just shared it for reference
the best way to try graphql query is via prefect’s interactive graphql playground. see: https://docs.prefect.io/orchestration/ui/interactive-api.html
k
Abishek is right here that you do have context available in a state handler, but for the end time for the finished state, you might need to hit the GraphQL API in the state handler
m
Great thank you Abhishek.