Madhup Sukoon
03/17/2022, 8:06 AMfrom prefect.utilities.notifications import slack_notifier
.
.
flow.state_handlers = [slack_notifier()]
I have added the prefect app and also added the webhook URL to the SLACK_WEBHOOK_URL
secret. Any pointers on why this is happening / how to debug?Anna Geller
03/17/2022, 9:12 AMflow.set_reference_tasks([task1, task2])
If you want the Slack alert to fire on a specific task's failure, then attach it to a task rather than flow.
Lastly as described in the link, it's more flexible to use SlackTask
than
prefect.utilities.notifications.slack_notifier
If you want to use slack_notifier
(or any other state handler function), you don't call it, but rather pass it as callable, so instead of
from prefect.utilities.notifications import slack_notifier
flow.state_handlers = [slack_notifier()]
Try [slack_notifier]
(no round brackets):
from prefect.utilities.notifications import slack_notifier
flow.state_handlers = [slack_notifier]
Madhup Sukoon
03/17/2022, 10:32 AMslack_notifier
and pass it as a callable, but then how would I add parameters like ignore_states=[state.Running]
?slack_notifier
, I also wanted to use jira_notifier
to create tickets for failed flows, and I need to provide only_states
and options
arguments to it.Anna Geller
03/17/2022, 11:30 AMfrom prefect import task
from prefect.engine.state import Running
from prefect.utilities.notifications import slack_notifier
handler = slack_notifier(only_states=[Running])
@task(state_handlers=[handler])
def add(x, y):
return x + y
Madhup Sukoon
03/17/2022, 11:50 AMflow.state_handlers = [slack_notifier()]
What you are suggesting, when used imperatively for a flow, becomes:
flow.state_handlers = [slack_notifier(only_states=[Running])]
Wouldn't this also result in the slack_notifier
function being called, instead of being passed as a callable?flow.state_handlers = [slack_notifier]
Still didn't work. Any ideas why?Anna Geller
03/17/2022, 1:48 PMSlackTask
instead which may be much easier and customizable e.g.:
import prefect
from prefect import task, Flow
from prefect.tasks.notifications import SlackTask
from typing import cast
def post_to_slack_on_failure(task, old_state, new_state):
if new_state.is_failed():
if isinstance(new_state.result, Exception):
value = "```{}```".format(repr(new_state.result))
else:
value = cast(str, new_state.message)
msg = (
f"The task `{prefect.context.task_name}` failed "
f"in a flow run {prefect.context.flow_run_id} "
f"with an exception {value}"
)
SlackTask(message=msg).run()
return new_state
@task(state_handlers=[post_to_slack_on_failure])
def divide_numbers(a, b):
return 1 / (b - a)
with Flow(name="flow_failing_with_bad_parameters") as flow:
result = divide_numbers(1, 1)
if __name__ == "__main__":
flow.run()
Madhup Sukoon
03/17/2022, 1:49 PMKevin Kho
03/17/2022, 1:51 PMMadhup Sukoon
03/17/2022, 1:52 PMslack_notifier
and jira_notifier
to work?Kevin Kho
03/17/2022, 1:53 PMMadhup Sukoon
03/17/2022, 1:55 PMAnna Geller
03/17/2022, 1:55 PMKevin Kho
03/17/2022, 1:57 PMMadhup Sukoon
03/17/2022, 1:58 PMflow.state_handlers = [
slack_notifier(ignore_states=[state.Running]),
jira_notifier(
only_states=[state.Failed],
options={
'project': 'XXXXXXX',
'issuetype': {'name': 'Task'},
'description': f'Flow Failed : {project_name}.{flow_name}'},
)
]
Kevin Kho
03/17/2022, 2:00 PMMadhup Sukoon
03/17/2022, 2:00 PMSLACK_WEBHOOK_URL
secret containing the slack webhook URL (I know this is okay because this works with Automations)JIRASECRETS
JSON secret with the 3 required keys.Kevin Kho
03/17/2022, 2:32 PMMadhup Sukoon
03/17/2022, 2:33 PMKevin Kho
03/17/2022, 2:42 PMfrom prefect import Flow, task
import prefect
from prefect.utilities.notifications import slack_notifier
@task(state_handlers=[slack_notifier])
def test(x):
<http://prefect.context.logger.info|prefect.context.logger.info>(f"test-{x}")
return x+1
with Flow("processes") as flow:
test(1)
flow.run()
Madhup Sukoon
03/17/2022, 2:43 PMKevin Kho
03/17/2022, 2:44 PM[context.secrets]
SLACK_WEBHOOK_URL = "<https://hooks.slack.com/services/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>"
Madhup Sukoon
03/17/2022, 2:45 PMKevin Kho
03/17/2022, 4:05 PMMadhup Sukoon
03/17/2022, 4:06 PMKevin Kho
03/17/2022, 5:25 PM