trails/packages/jobs/src/worker.ts
Ullrich Schäfer f75dd12f59 Fix pg-boss queue creation before work registration
pg-boss v10 requires explicit queue creation via `createQueue()` before
`schedule()` or `work()` can reference a queue. Without this, the planner
crashes on startup in fresh databases (like CI) with "Queue not found".

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
2026-04-14 09:08:19 +02:00

30 lines
692 B
TypeScript

import type PgBoss from "pg-boss";
import type { JobDefinition } from "./types.ts";
export async function startWorker(
boss: PgBoss,
jobs: JobDefinition[],
): Promise<void> {
await boss.start();
for (const job of jobs) {
await boss.createQueue(job.name);
if (job.cron) {
await boss.schedule(job.name, job.cron, undefined, {
retryLimit: job.retryLimit,
expireInSeconds: job.expireInSeconds,
});
}
await boss.work(job.name, job.handler);
}
const onShutdown = async () => {
await boss.stop({ graceful: true, timeout: 10_000 });
process.exit(0);
};
process.on("SIGTERM", onShutdown);
process.on("SIGINT", onShutdown);
}