Skip to content
55 changes: 55 additions & 0 deletions docs/production-incident-baseline.md
Original file line number Diff line number Diff line change
Expand Up @@ -12880,3 +12880,58 @@ Drizzle schema and runtime Zod schemas. Findings and remediations:
summary rebuild still exceeds the local ClickHouse 3 GiB memory limit on the
recursive activity de-duplication CTE, so hosted CI remains the source of
truth for the full Stryker matrix.

## 2026-07-13 — Garmin Dump Import Stalled After Worker OOM

- **Symptoms:** BullMQ import job `5`, name `garmin-dump`, failed with
`job stalled more than allowable limit`.
- **User impact:** The Garmin account export import did not complete. Prior
Garmin dump import jobs `2`, `3`, and `4` had the same BullMQ failure reason.
- **Evidence:** Redis stream `bull:import:events` showed job `5` added and made
active at `2026-07-13T20:54:30.592Z`, stalled at
`2026-07-13T21:05:04.748Z`, became active again, then stalled and failed at
`2026-07-13T21:15:24.496Z`; Redis documents `XRANGE` as the command for
reading stream entries by ID range
([Redis XRANGE](https://redis.io/docs/latest/commands/xrange/)). The worker container
`dofek_worker.1.0exqzy2t768r9kfpmtbar0c0a` ran from
`2026-07-13T20:59:55.719Z` to `2026-07-13T21:05:09.463Z` with
`Exit=137` and `OOMKilled=true`, as reported by `docker inspect`
([Docker inspect CLI](https://docs.docker.com/reference/cli/docker/inspect/)). The uploaded Garmin dump ZIP remained in
`/app/job-files`, was about 48 MiB, had 111 top-level entries and about
78.5 MiB top-level uncompressed data, and contained two
`DI_CONNECT/DI-Connect-Uploaded-Files/*Part1.zip` nested ZIPs. Each nested ZIP
contained 7,887 FIT files totaling about 44 MiB uncompressed.
- **Root cause:** `src/providers/garmin-dump.ts` previously read the full uploaded ZIP
into memory, recursively buffered every extracted entry into arrays, including
unrelated Garmin account-export payloads and both large nested uploaded-file
ZIPs, and only extended the BullMQ import-job lock with a one-shot 10-minute
extension. The 48 MiB export expanded into thousands of buffered FIT entries
inside the 400 MiB worker container while the same process was also running
other provider workers. The first attempt was OOM-killed, which stopped lock
renewal. The retry again exceeded the one-shot lock window and BullMQ failed
the job after the second stall; BullMQ documents that jobs rely on worker lock
renewal and can be marked stalled, retried, and eventually failed when the
lock is not renewed
([BullMQ stalled jobs](https://docs.bullmq.io/guide/workers/stalled-jobs)).
- **Fix / mitigation:** Updated Garmin dump parsing to open ZIP files from the
filesystem, skip irrelevant Garmin export entries before opening their
streams, spill selected summary entries to temporary files under the job-files
volume, and create a BullMQ flow for selected FIT files. The flow uses a
generic ZIP-entry extraction job to materialize entry paths to files, ZIP-
agnostic FIT import child jobs to process those files, and a batch parent job
to aggregate child results. The import job lock is now renewed periodically
during the long-running import instead of relying only on a one-shot 10-minute
extension. BullMQ documents flows as parent-child job trees where parent jobs
wait for child jobs to finish before processing
([BullMQ flows](https://docs.bullmq.io/guide/flows)).
- **Validation:** Docker inspect confirmed the OOM kill, Redis import events
confirmed the two-stall/fail timeline, queue inspection showed no
`fit-file-import` jobs were active/failed at investigation time, and ZIP
inspection confirmed the nested uploaded-file FIT volume. Regression coverage
now verifies selective filesystem-backed Garmin parsing, one-flow FIT fan-out,
generic nested ZIP-entry extraction, FIT import resolution from flow child
results, batch result aggregation, queue resource caching, and worker
registration for the new queues.
- **Remaining risk / follow-up:** Medium until deployed and tested against the
original production Garmin dump. Increasing the worker memory limit alone
would still be a mitigation knob, not the root fix.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
39 changes: 39 additions & 0 deletions src/jobs/process-fit-file-import-batch-job.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { describe, expect, it } from "vitest";
import { processFitFileImportBatchJob } from "./process-fit-file-import-batch-job.ts";

describe("processFitFileImportBatchJob", () => {
it("aggregates records and errors from child FIT import jobs", async () => {
Comment thread
Asherlc marked this conversation as resolved.
Resolved
const result = await processFitFileImportBatchJob({
getChildrenValues: async () => ({
"bull:fit-file-import:1": { recordsSynced: 2, errors: [] },
"bull:fit-file-import:2": {
recordsSynced: 1,
errors: [{ message: "bad fit" }],
},
}),
});

expect(result).toEqual({
recordsSynced: 3,
errors: [{ message: "bad fit" }],
});
});

it("returns an empty result when the batch has no child values", async () => {
const result = await processFitFileImportBatchJob({
getChildrenValues: async () => ({}),
});

expect(result).toEqual({ recordsSynced: 0, errors: [] });
});

it("rejects malformed child results", async () => {
await expect(
processFitFileImportBatchJob({
getChildrenValues: async () => ({
"bull:fit-file-import:1": { recordsSynced: "2", errors: [] },
}),
}),
).rejects.toThrow();
});
});
25 changes: 25 additions & 0 deletions src/jobs/process-fit-file-import-batch-job.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { z } from "zod";
import type { FitFileImportJobResult } from "./process-fit-file-import-job.ts";

interface FitFileImportBatchJob {
getChildrenValues: () => Promise<Record<string, unknown>>;
}

const fitFileImportJobResultSchema = z.object({
recordsSynced: z.number(),
errors: z.array(z.object({ message: z.string() })),
});

export async function processFitFileImportBatchJob(
job: FitFileImportBatchJob,
): Promise<FitFileImportJobResult> {
const childValues = await job.getChildrenValues();
const results = Object.values(childValues).map((value) =>
fitFileImportJobResultSchema.parse(value),
);

return {
recordsSynced: results.reduce((total, result) => total + result.recordsSynced, 0),
errors: results.flatMap((result) => result.errors),
};
}
214 changes: 177 additions & 37 deletions src/jobs/process-fit-file-import-job.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,42 +162,94 @@ describe("processFitFileImportJob", () => {
);
});

it("rejects invalid queue payloads before reading a file", async () => {
await expect(
processFitFileImportJob(
createFitFileImportJob({
originalPath: "DI_CONNECT/activity.fit",
userId: "user-1",
providerId: "garmin-dump",
sourceName: "Garmin Dump",
}),
mockDb,
),
).rejects.toThrow();
it("returns an error result for jobs without a file path or extraction child", async () => {
const result = await processFitFileImportJob(
createFitFileImportJob({
originalPath: "DI_CONNECT/activity.fit",
userId: "user-1",
providerId: "garmin-dump",
sourceName: "Garmin Dump",
}),
mockDb,
);

expect(result).toEqual({
recordsSynced: 0,
errors: [
{
message:
"Failed to import FIT file DI_CONNECT/activity.fit: FIT import job is missing filePath and has no child extraction result",
},
],
});
expect(mockParseFitFileInWorkerThread).not.toHaveBeenCalled();
expect(mockUpsertProviderActivity).not.toHaveBeenCalled();
expect(mockCaptureException).toHaveBeenCalledWith(expect.any(Error), {
tags: { fitImportStep: "process" },
extra: { originalPath: "DI_CONNECT/activity.fit" },
});
});

it("cleans up temp files when queue payload validation fails", async () => {
it("cleans up temp files when queue payload validation returns an error result", async () => {
const filePath = await writeTempFit(createActivityFit());

await expect(
processFitFileImportJob(
createFitFileImportJob({
filePath,
originalPath: "DI_CONNECT/activity.fit",
userId: "user-1",
providerId: "garmin-dump",
}),
mockDb,
),
).rejects.toThrow();
const result = await processFitFileImportJob(
createFitFileImportJob({
filePath,
originalPath: "DI_CONNECT/activity.fit",
userId: "user-1",
providerId: "garmin-dump",
}),
mockDb,
);

expect(result.recordsSynced).toBe(0);
expect(result.errors[0]?.message).toContain(
"Failed to import FIT file DI_CONNECT/activity.fit:",
);
expect(mockParseFitFileInWorkerThread).not.toHaveBeenCalled();
await expect(readFile(filePath)).rejects.toThrow();
});

it("reports cleanup failures to Sentry", async () => {
const directory = await mkdtemp(join(tmpdir(), "fit-file-import-cleanup-failure-test-"));
createdDirectories.push(directory);

const result = await processFitFileImportJob(
createFitFileImportJob({
filePath: directory,
originalPath: "DI_CONNECT/activity.fit",
userId: "user-1",
providerId: "garmin-dump",
sourceName: "Garmin Dump",
}),
mockDb,
);

expect(result.recordsSynced).toBe(0);
expect(result.errors[0]?.message).toContain(
"Failed to import FIT file DI_CONNECT/activity.fit:",
);
expect(mockCaptureException).toHaveBeenCalledWith(expect.any(Error), {
tags: { fitImportStep: "cleanup" },
extra: { path: directory },
});
});

it("keeps originalPath for error messages when job data has extra fields", async () => {
const result = await processFitFileImportJob(
createFitFileImportJob({
originalPath: "DI_CONNECT/activity.fit",
ignored: "extra",
}),
mockDb,
);

expect(result.errors[0]?.message).toContain(
"Failed to import FIT file DI_CONNECT/activity.fit:",
);
});

it("imports an activity FIT file with a parent summary and replaces sensor samples", async () => {
const filePath = await writeTempFit(createActivityFit());

Expand Down Expand Up @@ -321,6 +373,86 @@ describe("processFitFileImportJob", () => {
);
});

it("imports a FIT file from a flow child extraction result", async () => {
const filePath = await writeTempFit(createActivityFit());
const getChildrenValues = vi.fn().mockResolvedValue({
"bull:zip-entry-extract:1": { filePath },
});

const result = await processFitFileImportJob(
{
data: {
originalPath: "DI_CONNECT/asher@example.com_activity.fit",
userId: "user-1",
providerId: "garmin-dump",
sourceName: "Garmin Dump",
},
getChildrenValues,
updateProgress: vi.fn().mockResolvedValue(undefined),
},
mockDb,
);

expect(result).toEqual({ recordsSynced: 1, errors: [] });
expect(getChildrenValues).toHaveBeenCalledOnce();
expect(mockUpsertProviderActivity).toHaveBeenCalledOnce();
await expect(readFile(filePath)).rejects.toThrow();
});

it("cleans up flow child extraction files when job data validation fails", async () => {
const filePath = await writeTempFit(createActivityFit());

const result = await processFitFileImportJob(
{
data: {
originalPath: "DI_CONNECT/asher@example.com_activity.fit",
userId: "user-1",
sourceName: "Garmin Dump",
},
getChildrenValues: async () => ({
"bull:zip-entry-extract:1": { filePath },
}),
updateProgress: vi.fn().mockResolvedValue(undefined),
},
mockDb,
);

expect(result.recordsSynced).toBe(0);
expect(result.errors[0]?.message).toContain(
"Failed to import FIT file DI_CONNECT/asher@example.com_activity.fit:",
);
await expect(readFile(filePath)).rejects.toThrow();
});

it("returns an error result for FIT flow jobs with multiple extraction results", async () => {
const result = await processFitFileImportJob(
{
data: {
originalPath: "DI_CONNECT/asher@example.com_activity.fit",
userId: "user-1",
providerId: "garmin-dump",
sourceName: "Garmin Dump",
},
getChildrenValues: async () => ({
"bull:zip-entry-extract:1": { filePath: "/tmp/one.fit" },
"bull:zip-entry-extract:2": { filePath: "/tmp/two.fit" },
}),
updateProgress: vi.fn().mockResolvedValue(undefined),
},
mockDb,
);

expect(result).toEqual({
recordsSynced: 0,
errors: [
{
message:
"Failed to import FIT file DI_CONNECT/asher@example.com_activity.fit: FIT import job expected 1 child extraction result, got 2",
},
],
});
});

it("extracts numeric activity IDs from FIT file names", async () => {
const filePath = await writeTempFit(createActivityFit());

Expand Down Expand Up @@ -611,22 +743,30 @@ describe("processFitFileImportJob", () => {
expect(mockParseFitFileInWorkerThread).not.toHaveBeenCalled();
});

it("fails invalid FIT files before attempting activity parsing", async () => {
it("returns an error result for invalid FIT files before attempting activity parsing", async () => {
const filePath = await writeTempFit(Buffer.from("not a fit file"));

await expect(
processFitFileImportJob(
createFitFileImportJob({
filePath,
originalPath: "DI_CONNECT/broken.fit",
userId: "user-1",
providerId: "garmin-dump",
sourceName: "Garmin Dump",
}),
mockDb,
),
).rejects.toThrow("FIT decoder reported");
const result = await processFitFileImportJob(
createFitFileImportJob({
filePath,
originalPath: "DI_CONNECT/broken.fit",
userId: "user-1",
providerId: "garmin-dump",
sourceName: "Garmin Dump",
}),
mockDb,
);

expect(result).toEqual({
recordsSynced: 0,
errors: [
expect.objectContaining({
message: expect.stringContaining(
"Failed to import FIT file DI_CONNECT/broken.fit: FIT decoder reported",
),
}),
],
});
expect(mockParseFitFileInWorkerThread).not.toHaveBeenCalled();
await expect(readFile(filePath)).rejects.toThrow();
});
Expand Down
Loading