Sub topic 5
Scheduling a Job with Cron and Time Zones
This chapter assumes you can create a Databricks job with one or more tasks, and that you know basic cron fields (minute, hour, day, month, weekday). Here you attach a schedule to a job and handle time zones correctly.
How a schedule works
A job schedule tells Databricks to start a new run at times defined by a Quartz cron expression. A schedule has three settings:
quartz_cron_expression: when to run.timezone_id: the time zone that the expression is evaluated in.pause_status:UNPAUSEDto run,PAUSEDto keep the schedule but skip runs.
Quartz cron differs from Unix cron. It has six required fields, with seconds first, and an optional seventh field for year.
| Field | Allowed values |
|---|---|
| Seconds | 0-59 |
| Minutes | 0-59 |
| Hours | 0-23 |
| Day of month | 1-31, `?`, `L` |
| Month | 1-12 or JAN-DEC |
| Day of week | 1-7 or SUN-SAT, `?` |
| Year (optional) | 1970-2099 |
A daily schedule
This fragment of a job definition runs every day at 06:00 UTC:
{
"schedule": {
"quartz_cron_expression": "0 0 6 * * ?",
"timezone_id": "UTC",
"pause_status": "UNPAUSED"
},
"max_concurrent_runs": 1
}Read the expression field by field: second 0, minute 0, hour 6, any day of month, any month, and ? for day of week.
Common expressions
| Expression | Meaning |
|---|---|
| `0 0 6 * * ?` | Every day at 06:00 |
| `0 30 8 ? * MON-FRI` | Weekdays at 08:30 |
| `0 0 */4 * * ?` | Every four hours, on the hour |
| `0 0 2 1 * ?` | The first day of each month at 02:00 |
Choosing a time zone
Set timezone_id to an IANA name such as America/New_York or Europe/Berlin, not a fixed offset. The expression is read in that zone, so a job set to 06:00 in America/New_York follows daylight saving time. Its UTC start time shifts by an hour twice a year.
This causes two edge cases. A time that falls in the skipped hour in spring may not run as you expect. A time that falls in the repeated hour in autumn can be ambiguous. If your pipeline must run at a fixed interval, or if it feeds systems that use UTC, choose UTC and avoid scheduling in the hours around a clock change.
Overlapping runs
If a run takes longer than the interval between schedule times, the next run could start while the previous one is still going. With max_concurrent_runs set to 1, as above, Databricks skips the new run instead of running both. Chapter topics later in this tutorial cover this setting in more detail.
Common mistakes
- Writing a five-field Unix expression. Quartz rejects it or reads it differently.
- Forgetting that
timezone_idchanges the meaning of the hour field. - Leaving
pause_statusasPAUSEDand wondering why nothing runs. - Scheduling a job that is on a short interval but has no concurrency limit.
Exercise
You need a job to run at 07:15 on weekdays only, in the Europe/London time zone. Write the schedule's quartzcronexpression and name one effect of using Europe/London instead of UTC.
Show answer
The expression is 0 15 7 ? * MON-FRI. Because Europe/London observes daylight saving time, the job still starts at 07:15 local time all year, but its UTC start time moves by one hour when the clocks change.
Exercise
A job is scheduled with 0 0 6 * * * and the save fails. Why, and how do you fix it?
Show answer
Quartz requires ? in either the day-of-month or day-of-week field, and this expression uses * in both. Change the last field to ?, giving 0 0 6 * * ?.