Davoud Hejazi
02/01/2022, 9:22 PMfrom prefect.utilities.notifications import slack_notifier
from prefect.engine.state import Failed
SLACK_WEBHOOK_URL = "the slack secret webhook url"
# os.environ["PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL"] = "<https://hooks.slack.com/services/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>"
handler = slack_notifier(only_states=[Failed])
prefect.context.setdefault("secrets", {})
prefect.context.secrets["SLACK_WEBHOOK_URL"] = "the slack secret webhook url"
I have even created config.toml file in .predict directory with the content below:
[context.secrets]
SLACK_WEBHOOK_URL = "the slack secret webhook url"
My task stats with:
@task(nout=2, max_retries=1, retry_delay=timedelta(seconds=1), checkpoint=False, state_handlers=[handler])
I also tried to add prefect app to slack. But when running my flows I am seeing this error (part of the error): see thread
To make it clear, when I remove the state_handlers=[handler]
from my task definition the flow runs fine. When I visit the prefect app in slack I see this too, which I am not sure if it connected to my problem or not. Any comments on how I can solve the slack notification problem?Kevin Kho
config.toml
first? The os.environ won’t help because Prefect has already initialized the context by that point. You also can’t mutate the context like the following lines. For now, I would just try:
from prefect.utilities.notifications import slack_notifier
from prefect.engine.state import Failed
handler = slack_notifier(only_states=[Failed])
and then you can see if your secret is pullable with
Secret("SLACK_WEBHOOK_URL").get()
and try printing the value of that.
Do you get the error from flow.run()
or running with an agent?Kevin Kho
Davoud Hejazi
02/01/2022, 9:33 PMKevin Kho
Davoud Hejazi
02/01/2022, 9:34 PM└── 16:12:20 | ERROR | Unexpected error while calling state handlers: ValueError('Local Secret "SLACK_WEBHOOK_URL" was not found.')
Traceback (most recent call last):
File "/home/davoud/.local/share/virtualenvs/S3-reorganizing-_0Ro3yi6/lib/python3.8/site-packages/prefect/engine/runner.py", line 161, in handle_state_change
new_state = self.call_runner_target_handlers(old_state, new_state)
File "/home/davoud/.local/share/virtualenvs/S3-reorganizing-_0Ro3yi6/lib/python3.8/site-packages/prefect/engine/task_runner.py", line 113, in call_runner_target_handlers
new_state = handler(self.task, old_state, new_state) or new_state
File "/home/davoud/.local/share/virtualenvs/S3-reorganizing-_0Ro3yi6/lib/python3.8/site-packages/toolz/functoolz.py", line 306, in __call__
return self._partial(*args, **kwargs)
File "/home/davoud/.local/share/virtualenvs/S3-reorganizing-_0Ro3yi6/lib/python3.8/site-packages/prefect/utilities/notifications/notifications.py", line 295, in slack_notifier
str, prefect.client.Secret(webhook_secret or "SLACK_WEBHOOK_URL").get()
File "/home/davoud/.local/share/virtualenvs/S3-reorganizing-_0Ro3yi6/lib/python3.8/site-packages/prefect/client/secrets.py", line 167, in get
raise ValueError(
ValueError: Local Secret "SLACK_WEBHOOK_URL" was not found.
Davoud Hejazi
02/01/2022, 9:37 PMKevin Kho
import os
os.environ["SLAC_WEBHOOK_URL"] = ...
#prefect imports
because one it gets imported, it can’t be mutatedDavoud Hejazi
02/01/2022, 9:41 PMimport os
os.environ["SLAC_WEBHOOK_URL"] = ...
but after importing prefectDavoud Hejazi
02/01/2022, 9:42 PMimport os
os.environ["SLAC_WEBHOOK_URL"] = ...
almost before the rest of the packages? In the future we will shift the code and components into AWS, so I want to be independent from my local variables.Kevin Kho
import os
os.environ["PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL"] = ...
Davoud Hejazi
02/01/2022, 9:44 PMKevin Kho
import os
os.environ["PREFECT__CONTEXT__SECRETS__SOME_SECRET"] = "test"
import prefect
print(prefect.context.secrets.get("SOME_SECRET"))
Kevin Kho
Davoud Hejazi
02/01/2022, 9:47 PMDavoud Hejazi
02/01/2022, 9:47 PMKevin Kho
Davoud Hejazi
02/01/2022, 9:48 PMDavoud Hejazi
02/01/2022, 9:50 PM((S3-reorganizing) ) davoud@TitanLenovoL1501:~/s3$ prefect run --path test.py
Retrieving local flow... Error
Traceback (most recent call last):
File "/home/davoud/.local/share/virtualenvs/S3-reorganizing-_0Ro3yi6/lib/python3.8/site-packages/prefect/cli/run.py", line 75, in try_error_done
yield
File "/home/davoud/.local/share/virtualenvs/S3-reorganizing-_0Ro3yi6/lib/python3.8/site-packages/prefect/cli/run.py", line 589, in run
flow = get_flow_from_path_or_module(path=path, module=module, name=name)
File "/home/davoud/.local/share/virtualenvs/S3-reorganizing-_0Ro3yi6/lib/python3.8/site-packages/prefect/cli/run.py", line 173, in get_flow_from_path_or_module
flows = load_flows_from_script(path) if path else load_flows_from_module(module)
File "/home/davoud/.local/share/virtualenvs/S3-reorganizing-_0Ro3yi6/lib/python3.8/site-packages/prefect/cli/run.py", line 121, in load_flows_from_script
namespace = runpy.run_path(path, run_name="<flow>")
File "/home/davoud/.pyenv/versions/3.8.11/lib/python3.8/runpy.py", line 265, in run_path
return _run_module_code(code, init_globals, run_name,
File "/home/davoud/.pyenv/versions/3.8.11/lib/python3.8/runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "/home/davoud/.pyenv/versions/3.8.11/lib/python3.8/runpy.py", line 87, in _run_code
exec(code, run_globals)
File "test.py", line 5, in <module>
print(prefect.context.secrets.get("SOME_SECRET"))
AttributeError: 'Context' object has no attribute 'secrets'
Kevin Kho
export PREFECT___CLOUD____USE__LOCAL_SECRETS=false
and it will pull the Cloud secret for local runs as well. When you run with an agent on AWS, the default gets set to trueKevin Kho
Kevin Kho
Found no flows at xxx.py.
I was intending for you to do python xxx.py
Davoud Hejazi
02/01/2022, 9:52 PMKevin Kho
Davoud Hejazi
02/01/2022, 9:55 PMKevin Kho
config.toml
)Davoud Hejazi
02/01/2022, 9:56 PMKevin Kho
USE_LOCAL_SECRETS
env variable above and it will pull the SLACK_WEBHOOK_URL
secret stored in Prefect Cloud and that experience should be the same on AWS and on localDavoud Hejazi
02/01/2022, 10:01 PMDavoud Hejazi
02/01/2022, 10:02 PMexport PREFECT___CLOUD____USE__LOCAL_SECRETS=false
Kevin Kho
export PREFECT___CLOUD____USE__LOCAL_SECRETS=false
3. Run the flow as isDavoud Hejazi
02/01/2022, 10:12 PMimport os
os.environ["PREFECT__CONTEXT__SECRETS__SLACK_WEBHOOK_URL"] = "<https://hooks.slack.com/services/> ... the rest of url"
from prefect import task, Flow, unmapped, Parameter
---- and some more imports here ----
handler = slack_notifier(only_states=[Failed])
# export PREFECT__CLOUD__USE_LOCAL_SECRETS=false
Then there are flows, and finally I run it using:
prefect run --path mycode.py
I am not sure why I see the same errorKevin Kho
Davoud Hejazi
02/01/2022, 10:13 PMKevin Kho
prefect run
well enough. Using flow.run()
with python myflow.py
will work for this.Kevin Kho
Davoud Hejazi
02/01/2022, 10:19 PMflow.run()
and then python myflow.py
actually sends the error notifications to slack!Davoud Hejazi
02/01/2022, 10:21 PMhandler = slack_notifier(only_states=[Failed])
if I add export PREFECT__CLOUD__USE_LOCAL_SECRETS=false
it will try to use cloud secret key?Kevin Kho
import prefect
Loads in and creates the context. But for the prefect run …
CLI command, it creates a new flow run with a new context here so the context from the os.environ
does not carry over.Kevin Kho
Davoud Hejazi
02/01/2022, 10:24 PMKevin Kho
Kevin Kho
def message_on_err(obj, old_state, new_state):
msg = "Err running task"
if new_state.is_failed():
send_message = SlackTask(webhook_secret="SLACK_WEBHOOK_URL")
send_message.run(message=msg)
@task(state_handlers=[message_on_err])
def abc(x):
return x
Kevin Kho
prefect.context.get("task_name")
or something like that and add it to the message. You can pull anything from the Context in the state handler to construct the messageDavoud Hejazi
02/01/2022, 11:26 PMexport PREFECT__CLOUD__USE_LOCAL_SECRETS = False
I am getting error below:
export PREFECT__CLOUD__USE_LOCAL_SECRETS = False
^
SyntaxError: invalid syntax
Kevin Kho
Kevin Kho