fix(03): WR-04 start background workers only when run as entrypoint

This commit is contained in:
Lucas Berger
2026-06-09 10:40:54 -04:00
parent 22d1bc27d6
commit 7bc129f0f3
+15 -6
View File
@@ -62,11 +62,12 @@ app.route('/api/me', meRouter)
app.route('/api/events', eventsRouter) app.route('/api/events', eventsRouter)
app.route('/api/sse', sseRouter) app.route('/api/sse', sseRouter)
// Start the CalDAV broker poller (5-min cron, D-13 ctag change-detection) // WR-04: background worker startup (cron schedules) moved into the isMainModule()
// Runs in the background — errors are caught and logged per-credential (T-03-04) // guard below. Calling them at top level registered real node-cron schedules whenever
startBrokerPoller() // ./index.js was imported — the route tests import `app` from here, so the poller/outbox
// Drain the D-05 outbox every 15s: dispatches pending CalDAV writes to Fastmail // drain fired during the test run, touched the mocked DB/CalDAV layers nondeterministically,
startOutboxWorker() // and left open handles that blocked clean process exit. They now start only when the
// module is the process entrypoint.
// Serve React PWA static assets from ./public (Vite build output). // Serve React PWA static assets from ./public (Vite build output).
// MUST serve the whole ./public tree, not just /assets/* — root-level PWA files // MUST serve the whole ./public tree, not just /assets/* — root-level PWA files
@@ -97,8 +98,16 @@ function isMainModule(): boolean {
} }
} }
// Only start the HTTP server when this module is run directly (not imported in tests) // Only start the HTTP server AND background workers when this module is run directly
// (not imported in tests). WR-04: gating the cron schedules here keeps them out of the
// test process.
if (isMainModule()) { if (isMainModule()) {
// Start the CalDAV broker poller (5-min cron, D-13 ctag change-detection).
// Runs in the background — errors are caught and logged per-credential (T-03-04).
startBrokerPoller()
// Drain the D-05 outbox every 15s: dispatches pending CalDAV writes to Fastmail.
startOutboxWorker()
serve({ fetch: app.fetch, port: 3000 }, (info) => { serve({ fetch: app.fetch, port: 3000 }, (info) => {
console.log(`FamilySync API running on http://localhost:${info.port}`) console.log(`FamilySync API running on http://localhost:${info.port}`)
}) })