ARCHIVED from builddistributedsystem.com on 2026-04-28 — URL: https://builddistributedsystem.com/tracks/orchestrator/tasks/task-26-1-1-job-scheduling
TASK

Implementation

A job scheduler runs tasks at a specific time or on a recurring schedule without the caller waiting. It is the backbone of background work: sending emails, cleaning up data, generating reports.

Implement a node that manages job scheduling and execution:

// Schedule a one-time job to run after a delay
{ "type": "schedule_job", "msg_id": 1,
  "task": "send-email", "payload": {"to": "user@example.com"},
  "delay_ms": 60000 }
-> { "type": "job_scheduled", "in_reply_to": 1,
    "job_id": "<uuid>", "scheduled_at": "<iso-timestamp>" }

// Schedule a recurring job using a cron expression
{ "type": "schedule_job", "msg_id": 2,
  "task": "cleanup", "cron": "0 0 * * *", "payload": {"days": 30} }
-> { "type": "job_scheduled", "in_reply_to": 2,
    "job_id": "<uuid>", "next_run": "<iso-timestamp>" }

// Execute all jobs due at or before this time
{ "type": "execute_jobs", "msg_id": 3,
  "current_time": "2024-01-15T09:00:00Z" }
-> { "type": "jobs_executed", "in_reply_to": 3,
    "count": 2, "jobs": ["job-123", "job-456"] }

// Retry a failed job with exponential backoff
{ "type": "execute_job", "msg_id": 4,
  "job_id": "job-123", "force_fail": true }
-> { "type": "job_failed", "in_reply_to": 4,
    "job_id": "job-123", "retry_scheduled": true, "backoff_ms": 2000 }

Retry backoff: base_ms * 2^attempt. Stop retrying after max_attempts is reached.

Sample Test Cases

Schedule one-time jobTimeout: 5000ms
Input
{
  "src": "client",
  "dest": "scheduler",
  "body": {
    "type": "schedule_job",
    "msg_id": 1,
    "task": "send-email",
    "payload": {
      "to": "user@example.com"
    },
    "delay_ms": 60000
  }
}
Expected Output
{"type": "job_scheduled", "in_reply_to": 1, "job_id": ".*", "scheduled_at": ".*"}
Schedule recurring job with cronTimeout: 5000ms
Input
{
  "src": "client",
  "dest": "scheduler",
  "body": {
    "type": "schedule_job",
    "msg_id": 1,
    "task": "cleanup",
    "cron": "0 0 * * *",
    "payload": {
      "days": 30
    }
  }
}
Expected Output
{"type": "job_scheduled", "in_reply_to": 1, "job_id": ".*", "next_run": ".*"}

Hints

Hint 1
schedule_job with delay_ms schedules a one-time job to run after the delay
Hint 2
schedule_job with cron schedules a recurring job; parse the cron expression to compute next_run
Hint 3
execute_jobs finds all jobs whose scheduled_at <= current_time and runs them
Hint 4
On job failure, schedule a retry using exponential backoff: backoff = base_ms * 2^attempt
Hint 5
Each job must get a unique generated job_id returned in the response
OVERVIEW

Theoretical Hub

Concept overview coming soon

Key Concepts

job schedulingcron expressionsdelayed executionexponential backoffretry policy
main.py
python
Implement Job Scheduling System - The Orchestrator | Build Distributed Systems