fix(labs): stop the biomarker backfill on its job budget

The integration-backfill drain handed its job budget only to the
Google Health backfill. The lab-biomarker backfill now takes it too,
asks it before each analyte group and, once it says stop, throws
without touching the rest. Finished groups are linked and drop out of
the next scan, so the retry resumes where the pass stopped instead of
running on past the job's expiry beside its own retry.
This commit is contained in:
Marc-André Bombeck committed 2026-10-03 18:53:28 +02:00
1 parent 9455b64375
commit c3525cd02f
3 files changed
+99 -1

No files matched your search

+4 -1
View File
@@ -115,7 +115,10 @@ async function runAdmitted(
return;
}
case "lab-biomarker-backfill": {
const { markers, linked } = await runLabBiomarkerBackfillForUser(userId);
const { markers, linked } = await runLabBiomarkerBackfillForUser(
userId,
shouldStop,
);
totals.markers += markers;
totals.linked += linked;
workerLog(
+19
View File
@@ -103,9 +103,16 @@ export function groupRowsByAnalyte(rows: LegacyRow[]): AnalyteGroup[] {
* Backfill one user: group un-linked live readings, create/reuse a Biomarker
* per group, link the rows. Returns the count of markers touched + rows
* linked.
*
* `shouldStop` is the admission job's budget (`jobBudget`). It is asked
* before each analyte group; once it says stop, the pass throws without
* touching the rest, like the Google Health backfill does. Each group commits
* on its own and a linked reading drops out of the scan above, so the retry
* pg-boss schedules for the failed job starts where this one stopped.
*/
export async function runLabBiomarkerBackfillForUser(
userId: string,
shouldStop: () => boolean = () => false,
): Promise<{ markers: number; linked: number }> {
const rows = await prisma.labResult.findMany({
where: { userId, deletedAt: null, biomarkerId: null },
@@ -129,6 +136,18 @@ export async function runLabBiomarkerBackfillForUser(
let markers = 0;
let linked = 0;
for (const group of groups) {
if (shouldStop()) {
if (linked > 0) invalidateUserHealthScore(userId);
annotate({
action: {
name: "labs.biomarker.backfill.stopped",
details: { markers, linked, groupsLeft: groups.length - markers },
},
});
throw new Error(
`lab-biomarker backfill for user ${userId} stopped on its job budget after ${markers} of ${groups.length} groups; the retry resumes`,
);
}
// Re-use a hand-defined marker of the same name if one exists; otherwise
// mint it. `(userId, name)` is unique, so a concurrent re-run converges.
const biomarker = await prisma.biomarker.upsert({