I'm trying to setup a programatic notification on ...
# ask-community
b
I'm trying to setup a programatic notification on slack when my flows fail. I'm looking at the docs for SlackWebhook and they say to use it like so
Copy code
from prefect.blocks.notifications import SlackWebhook

slack_webhook_block = SlackWebhook.load("BLOCK_NAME")
slack_webhook_block.notify("Hello from Prefect!")
this is working for me. I wrote a function to do that as per this thread and added the
on_failure=[notify_slack]
but i'm getting the error (and mypy is showing the same)
Copy code
packages/flows/notify.py", line 7, in notify_slack
    slack_webhook_block.notify(
    ^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'coroutine' object has no attribute 'notify'
I'm curious why this is working when I just run this code directly, but within prefect flow infrastructure it's failing. I'm seeing that in pylance the variable is of type
slack_webhook_block: SlackWebhook | Coroutine[Any, Any, SlackWebhook]
-- so perhaps because my flow is async, this
notify_slack
function needs to be async as well?
so making the function async "worked" in that it's not crashing, but it's also not working haha. Now I have
Copy code
async def notify_slack(flow: Flow, flow_run, state):
    slack_webhook_block = await SlackWebhook.aload("my-block")
    slack_webhook_block.notify("...")
and prefect says its working
Copy code
Hook 'notify_slack' finished running successfully
but i didnt actually get the slack notification
ok it seems like
slack_webhook_block.notify
needs to be awaited. There seems to be a typing issue with prefect here, but perhaps you already know about it based on the comments @Alexander Azzam i feel like you and I have talked about something like this in the past.
fwiw i tried to also go the "official" automations route from the UI, and created the automation, but weirdly it never executed. It said it was working, but the slack message never actually came through... Perhaps something similarly wasn't awaited on the prefect side? Not sure
c
Yea this is a very frustrating piece of tech debt from 2.x lurking in the codebase: essentially there is engine magic that attempts to detect whether there is an available event loop or not, and based on that return a coroutine or make a synchronous call. There's an escape hatch kwarg called
_sync=True/False
that you can use to hardcode deterministic behavior on methods such as this. More info here: https://github.com/PrefectHQ/prefect/issues/15008 It makes type-hinting challenging to say the least!
b
oh interesting, thanks Chris! so am I "safe" to take this route? Or is there a better practice recommendation? Only asking because I couldn't safely tell from the docs
Copy code
from prefect import Flow
from prefect.blocks.notifications import SlackWebhook
from prefect.settings import PREFECT_UI_URL
from prefect.server.schemas.core import FlowRun
from prefect.server.schemas.states import State

async def notify_slack(flow: Flow, flow_run: FlowRun, state: State):
    slack_webhook_block = await SlackWebhook.aload("my-slack-webhook")
    await slack_webhook_block.notify(  # type: ignore
        (
            f"Your job {flow_run.name} entered {state.name} "
            f"with message:\n\n"
            f"See <{PREFECT_UI_URL.value()}/flow-runs/"
            f"flow-run/{flow_run.id}|the flow run in the UI>\n\n"
            f"Tags: {flow_run.tags}\n\n"
            f"Scheduled start: {flow_run.expected_start_time}"
        )
    )
c
yup that's exactly right! At some point we'll introduce
anotify
as well but until then this is the right way to proceed
thank you 1
b
Thanks Chris -- feel free to steal this if you'd like, or I can open another PR to your docs 🙂 https://gist.github.com/Ben-Epstein/782ba98dcae837ab633cfb0e5aa9eecf
c
i meannnnnnnn we do love docs contributions 😉 but no pressure, I'll set a reminder for this and if you haven't gotten around to it I can include it. Thank you!
b
Happy to do it! where would you like to add this? Maybe under this page https://docs.prefect.io/v3/tutorials/alerts#send-alerts-on-failure in the
Platform Engineering
section. LMK if you like that and ill add it
c
Yup that seems like the best place to me 👍
or actually
given that your example is a pure client-side implementation of alerts, it might be better in the "for data engineers" section
🫡 1
b
feel free to edit as needed 🙂 https://github.com/PrefectHQ/prefect/pull/17589
thank you 1