Hi All I am trying to set up slack notifications t...
# ask-community
v
Hi All I am trying to set up slack notifications to a channel i created from prefect, I have the SLACK_WEBHOOK_URL saved and it doesn't seem to be working, i'm getting this error:
Copy code
Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000002C254411C10>: Failed to establish a new connection: [WinError 10061] No connection could be made because the target machine actively refused it'))
k
Hi @Vikram Thirumalai! You’re doing this right?
v
yes I have
k
Maybe you can try testing your webhook like this? See if it works outside of Prefect.
Do you have a small Flow you can share about how you’re using it? You’re using the
slack_notifier
?
What agent are you using and run config?
v
Copy code
from prefect import Flow, task
from prefect.utilities.notifications import slack_notifier
from prefect.engine.state import Failed
from prefect.client.secrets import Secret


@task(name="1/x task", state_handlers=[slack_notifier])
def div():
    return 1 / 1


@task(name="Add 1 task", state_handlers=[slack_notifier])
def add(x):
    return x + 1


with Flow('Add Divide Flow') as f:
    res = div()
    print(res)

final_state = f.run()
this is the small snippet that i'm testing
im running this locally
k
Ok I would check the Slack Webhook works outside of Prefect just to be sure.
v
it does
k
That’s odd. And you saved it under
context.secrets
in the
config.toml
? What version of Prefect are you on?
Oh wait sorry. I think you’re using the
slack_notifier
wrong in the code.
Can you try creating the
handler
and passing the
handler
to
@task
like follows:
Copy code
from prefect.utilities.notifications import slack_notifier

handler = slack_notifier(only_states=[Failed]) # we can call it early

@task(state_handlers=[handler])
def add(x, y):
    return x + y
Right now you’re passing the function that was imported.
v
Copy code
handler = slack_notifier()


@task(name="1/x task", state_handlers=[handler])
def div():
    return 1 / 1


@task(name="Add 1 task", state_handlers=[slack_notifier])
def add(x):
    return x + 1


with Flow('Add Divide Flow') as f:
    res = div()
    print(res)

final_state = f.run()
this is what i changed my snippet to and it looks like im still getting the same error as above
oddly enough this happened to work maybe around a month ago but the same code isn't working now
k
I think you need to replace the second task. It still has the same
slack_notifier
v
ah changed the second task and im still getting the error
Copy code
@task(name="Add 1 task", state_handlers=[handler])
def add(x):
    return x + 1
😰 1
k
What version of Prefect are you on? I’ll try to get a working example.
v
the latest version: prefect 0.14.17
k
perfect I’ll message you when I get it to work
v
thank you!
k
I went through it and it works for me. Did you wrap the URL in quotes when you passed it to the
config.toml
?
Copy code
from prefect import Flow, task
from prefect.utilities.notifications import slack_notifier
from prefect.engine.state import Failed
from prefect.client.secrets import Secret

handler = slack_notifier()

@task(name="1/x task", state_handlers=[handler])
def div():
    return 1 / 1

@task(name="Add 1 task", state_handlers=[handler])
def add(x):
    return x + 1
    
with Flow('Add Divide Flow') as f:
    res = div()
    print(res)
    
final_state = f.run()
I think our code is identical
v
yup i have the quotes
k
Can you try running this code?
Copy code
import prefect
x = prefect.context.secrets.get("SLACK_WEBHOOK_URL")
print(x)
I get my URL back when I do this
v
i do as well, i think there may be an issue with my local server start
k
Ok I’m pretty confident you have it configured right.
What you do mean about the local server start?
v
im getting errors now when im running my prefect server start command locally, i'm assuming that has something to do with it
Are there any ways to run the slack_notifier without a prefect server running
k
It should work with just
flow.run()
. The doesn’t use cloud/server as a backend. It just runs on local.