How do scheduled and background jobs run? #68

Closed
opened 2026-11-27 14:38:42 -05:00 by bobby · 1 comment
Owner

Part of #44

Question

What runs recurring and background work in v1 (a worker Deployment, k8s CronJobs invoking hapi subcommands, or in-process scheduling), which v1 jobs exist (Job purge, Maintenance due checks, Token-expiry checks, anything Finance needs), how cross-Person work runs under RLS without a bypass role, and how it fits ADR-0004's connection budget and ADR-0009's Kuma push monitoring?

Context carried from the map: background jobs / recurring schedules (Maintenance Task reminders, bill-due reminders for Banked Finance Categories, periodic syncs, purging finished Jobs older than 90 days). Shape depends on the domain MVPs. Constraints: cross-Person jobs run once per Person under RLS; there is no bypass role; any worker process shares the ~46-connection budget with app pods (ADR-0004).

Part of #44 ## Question What runs recurring and background work in v1 (a worker Deployment, k8s CronJobs invoking `hapi` subcommands, or in-process scheduling), which v1 jobs exist (Job purge, Maintenance due checks, Token-expiry checks, anything Finance needs), how cross-Person work runs under RLS without a bypass role, and how it fits ADR-0004's connection budget and ADR-0009's Kuma push monitoring? **Context carried from the map:** background jobs / recurring schedules (Maintenance Task reminders, bill-due reminders for Banked Finance Categories, periodic syncs, purging finished Jobs older than 90 days). Shape depends on the domain MVPs. Constraints: cross-Person jobs run once per Person under RLS; there is no bypass role; any worker process shares the ~46-connection budget with app pods (ADR-0004).
bobby added the wayfinder:grilling label 2026-11-27 14:38:42 -05:00
bobby added a new dependency 2026-11-27 14:38:50 -05:00
bobby added a new dependency 2026-11-27 14:38:51 -05:00
bobby self-assigned this 2026-11-27 14:40:55 -05:00
Author
Owner

Resolution: scheduled and background work

Kubernetes CronJobs run hapi <subcommand> from the app image, as a tokenless scheduler Bot. There's no worker, no queue and no outbox: scheduled checks write durable Notifications. See ADR-0012.

  • Mechanism: CronJobs from the same digest-pinned image, set up like the migrate Job (ADR-0007). Rejected: a worker Deployment (always on, 10 connections, leader election) and scheduling in the API pods (advisory locks, competes for the event loop).
  • v1 inventory: hapi jobs sweep every 15 min marks expired Jobs and deletes finished Jobs older than 90 days (replaces hapi jobs purge; reads derive expired until it runs). Notification checks are CronJobs running hapi notify <check>, and their content is left to How does hAPI send notifications through ntfy?. Not scheduled: purging a removed Person (on-demand CLI), Finance rollover and Banked carry-forward (computed on read), date_review (set at ingest), backups (the backups ticket).
  • Event-triggered work: no in-process tasks and no domain-event outbox. A new Reading that makes a Task due is caught by the next scheduled check (latency ≤ one interval).
  • Notifications: checks write durable Notification records. They double as the dedupe ledger, and an undelivered one is retried on the next run, so an ntfy outage loses nothing. A future frontend inbox (with read state) reads the same records, and ntfy is one channel. Left to the ntfy ticket: the fields, routing, channel preferences per recipient, whether v1 stores read state, and whether delivery runs inside the check or in its own CronJob.
  • Principal: Household data → the built-in scheduler Bot (created by a migration, can never hold a Token, gets Household grants from the Owner CLI, and shows up in Recorded by). Personal data → once per Person with hapi.principal_id = that Person, read-only. Rejected: hapi_migrator in a recurring pod, and Bindings for the scheduler.
  • Connection budget (ADR-0004 amended): pool 1, no overflow, concurrencyPolicy: Forbid. Rule: app pods (with surge) × 10 + concurrent CronJobs × 1 + ~6 ≤ 46.
  • Monitoring: one generic vmalert rule fires when any CronJob hasn't succeeded within twice its interval (kube_cronjob_status_last_successful_time). Kuma push is kept only for jobs that must be caught even when the cluster is down (importer, backups).
  • Time: a single Household timezone setting in deploy/ feeds the app config and spec.timeZone on calendar CronJobs. Interval jobs ignore it.

Glossary: Notification added; Bot now notes built-in Bots that never hold a Token.

## Resolution: scheduled and background work **Kubernetes CronJobs run `hapi <subcommand>` from the app image, as a tokenless `scheduler` Bot. There's no worker, no queue and no outbox: scheduled checks write durable Notifications.** See [ADR-0012](https://git.bobbypalko.com/bobby/hapi/src/branch/main/docs/adr/0012-housekeeping-cronjobs.md). - **Mechanism:** CronJobs from the same digest-pinned image, set up like the migrate Job (ADR-0007). Rejected: a worker Deployment (always on, 10 connections, leader election) and scheduling in the API pods (advisory locks, competes for the event loop). - **v1 inventory:** `hapi jobs sweep` every 15 min marks expired Jobs and deletes finished Jobs older than 90 days (replaces `hapi jobs purge`; reads derive `expired` until it runs). Notification checks are CronJobs running `hapi notify <check>`, and their content is left to *How does hAPI send notifications through ntfy?*. Not scheduled: purging a removed Person (on-demand CLI), Finance rollover and Banked carry-forward (computed on read), `date_review` (set at ingest), backups (the backups ticket). - **Event-triggered work:** no in-process tasks and no domain-event outbox. A new Reading that makes a Task due is caught by the next scheduled check (latency ≤ one interval). - **Notifications:** checks write durable **Notification** records. They double as the dedupe ledger, and an undelivered one is retried on the next run, so an ntfy outage loses nothing. A future frontend inbox (with read state) reads the same records, and ntfy is one channel. Left to the ntfy ticket: the fields, routing, channel preferences per recipient, whether v1 stores read state, and whether delivery runs inside the check or in its own CronJob. - **Principal:** Household data → the built-in **`scheduler` Bot** (created by a migration, can never hold a Token, gets Household grants from the Owner CLI, and shows up in Recorded by). Personal data → once per Person with `hapi.principal_id` = that Person, **read-only**. Rejected: `hapi_migrator` in a recurring pod, and Bindings for the scheduler. - **Connection budget (ADR-0004 amended):** pool 1, no overflow, `concurrencyPolicy: Forbid`. Rule: app pods (with surge) × 10 + concurrent CronJobs × 1 + ~6 ≤ 46. - **Monitoring:** one generic vmalert rule fires when any CronJob hasn't succeeded within twice its interval (`kube_cronjob_status_last_successful_time`). Kuma push is kept only for jobs that must be caught even when the cluster is down (importer, backups). - **Time:** a single Household timezone setting in `deploy/` feeds the app config and `spec.timeZone` on calendar CronJobs. Interval jobs ignore it. Glossary: **Notification** added; **Bot** now notes built-in Bots that never hold a Token.
bobby closed this issue 2026-11-27 14:52:21 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Reference: bobby/hapi#68