Notifications¶
A DAG can send notifications about runs, tasks and triggers. A string names a server
channel, configured by the person running the deployment: a webhook’s URL and body, or an
email channel’s recipients. A list
uses the server’s default events (RUN_FAILED unless changed):
with Dag("orders", notify=["ops-slack"]):
...
A dict chooses events for each channel. This example also defines webhooks in the DAG:
"""Notification bodies for a relay and an email service."""
from schedr import Dag, Notify, WebhookChannel, notify, task
relay = WebhookChannel(
"relay",
url="https://relay.example.com/schedr",
body={"text": t"{notify.dag}: {notify.event}", "failures": notify.failed_tasks},
)
plain_email = WebhookChannel(
"plain-email",
url="https://relay.example.com/mail/send",
body={
"to": ["ops@example.com"],
"subject": t"{notify.dag}: {notify.event}",
"text": notify.text,
},
)
template_email = WebhookChannel(
"template-email",
url="https://relay.example.com/mail/template",
body={
"to": ["ops@example.com"],
"template_id": "run-alert",
"variables": {"dag": notify.dag, "message": notify.text},
},
)
@task
def prepare() -> str:
return "ready"
with Dag(
"notifications_demo",
description="Sends notifications to Slack and email when a run fails or recovers.",
notify={
"ops-slack": [Notify.RUN_FAILED, Notify.RUN_RECOVERED],
relay: [Notify.TASK_FAILED],
plain_email: [Notify.RUN_FAILED],
template_email: [Notify.RUN_FAILED],
},
):
prepare()
|
When it fires |
|---|---|
|
A run ends failed, including a cancellation with a failure. |
|
A run is cancelled without a failure. |
|
A run ends successfully. |
|
A run succeeds after the DAG’s previous run failed. |
|
A task fails after its last retry. |
|
An attempt fails and will be retried. |
|
A trigger has |
|
Cleanup after the run ended fails. |
A WebhookChannel posts JSON to its URL, which must be within the operator’s
[notifications] dag_urls. Keep secrets out of the URL: it is in the DAG file and its
JSON. Slack and Teams webhook URLs are secrets; ask the operator for a server channel.
A string names a server webhook or email channel. For example,
notify={"oncall": [Notify.RUN_FAILED]} sends that event to the operator’s oncall channel.
There is no EmailChannel class: the operator chooses the recipients.
An email is plain text, with the summary as its subject and details in its body. For example:
Subject: DAG `orders`: run run-123 failed (failed: prepare)
DAG `orders`: run run-123 failed (failed: prepare)
Event: RUN_FAILED
DAG: orders
Run: run-123
Planned for: 2026-11-09T02:00:00Z (2026-11-09 03:00 Europe/Warsaw)
At: 2026-11-09T03:00:12Z (2026-11-09 04:00 Europe/Warsaw)
Link: https://schedr.example.com/runs/run-123
Failed tasks:
prepare: connection failed
Bodies¶
A DAG webhook’s body can contain JSON values, placeholders and Python t-strings. Import
from schedr import notify and write notify.dag: importing notify.task or notify.text
directly would shadow schedr.task or schedr.text. A bare placeholder keeps its JSON type;
in a t-string it becomes text. Plain values in a t-string are baked in when the DAG loads.
Paths such as notify.data['rows'] and notify.data['items'][0] select fields and indexes.
A task send whose data lacks a path used by the body is refused, naming the field. Event
notifications have null data; a path into it becomes an empty string in text.
Bodies refuse non-JSON values, non-string object keys, f-strings over placeholders, calls on
placeholders, and conversions or format specs on them at load. Task outputs, secrets and
dag_ctx references cannot be interpolated: the master fills the body without running Python
or seeing task secrets.
Placeholder |
Value |
|---|---|
|
Event name; |
|
DAG ID. |
|
Run ID, or null (for a trigger). |
|
Run’s planned time in UTC (RFC 3339), or null. |
|
Run or task state, or null. |
|
One-line summary, or the task’s text. |
|
UI link to the run or DAG, or null without |
|
List of objects with |
|
Task ID for task events and sends, or null. |
|
Attempt number for task events and sends, or null. |
|
Event time in UTC (RFC 3339). |
|
DAG’s time zone. |
|
Task send’s JSON data, or null for events. |
Without a body, schedr posts an object with all these fields by name, except data is
omitted when null. Its top-level text works with Slack and Mattermost incoming webhooks.
A Microsoft Teams workflow webhook accepting a simple text body can use
body={"text": notify.text}. Configure its secret URL on the server. Check the workflow’s
own schema if it expects a different body.
To load this example on a server, configure ops-slack and allow
https://relay.example.com/ under [notifications] dag_urls. Replace the example URLs
with your service’s addresses.
The example’s email service bodies show a plain send and a stored-template send with a template ID and variables. Adapt their field names and URLs to your service; these are webhooks, not built-in email channels. Choose this recipe when an email service accepts HTTP requests or uses stored templates instead of SMTP. The operator can instead configure these bodies on server channels, including authentication headers from environment variables.
Sending from a task¶
A task can send its own message. The name is looked up among the DAG’s channels first,
then server channels. The event is MESSAGE, with the task’s DAG, run and task filled in.
from schedr import api
relay.send("Loaded rows", data={"rows": 42})
api.notify("ops-slack", text="Loaded rows", data={"rows": 42})
api.notify("oncall", text="Loaded rows", data={"rows": 42})
A command task can send JSON directly or read it from a file:
schedr task-ctx notify ops-slack --text 'Loaded rows' --data '{"rows":42}'
schedr task-ctx notify ops-slack --text 'Loaded rows' --data @file.json
schedr task-ctx notify oncall --text 'Loaded rows' --data '{"rows":42}'
For an email channel, the task’s text becomes the subject: its first line is trimmed and
shortened after 200 characters. The full text and data appear in the body.
A successful call means the message is queued. Delivery happens later. Each attempt may send at most
[notifications] max_per_attempt messages (100 by default); beyond it Python raises and the
CLI exits non-zero. See Notifications for operators for
delivery and retries.
For a unit test, task_context records sends without checking the channel against a master:
from schedr import api
from schedr.testing import task_context
with task_context() as values:
api.notify("ops-slack", text="Loaded rows", data={"rows": 42})
assert values.notifications == [
{"channel": "ops-slack", "text": "Loaded rows", "data": {"rows": 42}}
]