4.0 KiB
4.0 KiB
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
BackgroundTasksafter 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 sweepevery 15 min marks pending Jobs pastexpires_atasexpiredand deletes finished Jobs older than 90 days (replacing ADR-0006'shapi jobs purge). Reads deriveexpiredfromexpires_atuntil the sweep catches up, so correctness never depends on how often it runs. The notification checks are CronJobs runninghapi notify <check>(tasks,jobs,budget,tokens), plushapi notify deliverevery 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 anddate_reviewneed no schedule. - Principal: work on Household data runs as a built-in
schedulerBot. 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 showsscheduler. Work on Personal data runs once per Person withhapi.principal_idset 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 tospec.timeZoneon calendar-based CronJobs (daily checks, month-end). Interval jobs like the sweep ignore it.