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(
+3 -4
View File
@@ -18,7 +18,7 @@ import {
import { getUserGoogleHealthCredentials } from "@/lib/google-health/credentials";
import { GOOGLE_HEALTH_OAUTH_STATE_COOKIE } from "@/lib/google-health/oauth-state";
import { GOOGLE_HEALTH_BACKFILL_QUEUE } from "@/lib/jobs/google-health-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 {
@@ -235,8 +235,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("google-health-backfill", async (boss) => {
await boss
.send(GOOGLE_HEALTH_BACKFILL_QUEUE, {
userId: recordUserId,
@@ -247,7 +246,7 @@ export const GET = apiHandler(async (request: NextRequest) => {
`google-health-backfill enqueue failed: ${err}`,
),
);
}
});
const response = NextResponse.redirect(
new URL(
+3 -4
View File
@@ -16,7 +16,7 @@ import { encrypt } from "@/lib/crypto";
import { checkRateLimit, rateLimitHeaders } from "@/lib/rate-limit";
import { markReconnected } from "@/lib/integrations/status";
import { NIGHTSCOUT_SYNC_QUEUE } from "@/lib/jobs/integration-poll-queues";
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
import { enqueueWhenBossReady } from "@/lib/jobs/boss-ready";
import {
fetchSgvEntries,
NightscoutApiError,
@@ -160,8 +160,7 @@ export const POST = apiHandler(async (request: NextRequest) => {
// empty card until the next hourly tick. Best-effort by design: the hourly
// cron remains the safety net, so a missing boss instance must never fail the
// connect response the user just earned.
const boss = getGlobalBoss();
if (boss) {
await enqueueWhenBossReady("nightscout-sync", async (boss) => {
await boss
.send(NIGHTSCOUT_SYNC_QUEUE, { userId: user.id })
.catch((err) =>
@@ -169,7 +168,7 @@ export const POST = apiHandler(async (request: NextRequest) => {
`nightscout-sync connect enqueue failed: ${err}`,
),
);
}
});
return apiSuccess({ connected: true });
});
+3 -4
View File
@@ -25,7 +25,7 @@ import { runOAuthCallbackOnce } from "@/lib/oauth/callback-claim";
import { ouraCollectionsMissingFromGrant } from "@/lib/oura/scope";
import { markReconnected } from "@/lib/integrations/status";
import { OURA_SYNC_QUEUE } from "@/lib/jobs/integration-poll-queues";
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
import { enqueueWhenBossReady } from "@/lib/jobs/boss-ready";
import { integrationBackfillSourceOptions } from "@/lib/jobs/integration-backfill-admission";
import {
OURA_BACKFILL_QUEUE,
@@ -184,8 +184,7 @@ async function completeCallback(
// missing. Reconnects ride the same path. Best-effort by design: the hourly
// cron remains the safety net, so a missing boss instance must never strand
// the redirect.
const boss = getGlobalBoss();
if (boss) {
await enqueueWhenBossReady("oura-connect", async (boss) => {
await boss
.send(OURA_SYNC_QUEUE, { userId })
.catch((err) =>
@@ -206,7 +205,7 @@ async function completeCallback(
`oura-backfill connect enqueue failed: ${err}`,
),
);
}
});
const response = NextResponse.redirect(
new URL(
+3 -4
View File
@@ -19,7 +19,7 @@ import {
import { runOAuthCallbackOnce } from "@/lib/oauth/callback-claim";
import { markReconnected } from "@/lib/integrations/status";
import { POLAR_SYNC_QUEUE } from "@/lib/jobs/integration-poll-queues";
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
import { enqueueWhenBossReady } from "@/lib/jobs/boss-ready";
import { NextRequest, NextResponse } from "next/server";
/**
@@ -156,14 +156,13 @@ async function completeCallback(
// card until the next hourly tick. Best-effort by design: the hourly cron
// remains the safety net, so a missing boss instance must never strand the
// redirect.
const boss = getGlobalBoss();
if (boss) {
await enqueueWhenBossReady("polar-connect", async (boss) => {
await boss
.send(POLAR_SYNC_QUEUE, { userId })
.catch((err) =>
getEvent()?.addWarning(`polar-sync connect enqueue failed: ${err}`),
);
}
});
const response = NextResponse.redirect(
new URL(
+3 -4
View File
@@ -19,7 +19,7 @@ import {
import { runOAuthCallbackOnce } from "@/lib/oauth/callback-claim";
import { markReconnected } from "@/lib/integrations/status";
import { STRAVA_BACKFILL_QUEUE } from "@/lib/jobs/strava-backfill";
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
import { enqueueWhenBossReady } from "@/lib/jobs/boss-ready";
import { NextRequest, NextResponse } from "next/server";
/**
@@ -161,8 +161,7 @@ async function completeCallback(
// (`stravaBackfillCompletedAt IS NULL`) is the safety net, so a missing
// boss instance here never strands the connection. Mirrors the WHOOP /
// Fitbit callback enqueue.
const boss = getGlobalBoss();
if (boss) {
await enqueueWhenBossReady("strava-backfill", async (boss) => {
await boss
.send(STRAVA_BACKFILL_QUEUE, {
userId,
@@ -171,7 +170,7 @@ async function completeCallback(
.catch((err) =>
getEvent()?.addWarning(`strava-backfill enqueue failed: ${err}`),
);
}
});
const response = NextResponse.redirect(
new URL(
+3 -4
View File
@@ -15,7 +15,7 @@ import { getUserWhoopCredentials } from "@/lib/whoop/credentials";
import { WHOOP_OAUTH_STATE_COOKIE } from "@/lib/whoop/oauth-state";
import { buildReturnSchemeRedirect } from "@/lib/whoop/return-scheme";
import { WHOOP_BACKFILL_QUEUE } from "@/lib/jobs/whoop-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 { isP2002 } from "@/lib/prisma-errors";
@@ -257,8 +257,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("whoop-backfill", async (boss) => {
await boss
.send(WHOOP_BACKFILL_QUEUE, {
userId,
@@ -267,7 +266,7 @@ export const GET = apiHandler(async (request: NextRequest) => {
.catch((err) =>
getEvent()?.addWarning(`whoop-backfill enqueue failed: ${err}`),
);
}
});
const response = outcomeRedirect(returnScheme, "connected");
response.cookies.delete(WHOOP_OAUTH_STATE_COOKIE);