Sub topic 13
Notifying on Failure
This chapter assumes you can create a multi-task job, you know how retries and timeouts behave, and you can read a job definition in JSON or YAML. Retries decide whether a task tries again. Notifications decide who finds out when it does not recover.
What a notification is
A notification is a message that Databricks sends when a run reaches a lifecycle event. You attach notifications to a job, to an individual task, or to both. The message goes to a destination: an email address, a webhook-based system, or a workspace user.
Job-level notifications describe the whole run. Task-level notifications describe one task inside the run. A job-level failure message tells you the run failed. A task-level message tells you which task failed, which is faster to act on when a job has many tasks.
Events you can subscribe to
The Jobs API groups subscriptions by event. Each event takes a list of destinations.
| Event | Fires when | Typical use |
|---|---|---|
| `on_start` | A run begins | Audit trail, "pipeline started" messages |
| `on_success` | A run finishes successfully | Confirm that a daily load completed |
| `on_failure` | A run finishes in a failed state | Page the owner |
| `on_duration_warning_threshold_exceeded` | A run takes longer than a warning threshold | Catch slow runs before a hard timeout |
| `on_streaming_backlog_exceeded` | A streaming run falls behind | Streaming jobs only |
Most batch pipelines need only on_failure, and many add the duration warning. Subscribing to on_start and on_success for every job adds noise that teaches people to ignore the channel.
Configuring email notifications
Email notifications go in the email_notifications object. The example below extends the daily pipeline from earlier chapters.
{
"name": "daily-sales-pipeline",
"email_notifications": {
"on_failure": ["[email protected]"],
"on_duration_warning_threshold_exceeded": ["[email protected]"],
"no_alert_for_skipped_runs": true
},
"health": {
"rules": [
{
"metric": "RUN_DURATION_SECONDS",
"op": "GREATER_THAN",
"value": 3600
}
]
},
"timeout_seconds": 7200
}The health rule sets the warning threshold at one hour. When a run passes it, on_duration_warning_threshold_exceeded fires. The run keeps going. This differs from timeout_seconds, which cancels the run at two hours. Use the warning to learn about a slow run while it can still finish, and the timeout as the hard stop.
The setting no_alert_for_skipped_runs suppresses messages for runs that were skipped. A run is skipped, for example, when max_concurrent_runs is 1 and a previous run is still active. Without this flag, an overlong run can generate a stream of "skipped" alerts that look like failures.
Task-level notifications
A task can carry its own email_notifications. Task-level notifications support start, success, failure, and duration warning events.
{
"task_key": "ingest",
"job_cluster_key": "etl_cluster",
"notebook_task": {"notebook_path": "/Workspace/Shared/ingest"},
"max_retries": 2,
"min_retry_interval_millis": 60000,
"email_notifications": {
"on_failure": ["[email protected]"]
}
}This lets different teams own different tasks. The ingestion team hears about ingest failures, and the analytics team hears about publish failures. The job-level list can still go to a shared on-call address.
Retries and notification timing
Retries interact with notifications in a way that surprises people. Consider ingest with max_retries set to 2.
- The first attempt fails.
- Databricks waits for the retry interval and tries again.
- The second attempt fails, and so does the third.
- The task is now failed for good, and the job run fails.
By default, the failure notification fires only after the final attempt, not after each one. A transient error that succeeds on retry produces no alert. This is usually what you want. If you want to be told about every failed attempt, the job settings include a notify_on_last_failure option on the task. Setting it to true limits failure notifications for a retrying task to the last attempt, and leaving it off notifies on every failure. Check the current Jobs API reference for the exact default in your workspace, because it has changed between versions.
The diagram below shows when each message goes out for a task with two retries.
Other destinations
Email is the simplest destination, but teams often prefer chat or incident tools. Databricks supports notification destinations for Slack, Microsoft Teams, PagerDuty, and generic webhooks. An administrator creates the destination once in workspace settings. You then reference it by its ID in webhook_notifications.
resources:
jobs:
daily_sales:
name: daily-sales-pipeline
email_notifications:
on_failure:
- [email protected]
webhook_notifications:
on_failure:
- id: 00000000-0000-0000-0000-000000000000
tasks:
- task_key: ingest
notebook_task:
notebook_path: ../src/ingest.ipynbReplace the placeholder ID with the real destination ID from your workspace. Referencing an ID, not a URL, keeps secrets such as webhook tokens out of your job definition and your version control.
Common errors and gotchas
- Alerts go to one person who leaves. Use a team distribution list or an on-call destination, not a personal address.
- Only
on_failureis set, and a run hangs. A hung run never fails, so no message goes out. Pair notifications with atimeout_secondsvalue or a duration warning. - A timed-out run is not always a "failure" event. Check that timeouts and cancellations notify the way you expect by testing with a short timeout.
- Skipped-run noise. Set
no_alert_for_skipped_runswhen you use a concurrency limit of 1. - Notifications are not delivered on a deleted or unauthorized destination. If a webhook destination is removed, the job still runs but the alert silently goes nowhere. Review destinations periodically.
- Repairs send their own messages. When you repair a run, the repaired tasks run again and can notify again. Expect a second message when the repair succeeds or fails.
Testing your setup
Do not wait for a real outage to learn that alerts do not arrive. Create a copy of the job with a notebook task that raises an error:
# Notebook for a test task named "always_fails"
raise RuntimeError("Intentional failure to test notifications")Run it once with on_failure pointed at your destination. Confirm that the message arrives, that it names the right job and task, and that the link opens the failed run. Then delete the test job.
Summary of decisions
- Subscribe to
on_failureat the job level for every production job. - Add task-level failure lists when different teams own different tasks.
- Add a duration warning below your timeout to catch slow runs early.
- Use destinations by ID for chat and paging tools.
- Suppress skipped-run alerts when you limit concurrency.
Exercise
A job has max_retries set to 2 on its ingest task, a job-level on_failure email, and timeout_seconds of 7200. The ingest task fails on its first attempt and succeeds on the second. Is a failure email sent by default? Separately, which setting would you add to hear about a run that is taking too long but has not yet reached the timeout?
Show answer
By default no failure email is sent, because the failure notification fires only when the task fails after its final attempt, and here the second attempt succeeded. (Verify the default for your workspace, since the behavior is tied to the retry notification setting.) To hear about a slow run before the timeout, add a health rule on RUN_DURATION_SECONDS with a threshold below 7200 and subscribe a destination to on_duration_warning_threshold_exceeded.
Exercise
Your job has max_concurrent_runs set to 1. Last night's run overran, and the next scheduled run was skipped. The team received an alert that looked like a failure. Which setting prevents this, and why?
Show answer
Set no_alert_for_skipped_runs to true in email_notifications. A run skipped because of the concurrency limit is not a real failure, and this flag stops Databricks from sending an alert for it.