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>
41 lines
1.6 KiB
TypeScript
41 lines
1.6 KiB
TypeScript
import { defineJob, type JobDefinition, type TypedJobDefinition } from "@trails-cool/jobs";
|
|
import type { DeliveryPayload } from "../lib/federation-delivery.server.ts";
|
|
import type { GarminImportData } from "../lib/connected-services/providers/garmin/import.server.ts";
|
|
|
|
/**
|
|
* Every journal job queue and its payload shape, in one place. The
|
|
* typed `enqueue` / `enqueueOptional` in boss.server.ts and the
|
|
* `defineJournalJob` helper below both key off this map, so an enqueue
|
|
* site and its handler cannot drift apart, and a queue-name typo is a
|
|
* compile error instead of an orphaned queue.
|
|
*
|
|
* `void` marks cron-only jobs that are never enqueued with data.
|
|
*/
|
|
export interface JobPayloads {
|
|
"backfill-user-keypairs": Record<string, never>;
|
|
"consumed-jti-sweep": void;
|
|
"deliver-activity": DeliveryPayload;
|
|
"demo-bot-generate": void;
|
|
"demo-bot-prune": void;
|
|
"federation-kv-sweep": void;
|
|
"garmin-import-activity": GarminImportData;
|
|
"import-batches-sweep": void;
|
|
"komoot-bulk-import": { batchId: string; userId: string; serviceId: string };
|
|
"notifications-fanout": { activityId: string };
|
|
"notifications-purge": void;
|
|
"poll-remote-actor": { actorIri: string };
|
|
"poll-remote-outboxes": void;
|
|
"send-welcome-email": { email: string; username: string };
|
|
}
|
|
|
|
export type JobName = keyof JobPayloads;
|
|
|
|
/**
|
|
* defineJob, constrained to the journal's queue map: the name must be
|
|
* a known queue and the handler's payload type follows from it.
|
|
*/
|
|
export function defineJournalJob<K extends JobName>(
|
|
definition: TypedJobDefinition<JobPayloads[K]> & { name: K },
|
|
): JobDefinition {
|
|
return defineJob<JobPayloads[K]>(definition);
|
|
}
|