SPB Git
922 B · 13 lines typescript
1import { PgBoss } from "pg-boss";2let boss: PgBoss | null = null; let starting: Promise<PgBoss> | null = null;3export { QUEUES } from "./queues";4import { QUEUES } from "./queues";5export async function getBoss(): Promise<PgBoss> {6  if (boss) return boss;7  if (!starting) starting = (async () => { const b = new PgBoss({ connectionString: process.env.DATABASE_URL!, schema: "pgboss", max: 3 }); b.on("error", (e: Error) => console.error("pg-boss", e)); await b.start(); for (const q of Object.values(QUEUES)) await b.createQueue(q).catch(() => {}); boss = b; return b; })();8  return starting;9}10export async function enqueue<T extends object>(queue: string, data: T, opts: Record<string, unknown> = {}) {11  try { const b = await getBoss(); return await b.send(queue, data, { retryLimit: 3, retryDelay: 10, retryBackoff: true, ...opts } as any); } catch (e) { console.error("enqueue failed", queue, e); return null; }12}13
Files · main
Menu

Theme