Files
HealthLog/src/lib/jobs/fitbit-backfill.ts
T
Marc-André Bombeck 25b7ada8bf fix(fitbit): hold the sync verdict when a token is dead or a write fails
A revoked or expired Fitbit refresh token used to fail the cycle's status
ledger and then stamp the same cycle a success: the connection flipped back
to connected, the watermark advanced, and after a reconnect the 24h overlap
could no longer reach the missed days. getValidToken's three null-return
paths (credentials missing, refresh failure, vanished connection) now register
on the per-cycle hard-fail ledger, so the cycle's verdict fails, the park at
error_reauth holds, and lastSyncedAt does not move. The two measurement write
catches do the same, so fetched-but-unwritten rows are re-fetched next tick
instead of aging out of the overlap.

syncUserFitbit now returns { imported, failed }; the manual-sync route, the
poll cohort adapter, and the backfill consume the new shape. The backfill
stamps backfillCompletedAt only after a clean full-history walk: a parked
connection returns without stamping, and a failed verdict throws so pg-boss
retries on the already-configured retry budget.
2026-07-24 12:47:27 +02:00

154 lines
5.2 KiB
TypeScript

/**
* v1.12.0 — pg-boss queue + boot-time self-converging backfill for newly
* connected Fitbit / Google Health accounts. Modelled on the WHOOP backfill
* (`src/lib/jobs/whoop-backfill.ts`): a discovery query enqueues one job per
* connection that has NOT yet been backfilled, and the pass is idempotent
* across reboots (the predicate `backfill_completed_at IS NULL` drops a
* connection once its backfill finishes).
*
* The queue name MUST be registered in `allQueues` in
* `src/lib/jobs/reminder-worker.ts` or pg-boss never provisions it and the boot
* enqueue silently never drains (the v1.4.37 dead-queue class).
*/
import { prisma } from "@/lib/db";
import { annotate } from "@/lib/logging/context";
import { getGlobalBoss } from "@/lib/jobs/boss-instance";
import { integrationBackfillSourceOptions } from "@/lib/jobs/integration-backfill-admission";
import { isReauthRequired } from "@/lib/integrations/status";
import { syncUserFitbit } from "@/lib/fitbit/sync";
export const FITBIT_BACKFILL_QUEUE = "fitbit-backfill";
/**
* Serial concurrency — a backfill walks years of history for one account and is
* rate-bounded by Google's per-app quota; concurrency-1 keeps it from crowding
* the request pool, matching `WHOOP_BACKFILL_CONCURRENCY`.
*/
export const FITBIT_BACKFILL_CONCURRENCY = 1;
export interface FitbitBackfillPayload {
userId: string;
enqueuedAt: string;
}
/**
* Per-user backfill handler. Runs a full-history sync for one account and stamps
* `backfillCompletedAt` ONLY on a clean run, so the discovery query drops it.
* Idempotent: the per-resource upserts are key-stable, so a re-run (e.g. a
* reboot mid-walk) overwrites rather than duplicating. Mirrors
* `runWhoopBackfillForUser` (whose identical unconditional-stamp bug is fixed
* separately — this is the template for that port).
*
* Verdict-gated: a partial hard failure (one collection 400ing, a write
* failing, a dead token) THROWS so pg-boss retries the job — stamping the
* marker over a partial walk would freeze the history gap in forever and the
* configured `retryLimit` would be dead code. A connection parked at
* `error_reauth` is NOT clean either, but throwing against a dead grant would
* just burn the retry budget on the same no-op: return WITHOUT stamping — the
* boot discovery re-enqueues the account on the next boot, and the stamp lands
* once the user reconnects and a clean walk completes.
*/
export async function runFitbitBackfillForUser(
userId: string,
): Promise<{ imported: number }> {
if (await isReauthRequired(userId, "fitbit")) {
annotate({
action: {
name: "fitbit.backfill.skipped_reauth",
details: { imported: 0 },
},
});
return { imported: 0 };
}
const { imported, failed } = await syncUserFitbit(userId, { fullSync: true });
if (failed) {
// Surface through the pg-boss retry path (retryLimit 3, backoff, set by
// `integrationBackfillSourceOptions`). If the failure parked the connection
// at error_reauth meanwhile, the next attempt takes the reauth return above
// instead of failing again.
throw new Error(
`fitbit backfill incomplete for user ${userId} — marker not stamped`,
);
}
await prisma.fitbitConnection.update({
where: { userId },
data: { backfillCompletedAt: new Date() },
});
annotate({
action: {
name: "fitbit.backfill.complete",
details: { imported },
},
});
return { imported };
}
/**
* Boot-time discovery. Finds every Fitbit connection not yet backfilled
* (`backfill_completed_at IS NULL`) and enqueues one backfill job per account.
*
* Idempotent across reboots: once a connection's backfill completes,
* `backfillCompletedAt` is set and the predicate drops it from the discovery
* set. pg-boss `singletonKey` coalesces duplicate sends so a fast restart while
* a job is queued doesn't double up.
*
* Best-effort: errors are returned through the result value so the worker boot
* never fails because of a backfill miss.
*/
export async function enqueueBootTimeFitbitBackfill(
startAfterSeconds: number = 0,
): Promise<{
enqueued: number;
skipped: number;
error: string | null;
}> {
const boss = getGlobalBoss();
if (!boss) {
return { enqueued: 0, skipped: 0, error: null };
}
try {
const connections = await prisma.fitbitConnection.findMany({
where: { backfillCompletedAt: null },
select: { userId: true },
});
if (connections.length === 0) {
return { enqueued: 0, skipped: 0, error: null };
}
let enqueued = 0;
let skipped = 0;
for (const { userId } of connections) {
const payload: FitbitBackfillPayload = {
userId,
enqueuedAt: new Date().toISOString(),
};
const jobId = await boss.send(
FITBIT_BACKFILL_QUEUE,
payload,
integrationBackfillSourceOptions(
`fitbit-backfill|${userId}`,
startAfterSeconds,
),
);
if (jobId) {
enqueued += 1;
} else {
skipped += 1;
}
}
return { enqueued, skipped, error: null };
} catch (err) {
return {
enqueued: 0,
skipped: 0,
error: err instanceof Error ? err.message : String(err),
};
}
}