Florent VanDeMoortele
08/20/2025, 12:56 PMMarvin
08/20/2025, 12:56 PMFlorent VanDeMoortele
08/20/2025, 12:58 PMMarvin
08/20/2025, 12:58 PMMarvin
08/20/2025, 12:58 PMMarvin
08/20/2025, 12:59 PMfrom prefect import flow, task
from prefect.blocks.notifications import SlackWebhook
# One-time setup (run once, e.g., in a notebook or script)
# slack_block = SlackWebhook(url="<https://hooks.slack.com/services/XXX/YYY/ZZZ>")
# slack_block.save("alerts", overwrite=True)
@task
def do_thing():
# your task logic
return 42
@flow
def my_flow():
slack = SlackWebhook.load("alerts")
try:
result = do_thing()
# Optional success notification if you really want it
# slack.notify(text="my_flow completed successfully.")
return result
except Exception as e:
slack.notify(text=f"my_flow failed: {e}")
raise
- For a critical task-only alert, wrap that task’s logic similarly (notify in the except
, then re-raise).
- To avoid spam from retries, only notify on the final failure (e.g., after your retries are exhausted) or send at the flow level only.
Noise control tips
- Alert on final states only (Failed/Crashed) and maybe Late.
- If you must do task-level alerts, limit to specific tasks/tags and dedupe retries.
- Include the flow run URL and state message in notifications for fast triage (Cloud automations do this nicely).
If you tell me whether you’re on Cloud or OSS and which channel you want (Slack, email, webhook), I’ll give you the exact clicks or code.
PS: If you’re planning any upgrades, Prefect 3.x improves the notification story and worker model. I won’t pretend it makes alerts exciting, but it does make them easier.