Re-arm queued calculation jobs on startup and shut down gracefully #30

Open
opened 2026-07-17 03:07:29 +00:00 by claude-bot · 0 comments
Contributor

Recompute scheduling is an in-process setTimeout(...).unref() (backend/src/app.js:43-69). On restart, recoverAbandonedJobs (repository.pg.js:442-452) marks running jobs failed but never re-arms queued ones; they only run again if that home's calculation status is polled (app.js:191-193). server.js installs no signal handlers and never calls repository.close(), so docker stop drops timers and connections mid-flight.

Fix: (1) at startup, list due queued calculation jobs and schedule them; (2) add SIGTERM/SIGINT handlers: stop accepting connections, await in-flight calculation runs, pool.end(). Consider replacing per-change timers with a single interval poller over claimDueCalculationJob (already SKIP LOCKED-safe) if a second replica ever appears.

Acceptance criteria

  • Restart with a pending queued job runs it without requiring a status poll
  • SIGTERM drains and closes the pool (verify via compose stop logs)
  • Test covering startup re-arm (memory repo or pg integration tier)

Relations: relates to #16

(Audit finding F-09, 2026-07-16 — Severity: Medium, Confidence: High)

Recompute scheduling is an in-process `setTimeout(...).unref()` (`backend/src/app.js:43-69`). On restart, `recoverAbandonedJobs` (`repository.pg.js:442-452`) marks `running` jobs failed but never re-arms `queued` ones; they only run again if that home's calculation status is polled (`app.js:191-193`). `server.js` installs no signal handlers and never calls `repository.close()`, so docker stop drops timers and connections mid-flight. Fix: (1) at startup, list due `queued` calculation jobs and schedule them; (2) add SIGTERM/SIGINT handlers: stop accepting connections, await in-flight calculation runs, `pool.end()`. Consider replacing per-change timers with a single interval poller over `claimDueCalculationJob` (already SKIP LOCKED-safe) if a second replica ever appears. **Acceptance criteria** - [ ] Restart with a pending queued job runs it without requiring a status poll - [ ] SIGTERM drains and closes the pool (verify via compose stop logs) - [ ] Test covering startup re-arm (memory repo or pg integration tier) **Relations:** relates to #16 *(Audit finding F-09, 2026-07-16 — Severity: Medium, Confidence: High)*
Sign in to join this conversation.
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
rbrooks/BatteryStorageCalculator#30
No description provided.