Sub topic 6
Event-based and periodic triggers
This chapter assumes you can create a job with tasks, and that you know how a Quartz cron schedule works from the previous chapter. It covers the two other ways to start a job automatically: a file arrival trigger and a periodic trigger.
Why use a trigger instead of a cron schedule
A cron schedule fires at fixed clock times, whether or not there is work to do. A trigger starts a run in response to something else. Two kinds are covered here:
- A file arrival trigger starts a run when new files land in a storage location.
- A periodic trigger starts a run at a fixed interval, such as every 4 hours, counted from when the trigger was set up rather than from a clock time.
A job has one way of starting automatically. You configure either a schedule or a trigger on a job, not both.
File arrival triggers
A file arrival trigger watches a storage path. The path must be a Unity Catalog volume or external location, or a path under one of them. When the trigger sees new files, it starts a run.
The trigger checks the location about once a minute. You can set two timing options:
min_time_between_triggers_secondsis the minimum wait after a run starts before another can start.wait_after_last_change_secondsmakes the trigger wait until no new files have arrived for that long. This is useful when a producer writes many files in a burst.
{
"name": "process-landing-files",
"max_concurrent_runs": 1,
"trigger": {
"pause_status": "UNPAUSED",
"file_arrival": {
"url": "/Volumes/main/sales/landing/",
"min_time_between_triggers_seconds": 300,
"wait_after_last_change_seconds": 60
}
}
}This fragment shows only the trigger settings. A real job also needs a tasks list.
Periodic triggers
A periodic trigger repeats at an interval you choose, in hours, days, or weeks. It is simpler than cron when you only need "every N hours" and do not care about the exact clock time.
{
"name": "refresh-every-4-hours",
"trigger": {
"pause_status": "UNPAUSED",
"periodic": {
"interval": 4,
"unit": "HOURS"
}
}
}Choosing between them
| Need | Use |
|---|---|
| Run at 06:00 in a given time zone | Quartz cron schedule |
| Run when upstream data arrives | File arrival trigger |
| Run every N hours or days, any start time | Periodic trigger |
Common errors
- The path is not under a volume or external location, so the trigger cannot be created. Check that you have read access to the path.
- Setting a schedule and a trigger on the same job. Pick one.
- Expecting one run per file. A burst of files can produce a single run, especially with
wait_after_last_change_secondsset. - Leaving
max_concurrent_runsat its default when runs can overlap. Set it deliberately, as you will see in the chapter on concurrency. - Forgetting that
pause_statuscan bePAUSED. A paused trigger never fires.
Exercise
A vendor uploads about 200 CSV files into a volume over roughly two minutes, several times a day. You want one run per upload batch, not many. Which trigger and which setting would you use?
Show answer
Use a file arrival trigger on the volume path and set waitafterlastchangeseconds (for example 120) so the trigger waits until the uploads pause. Also set mintimebetweentriggersseconds to limit how soon another run can start.
Exercise
Your job already has a Quartz cron schedule. You want it to run when files arrive instead. What must you do?
Show answer
Remove the schedule and add a file arrival trigger, because a job can use a schedule or a trigger but not both.