import PgBoss from 'pg-boss'; export const QUEUE = 'generate'; export interface GenerateJob { itemId: string; jobId: string; } let boss: PgBoss | null = null; export async function getBoss(): Promise { if (!boss) { boss = new PgBoss({ connectionString: process.env.DATABASE_URL }); boss.on('error', (e) => console.error('[pg-boss]', e)); await boss.start(); await boss.createQueue(QUEUE); } return boss; } export async function enqueue(job: GenerateJob): Promise { const b = await getBoss(); await b.send(QUEUE, job, { retryLimit: 2, retryBackoff: true }); } export async function startWorker( concurrency: number, handler: (job: GenerateJob) => Promise, ): Promise { const b = await getBoss(); await b.work( QUEUE, { batchSize: Math.max(1, concurrency), pollingIntervalSeconds: 2 }, async (jobs) => { await Promise.all(jobs.map((j) => handler(j.data))); }, ); console.log(`[queue] Worker aktiv (Parallelität ${concurrency}).`); }