Skip to content

0169: The background service is a resident scheduler

  • Implementation partial

Context and Problem Statement

ADR 0020 runs hz system daemon tick at wall-clock slots: launchd's StartCalendarInterval or a systemd timer starts a short process that runs the due jobs one after another, and a timeout only abandons a thread. Jobs can only be cron. Calendar reminders need a deadline the tick cannot meet, so ADR 0126 gave them a second per-profile unit with its own setup. The owner decided (0.9.59, round 3 and 5) that the background service becomes resident, runs many tasks in parallel at arbitrary frequencies and schedules, runs reminders as one of them, and is the default. A reminders-in-the-tick draft (a child lane per tick, drafted as 0147 on the unmerged branch docs/reminders-in-daemon, c735f8b0a) was overruled by that decision and is superseded by this record; it was never merged, so no number is renumbered.

Decision Drivers

  • One background service and one consent per computer; reminders without a second unit.
  • Tasks at any cadence (cron, interval, always running), in parallel, with a cap.
  • A crash, a hang or a memory spike in one task never takes another down.
  • New code runs after an update, and an update never interrupts a run halfway without a drain.
  • Existing 0.9.57 and 0.9.58 installs keep working until they are migrated, and a changed unit is never replaced.

Considered Options

  • One resident supervisor per profile that runs every task run as its own child process (chosen)
  • Threads in one resident process: a thread can be neither killed nor isolated.
  • A child lane per tick for reminders only (the superseded draft): overruled by the owner; it still has no general scheduler.
  • Keep the tick and add a second resident unit per feature: two things to install, show and repair.

Decision Outcome

Chosen option: one resident supervisor, because it is the only option that runs arbitrary tasks in parallel and isolates each run.

  1. Process. The existing label runs hz system daemon serve. launchd: RunAtLoad, KeepAlive, ThrottleInterval 10, Background, Nice 10, LowPriorityIO, ExitTimeOut 90. systemd: Type=simple, Restart=always, KillMode=mixed, TimeoutStopSec=90, MemoryMax=768M; the timer stays as a watchdog that starts a stopped service at login and every five minutes. One supervisor per profile holds supervisor.lock.
  2. Runs are children. The supervisor never runs job code. Each run is hz system daemon run-job ID --slot ISO in its own process group at Nice 10. A timeout kills the group; a child over 512 MB RSS is killed and recorded.
  3. Triggers. A job has exactly one of schedule (cron, as before), every (an interval, slots on a fixed grid) or resident=True (started again whenever it exits). It has max_parallel (default 1) and overlap skip or queue. daemon.max_children (default 4) caps the children running at once. Slots, catch_up, idempotency and receipts stay as in 0020; the scheduler's last slot per task is durable in scheduler.json, seeded from the ledger.
  4. Crashes. A failed or killed run backs off from 1 s, doubling, to at most 15 min. After 5 failures in a row the task is quarantined until the next version or hz system daemon resume-job ID.
  5. Loop. The supervisor sleeps until the next due slot, at most 30 s, and writes heartbeat.json every loop. A wall clock that moved further than the monotonic clock is logged as a wake from sleep; slots are wall-clock, so missed ones follow catch_up.
  6. Update handover. SIGTERM drains: no new runs, running children get 60 s, then are killed, and the supervisor exits 0 so the manager starts the new version. A handover request restarts the resident unit (launchctl kickstart -k, systemctl restart); the new process passes 0020's birth-after-request check and commits the handover as a manager-bound tick did. tick.lock is held while any child runs, so quiescence checks hold.
  7. Status. DaemonStatus gains mode, supervisor{pid,since,version}, tasks[{id,trigger,next,running,last, failing,rss_mb}], keeping[{label,memory_mb,since}] and overdue; the Background service pane shows why it keeps running.
  8. Default and migration. install writes the resident unit unless --mode tick is given. A manager-bound tick of a new version whose tick unit is exactly what this version renders (byte-identical, owned by its profile) starts one detached hz system daemon migrate --after-pid PID per version, which waits for the tick to end and installs the resident unit through the ordinary verified install. A changed unit is never replaced; status and doctor report it. daemon.mode = "tick" keeps the tick.
  9. Reminders. Reminders are the resident task org:calendar-reminders, wrapping 0126's worker cycle under its consent and maintenance locks. The task removes the profile's 0126 unit only when it runs itself (so the resident service owns the profile) and only if that unit is byte-identical to what Heinzel wrote; a changed one is kept and reported. Without the resident service, the 0126 unit keeps working.

Consequences

  • Good: any cadence and parallel tasks; reminders meet 0126's deadline without a second unit; runs are killable.
  • Bad: one process stays resident (budget: at most 60 MB RSS and about no CPU when idle).
  • Neutral: interval_minutes stays in the install record, read-only, for jobs' max_tick_interval; resident installs record 1.

Confirmation

Unit tests drive the scheduler with a fake clock and spawner (cron, interval, DST, overlap, cap, backoff, quarantine, drain), real children (a crash or hang leaves other tasks running; a timeout kills the group), byte tests for the unit texts, and the migration with a fake service manager in an isolated home. Live launchd and systemd runs on a test profile are still to be done.

More Information

Design: owner round 3 and 5 for 0.9.59 (jvry, e9g0, isjn).