File size: 643 Bytes
6d6eaef
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import PgBoss from 'pg-boss';

// Single pg-boss instance per process, started lazily. pg-boss manages its own
// 'pgboss' schema in the same Postgres database — no Redis required.
let bossPromise: Promise<PgBoss> | null = null;

export async function getBoss(): Promise<PgBoss> {
  if (!bossPromise) {
    const connectionString = process.env.DATABASE_URL;
    if (!connectionString) {
      throw new Error('DATABASE_URL is not set');
    }
    const boss = new PgBoss({ connectionString });
    boss.on('error', (err) => console.error('[pg-boss] error', err));
    bossPromise = boss.start().then(() => boss);
  }
  return bossPromise;
}