fix(jobs): enqueue during boot waits for the boss instead of dropping

The server answers before pg-boss is bound, and a producer rebinds in
the background after losing its connection. A request in either window
read the boss as null and skipped its enqueue: OAuth callbacks lost the
first sync and the history backfill, measurement and mood writes lost
their rollup recompute, WHOOP webhook deliveries were dropped.

whenBossReady() waits for the boss while a start is under way (boot or
reconnect), bounded at 60 s and logged when it gives up, and answers
null at once when nothing is coming. enqueueWhenBossReady() sends at
once through a bound boss and otherwise defers the send to the
background, so the request is not held for the boot.
This commit is contained in:
Marc-André Bombeck committed 2026-10-21 21:37:21 +02:00
1 parent e0eaed2251
commit b795b3b1f3
14 files changed
+266 -83

No files matched your search

+3 -4
View File
@@ -16,7 +16,7 @@ import {
import { getUserFitbitCredentials } from "@/lib/fitbit/credentials";
import { FITBIT_OAUTH_STATE_COOKIE } from "@/lib/fitbit/oauth-state";
import { FITBIT_BACKFILL_QUEUE } from "@/lib/jobs/fitbit-backfill";
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
import { enqueueWhenBossReady } from "@/lib/jobs/boss-ready";
import { markReconnected } from "@/lib/integrations/status";
import { Prisma } from "@/generated/prisma/client";
import {
@@ -205,8 +205,7 @@ export const GET = apiHandler(async (request: NextRequest) => {
// Enqueue the self-converging history backfill. Best-effort: the boot-time
// discovery query (`backfillCompletedAt IS NULL`) is the safety net, so a
// missing boss instance here doesn't strand the connection.
const boss = getGlobalBoss();
if (boss) {
await enqueueWhenBossReady("fitbit-backfill", async (boss) => {
await boss
.send(FITBIT_BACKFILL_QUEUE, {
userId: user.id,
@@ -215,7 +214,7 @@ export const GET = apiHandler(async (request: NextRequest) => {
.catch((err) =>
getEvent()?.addWarning(`fitbit-backfill enqueue failed: ${err}`),
);
}
});
const response = NextResponse.redirect(
new URL(