Files
hapi/docs/adr/0012-housekeeping-cronjobs.md
T
2026-11-27 15:19:32 -05:00

4.0 KiB
Raw Blame History

Scheduled work runs as CronJobs of the hAPI image, as a tokenless scheduler Bot

hAPI's recurring work is a small set of Kubernetes CronJobs that run hapi <subcommand> from the same digest-pinned image as the app, delivered the same way as the migrate Job (ADR-0007). There is no worker Deployment, no in-process scheduler and no task queue. Most domain logic is computed on read (Finance summaries, Maintenance due dates, Fitness bests), so little has to run on a timer. A CronJob keeps nothing running between runs, gets retries and concurrencyPolicy: Forbid from Kubernetes, and is a CLI command the Owner can also run by hand.

Considered Options

  • A worker Deployment with an in-process scheduler (APScheduler, arq). Rejected: it runs all the time, holds its own pool of up to 10 connections from ADR-0004's budget, and needs leader election before it can have more than one replica.
  • Scheduling inside the API pods. Rejected: with 2 replicas every task needs an advisory lock, and the work competes with requests for the event loop.
  • A transactional outbox of domain events (e.g. "Reading recorded"), drained by a CronJob. Rejected for v1. The state already in the database is the event: a scheduled check compares it with what has already been notified, at the cost of up to one interval of latency. A generic event model can arrive once something needs sub-minute reactions.
  • FastAPI BackgroundTasks after commit. Rejected: the work is lost if the pod dies.
  • Running as hapi_migrator, which bypasses RLS as the table owner. Rejected: it would put migrator credentials in a recurring pod.
  • Giving the scheduler Bindings to Personal data. Rejected: every Person would have to consent, and v1's per-Person work is read-only anyway.

Consequences

  • v1 inventory: hapi jobs sweep every 15 min marks pending Jobs past expires_at as expired and deletes finished Jobs older than 90 days (replacing ADR-0006's hapi jobs purge). Reads derive expired from expires_at until the sweep catches up, so correctness never depends on how often it runs. The notification checks are CronJobs running hapi notify <check> (tasks, jobs, budget, tokens), plus hapi notify deliver every 2 min, which alone publishes to ntfy (ADR-0013). Purging a removed Person's data stays an on-demand Owner CLI command, and Finance month rollover, Banked carry-forward and date_review need no schedule.
  • Principal: work on Household data runs as a built-in scheduler Bot. A migration creates it, it can never hold a Token (so it can't be reached from the API), and the Owner gives it Household grants like any other Bot. Recorded by shows scheduler. Work on Personal data runs once per Person with hapi.principal_id set to that Person (ADR-0004), and is read-only: a scheduled write to Personal data needs a new decision.
  • Event-triggered work (e.g. a new Reading making a Task due) is picked up by the next scheduled check. Checks write durable Notifications, which double as the record of what was already notified. A Notification not yet delivered stays pending and is retried on the next run, so an ntfy outage loses nothing, and a future frontend inbox reads the same records.
  • Connection budget (amends ADR-0004): each housekeeping CronJob uses a pool of 1 with no overflow and concurrencyPolicy: Forbid. The rule becomes: app pods including surge × 10 + concurrently runnable CronJobs × 1 + ~6 ≤ 46.
  • Monitoring: one generic vmalert rule fires when any CronJob hasn't succeeded within twice its interval (kube-state-metrics' kube_cronjob_status_last_successful_time). ADR-0009's Watchdog covers vmalert itself. Kuma push monitors are kept for jobs whose silence must be caught even when the whole cluster is down: the importer (ADR-0011) and backups.
  • Time: the Household timezone is one setting in deploy/, fed both to the app config and to spec.timeZone on calendar-based CronJobs (daily checks, month-end). Interval jobs like the sweep ignore it.