Two related fixes from the architecture review: Typed job seam. Job payloads were `unknown` end-to-end: every handler opened with `job.data as SomePayload`, every enqueue site passed a bare string queue name and an unchecked object, and a typo meant a runtime failure in a background worker. packages/jobs gains defineJob(), which keeps the payload typed inside the handler and performs the contravariance cast once inside the package. The journal declares all 14 queues and their payload shapes in app/jobs/payloads.ts; enqueue()/ enqueueOptional() and defineJournalJob() key off that map, so enqueue sites and handlers cannot drift and queue names are compile-checked. Komoot credential bypass. The bulk-import route enqueued the raw credentials JSONB in the pg-boss payload, skipping withFreshCredentials entirely: credentials sat at rest in the job table, were never refreshed if stale, and markNeedsRelink never fired. The payload now carries only the serviceId; the handler resolves fresh credentials through the ConnectedServiceManager at execution time, and marks the import batch failed (instead of leaving it pending forever) when credential resolution itself fails. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
import { defineJournalJob } from "./payloads.ts";
|
|
import { and, lt, inArray, count } from "drizzle-orm";
|
|
import { getDb } from "../lib/db.ts";
|
|
import { importBatches, type ImportBatchStatus } from "@trails-cool/db/schema/journal";
|
|
import { logger } from "../lib/logger.server.ts";
|
|
|
|
const STALE_MS = 10 * 60 * 1000;
|
|
const STALE_STATUSES: ImportBatchStatus[] = ["pending", "running"];
|
|
|
|
export const importBatchesSweepJob = defineJournalJob({
|
|
name: "import-batches-sweep",
|
|
cron: "* * * * *",
|
|
retryLimit: 0,
|
|
expireInSeconds: 55,
|
|
async handler() {
|
|
const db = getDb();
|
|
const cutoff = new Date(Date.now() - STALE_MS);
|
|
const staleFilter = and(
|
|
inArray(importBatches.status, STALE_STATUSES),
|
|
lt(importBatches.startedAt, cutoff),
|
|
);
|
|
|
|
// Skip the write when nothing is stale to avoid an unconditional UPDATE every minute.
|
|
const rows = await db
|
|
.select({ staleCount: count() })
|
|
.from(importBatches)
|
|
.where(staleFilter);
|
|
if ((rows[0]?.staleCount ?? 0) === 0) return;
|
|
|
|
const result = await db
|
|
.update(importBatches)
|
|
.set({
|
|
status: "failed" satisfies ImportBatchStatus,
|
|
errorMessage: "Import timed out — the server may have restarted mid-import. Click 'Run again' to retry.",
|
|
completedAt: new Date(),
|
|
})
|
|
.where(staleFilter)
|
|
.returning({ id: importBatches.id });
|
|
|
|
logger.info({ count: result.length }, "import-batches-sweep: marked stale batches as failed");
|
|
},
|
|
});
|