Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Background Jobs  ›  Lesson

Schedule-Driven Jobs

Schedule Driven 12 minA schedule-driven background job is started by the clock rather than by an event, and must be safe against overlapping runs, duplicate schedulers and missed runs.

Schedule-Driven Jobs

Some Hop links have an expiry date. At the moment a link expires, nothing happens — nobody clicks a button, no message is sent. Time simply passes. Work like this cannot wait for an event, because there is none. It needs the clock.

The rule

No event to wait for? Let the clock start the job.

A schedule-driven job runs at fixed times or intervals: every night at 03:00, every hour, every Monday. Typical uses:

  • clean-up: disable expired links, delete old temporary files
  • roll-ups: turn a day's raw click events into daily totals
  • reports and reminders: a weekly usage email
  • checks: compare records between two systems and flag differences

On one machine this is often a cron line:

# minute hour day month weekday  command
0 3 * * *  /opt/hop/bin/expire-links

The clock is simple; the surprises come from the world around it:

  • Several servers. If all three Hop servers carry the same cron line, the job runs three times. Use one scheduler, or let each server try to take a lock and let only the winner run.
  • Overlap. If a run takes longer than the interval, the next one starts on top of it. Skip a run while the previous is still going.
  • Missed runs. The machine was down at 03:00. The job should catch up, not skip a day's work forever.

The shortcut

Write the job so it asks "what is still left to do?" instead of "what happened since last time?" Then missed runs catch up and repeated runs do nothing harmful.

Hop's expiry job, made robustStep 1 of 4
  1. Choose the trigger

    Expiry has no event, so schedule it: 03:00 every night.

The trap is the job that works on your laptop and runs three times in production.

Drills

Answer fast — trust the rule.

quick drill

Which task is naturally schedule-driven?

quick drill

Hop runs the same cron line on three servers. What goes wrong?

quick drill

Which selection lets a missed nightly run catch up automatically?

quick drill

A job scheduled every 10 minutes sometimes takes 15. What should the design do?

Remember

  • No event to wait for? Let the clock start the job.
  • Shortcut: ask "what is still left to do?", not "what happened since last time?"
  • Trap: duplicate runs across servers, overlapping runs and silently missed runs.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainDesign Hop's nightly expiry job

Hop links can have an expiry date. Design a nightly job that disables expired links. Explain why it is schedule-driven rather than event-driven, and how you keep it correct when Hop runs on three servers and when one night's run is missed.