<@ULVA73B9P> what's the best practice : notify for...
# ask-marvin
f
@Marvin what's the best practice : notify for failures or completion from task or from flow?
m
thought for 97.4 seconds
f
@Marvin I work with Prefect 2
m
Short answer: flow-level. Notify on the flow run’s outcome (failed/crashed/late, and only the rare “completed” if it’s genuinely useful). Task-level notifications are for exceptional, high‑impact steps only—otherwise you’ll drown in noise. Recommended approach in Prefect 3.x: - Prefer flow-run notifications via Automations: centralizes alerts, adds context (link to the run, state message), and avoids spamming on every task retry. - Add task-run notifications sparingly: only for “must-know” tasks (e.g., a long ETL load, a payment capture) or when early, granular alerts reduce MTTR. - Avoid in-code notifications unless you need custom formatting or you’re running open-source only; if you do, use notification blocks (e.g., SlackWebhook) rather than hardcoding webhooks. How to set it up: - Prefect Cloud: create an Automation that triggers on Flow Run state entered = Failed/Crashed/Late (optionally Completed) and action = Send notification to your Slack/Email/Webhook. You can scope by flow, tags, or work pool. - In-code option: use notification blocks (Slack webhook, etc.) if you’re not using Cloud Automations or need bespoke messages. See blocks and Slack integration: - Blocks: Notification blocks - Slack integration: prefect-slack General tips: - Alert on failure/crash and “late” starts; keep “completed” to truly critical flows. - Include the flow run URL and state message in notifications for fast triage. - If you must alert on tasks, filter to specific task names or tags and dedupe retries. If you share whether you’re on Prefect Cloud or OSS, and whether you want Slack or email, I can give you the exact steps/templates.
thought for 107.3 seconds
Got it — Prefect 2.x. Best practice: - Default to flow-level notifications. Alert on final flow states (Failed/Crashed; optionally Completed if it’s truly useful). You get one clean signal with full context. - Use task-level notifications sparingly for high-impact steps only (e.g., a critical load or payment). Otherwise you’ll earn a lifetime subscription to alert fatigue. How to do it in Prefect 2.x Prefect Cloud (recommended) - Use Automations to send notifications on flow-run state changes. Configure: - Trigger: Flow run state entered = Failed/Crashed (optionally Late or Completed) - Filters: by flow, tags, work queue/pool, etc. - Action: Send notification to Slack/email/webhook (whatever you’ve connected) - You can also target Task Run states if you truly need task-level alerts—filter by task name or tag to keep it sane. - Docs: - Automations: https://docs-2.prefect.io/latest/concepts/automations/ - States overview: https://docs-2.prefect.io/latest/concepts/states/ Open-source (or custom formatting) - Use notification blocks (e.g., Slack webhook) in code. Create once, then load and notify in your flow. Example:
Copy code
from 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.