Guilherme Petris
11/30/2021, 10:38 AMGuilherme Petris
11/30/2021, 10:39 AMSylvain Hazard
11/30/2021, 10:42 AMAnna Geller
export PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL="your_webook_url"
Anna Geller
Guilherme Petris
11/30/2021, 11:05 AMGuilherme Petris
11/30/2021, 11:06 AMAnna Geller
Guilherme Petris
11/30/2021, 11:09 AMAnna Geller
export PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL="your_webook_url"
and then in the same terminal session run:
from prefect.tasks.notifications import SlackTask
SlackTask("Hi Guilherme!").run()
You should get:
If not then either your webhook doesn’t work, or something is wrong with your Prefect installationAnna Geller
Guilherme Petris
11/30/2021, 11:18 AMPython 3.9.7 (default, Oct 13 2021, 06:45:31)
[Clang 13.0.0 (clang-1300.0.29.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from prefect.tasks.notifications import SlackTask
>>> SlackTask("Hi!").run()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/utilities/tasks.py", line 456, in method
return run_method(self, *args, **kwargs)
File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/tasks/notifications/slack_task.py", line 61, in run
webhook_url = webhook_url or cast(str, Secret(webhook_secret).get())
File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/client/secrets.py", line 167, in get
raise ValueError(
ValueError: Local Secret "SLACK_WEBHOOK_URL" was not found.
Anna Geller
from prefect.tasks.notifications import SlackTask
import os
os.environ["PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL"] = "your_url"
SlackTask("Hi Guilherme!").run()
I mean, you can see that something with your local secret is wrong. Are you on Cloud?Anna Geller
Guilherme Petris
11/30/2021, 11:22 AMGuilherme Petris
11/30/2021, 11:22 AMAnna Geller
Guilherme Petris
11/30/2021, 11:23 AMAnna Geller
<http://requests.post|requests.post>(url=webhook_url, data=json.dumps({"text": "your slack message"}))
Guilherme Petris
11/30/2021, 11:25 AMEven with the other code that you sent i'm getting this: Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/utilities/tasks.py", line 456, in method
return run_method(self, *args, **kwargs)
File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/tasks/notifications/slack_task.py", line 61, in run
webhook_url = webhook_url or cast(str, Secret(webhook_secret).get())
File "/Users/guilherme.petris/analytics-toolbox/venv/lib/python3.9/site-packages/prefect/client/secrets.py", line 167, in get
raise ValueError(
ValueError: Local Secret "SLACK_WEBHOOK_URL" was not found.
Anna Geller
prefect backend server
prefect server start
You can also set this env variable to be sure:
export PREFECT__CLOUD__USE_LOCAL_SECRETS=true
Guilherme Petris
11/30/2021, 11:30 AMAnna Geller
<http://requests.post|requests.post>(url=webhook_url, data=json.dumps({"text": "your slack message"}))
Guilherme Petris
11/30/2021, 11:33 AMGuilherme Petris
11/30/2021, 11:37 AMAnna Geller
import pendulum
from typing import cast
import prefect
from prefect import task, Flow
from prefect.engine import signals
from prefect.client import Secret
from prefect.storage import Docker
from prefect.tasks.notifications import SlackTask
def notify_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)
now = pendulum.now().isoformat()
msg = (
f"The task `{prefect.context.task_name}` failed on {now} "
f"in a flow run {prefect.context.flow_run_id} "
f"with an exception `{value}`"
)
# we use Slack here, but it might be Teams or a hook URL to any other messaging service
# webhook_url = Secret("SLACK_WEBHOOK_URL").get()
<http://requests.post|requests.post>(url=webhook_url, data=json.dumps({"text": msg}))
return new_state
@task(state_handlers=[post_to_slack_on_failure])
def fail_successfully(x):
return 1 / x
with Flow(name="state-handler-demo-flow") as flow:
result = fail_successfully(x=0)
Guilherme Petris
11/30/2021, 11:44 AMAnna Geller