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>
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import { defineJournalJob } from "./payloads.ts";
|
|
import {
|
|
DEMO_BACKFILL_TARGET,
|
|
DEMO_DAILY_CAP,
|
|
countSyntheticRoutesRecent,
|
|
countSyntheticRoutesTotal,
|
|
ensureDemoUser,
|
|
generateOneWalk,
|
|
isDemoBotEnabled,
|
|
refreshDemoBotGauges,
|
|
shouldWalkNow,
|
|
} from "../lib/demo-bot.server.ts";
|
|
import { logger } from "../lib/logger.server.ts";
|
|
|
|
/**
|
|
* Generate job. Fires every 30 minutes via pg-boss cron. Handler steps:
|
|
*
|
|
* 1. Bail if `DEMO_BOT_ENABLED` is not "true".
|
|
* 2. On first run (no synthetic rows yet) produce a small backfill so the
|
|
* demo user's profile has content immediately.
|
|
* 3. Otherwise apply the decide-to-walk gate (local hour + p=0.09) and
|
|
* daily cap; on pass, insert one route+activity via `generateOneWalk`.
|
|
*/
|
|
export const demoBotGenerateJob = defineJournalJob({
|
|
name: "demo-bot-generate",
|
|
cron: "0,30 * * * *",
|
|
retryLimit: 1,
|
|
expireInSeconds: 120,
|
|
async handler() {
|
|
if (!isDemoBotEnabled()) return { skipped: "disabled" };
|
|
|
|
const ownerId = await ensureDemoUser();
|
|
|
|
const total = await countSyntheticRoutesTotal();
|
|
if (total === 0) {
|
|
let inserted = 0;
|
|
for (let i = 0; i < DEMO_BACKFILL_TARGET; i++) {
|
|
const id = await generateOneWalk(ownerId);
|
|
if (id) inserted++;
|
|
}
|
|
logger.info({ inserted }, "demo-bot backfill complete");
|
|
await refreshDemoBotGauges();
|
|
return { mode: "backfill", inserted };
|
|
}
|
|
|
|
const recent = await countSyntheticRoutesRecent(14);
|
|
if (recent >= DEMO_DAILY_CAP) {
|
|
logger.info({ recent, cap: DEMO_DAILY_CAP }, "demo-bot cap hit, skipping");
|
|
return { skipped: "cap", recent };
|
|
}
|
|
|
|
const now = new Date();
|
|
if (!shouldWalkNow(now)) return { skipped: "gate" };
|
|
|
|
const id = await generateOneWalk(ownerId, { now });
|
|
logger.info({ inserted: id ? 1 : 0, routeId: id }, "demo-bot walk");
|
|
await refreshDemoBotGauges();
|
|
return { mode: "single", routeId: id };
|
|
},
|
|
});
|