Sub topic 6
Event-based and periodic triggers
This chapter assumes you can create a multi-task job, you know how a cron schedule works, and you have a Unity Catalog workspace with at least one external location or volume. A cron schedule starts a job at fixed clock times. A trigger starts a job when something happens, or at a fixed interval. This chapter covers the file arrival trigger and the periodic trigger. It also covers the settings that stop a trigger from firing too often.
Schedule or trigger?
A job can start in five ways:
- A person or API call starts it manually.
- A cron schedule starts it.
- A trigger starts it.
- It runs continuously.
- Another job starts it with a Run Job task.
In the job's settings, a cron schedule and a trigger exclude each other. You pick one automatic mechanism per job. If you need both, create two jobs that run the same notebooks, or have one job call the other.
| Mechanism | Fires when | Typical use |
|---|---|---|
| Scheduled (cron) | A wall-clock time matches the Quartz expression | Daily reports at 06:00 in a given time zone |
| Periodic trigger | A fixed interval has passed | "Every 2 hours", where the exact minute does not matter |
| File arrival trigger | New files appear in a monitored location | Ingest data as soon as an upstream system drops it |
| Continuous | The previous run ends | Near-real-time streaming jobs |
How a file arrival trigger works
A file arrival trigger watches a storage location. Databricks checks that location for new files. When it sees some, it starts a run of the job. The location must be covered by Unity Catalog. It can be a volume, or a path under an external location. The identity that owns and runs the job needs read access to it, for example READ FILES on the external location.
Configuring it in the Jobs API
The trigger is a top-level field of the job settings. It replaces the schedule block.
{
"name": "ingest-on-arrival",
"max_concurrent_runs": 1,
"trigger": {
"pause_status": "UNPAUSED",
"file_arrival": {
"url": "s3://my-bucket/landing/orders/",
"min_time_between_triggers_seconds": 300,
"wait_after_last_change_seconds": 120
}
},
"tasks": [
{
"task_key": "ingest",
"notebook_task": {"notebook_path": "/Workspace/Shared/ingest"}
}
]
}The three settings under file_arrival do different jobs.
urlis the location to watch. Use a trailing slash so the path is read as a folder. In the UI you can also enter a volume path such as/Volumes/main/sales/landing/orders/.wait_after_last_change_secondsmakes the trigger wait until no new file has appeared for that long. It prevents a run from starting while an upload is still in progress.min_time_between_triggers_secondssets the minimum gap between two runs started by this trigger. It protects your compute when files arrive continuously.
If you leave the two timing settings out, Databricks applies its own minimum values, which are about one minute. Check the current documentation for the exact floors, because they can change.
The run does not receive a file list
The trigger tells the job that something arrived. It does not pass the names of the new files into the run. The job must work out which files are new. The reliable way is Auto Loader, which tracks processed files in a checkpoint. With availableNow, the stream processes everything pending and then stops, which suits a triggered job.
(
spark.readStream
.format("cloudFiles")
.option("cloudFiles.format", "json")
.option("cloudFiles.schemaLocation", "/Volumes/main/sales/meta/orders_schema")
.load("/Volumes/main/sales/landing/orders/")
.writeStream
.option("checkpointLocation", "/Volumes/main/sales/meta/orders_checkpoint")
.trigger(availableNow=True)
.toTable("main.sales.raw_orders")
)If you list the folder yourself and process every file you find, a second run will reprocess the same data.
Periodic triggers
A periodic trigger starts the job at a fixed interval. The units are hours, days, or weeks.
{
"trigger": {
"pause_status": "UNPAUSED",
"periodic": {"interval": 2, "unit": "HOURS"}
}
}A periodic trigger measures time since the previous trigger. It does not align to clock times. Two consequences follow:
- You cannot ask for "09:00 every day". Use a cron schedule for that.
- There is no time zone setting and no daylight saving problem. A cron job set to 02:30 in a zone with a clock change can skip or repeat an hour. An interval of 24 hours cannot.
Use a periodic trigger when "about every N hours" is enough and you do not care about the exact minute.
Table update triggers
Databricks also offers a table update trigger, which starts a job when watched Unity Catalog tables change. Its field is table_update, and it takes a list of table names and a condition such as ANY_UPDATED. It accepts the same two timing settings as the file arrival trigger. This chapter does not go deeper into it. The concepts are the same: an event starts a run, and the timing settings control how often.
The same trigger as a bundle resource
With Databricks Asset Bundles, the trigger sits in the job resource in the same shape as in the API.
resources:
jobs:
ingest_on_arrival:
name: ingest-on-arrival
max_concurrent_runs: 1
trigger:
pause_status: UNPAUSED
file_arrival:
url: s3://my-bucket/landing/orders/
min_time_between_triggers_seconds: 300
wait_after_last_change_seconds: 120
tasks:
- task_key: ingest
notebook_task:
notebook_path: ../src/ingest.ipynbCommon errors and edge cases
- The trigger never fires. The most common cause is permissions. The job owner cannot read the location, or the path is not covered by an external location or volume. Also check that
pause_statusisUNPAUSED. - A run starts on a half-uploaded batch. Raise
wait_after_last_change_seconds. Better still, have the producer write a marker file last and point the trigger at the folder where markers land. - Runs pile up or overlap. Combine
min_time_between_triggers_secondswithmax_concurrent_runsset to 1. The next chapter covers concurrency in detail. - Files are processed twice, or missed. The notebook is probably not using a checkpointed reader. Use Auto Loader, or record processed file names in a table.
- A job with a schedule will not accept a trigger. Remove the schedule first.
Exercise
A vendor uploads many large CSV files into a folder between 01:00 and 01:30 each night, and your job must start only after the last file lands. Which trigger and settings fit best, and how should the notebook decide which files to read?
Show answer
Use a file arrival trigger on the vendor's folder with wait_after_last_change_seconds longer than the longest gap between uploads, for example 600. Add a min_time_between_triggers_seconds of a few hours so late stragglers do not start a second run. The notebook should read with Auto Loader and trigger(availableNow=True) so its checkpoint decides which files are new.
Exercise
You need a job to run "every 6 hours" and nobody cares about the exact minute. A teammate proposes the cron expression 0 0 */6 * * ? with the time zone America/New_York. What is a simpler choice, and what problem does it avoid?
Show answer
Use a periodic trigger with "interval": 6, "unit": "HOURS". It has no time zone, so it avoids clock-change effects. A cron expression in a zone with daylight saving can shift, skip, or repeat a run when the clocks change.