timgit / pg-boss

Queueing jobs in Node.js using PostgreSQL like a boss
MIT License
1.79k stars 149 forks source link

Example fan-out/pub-sub code? #351

Closed djvs closed 1 year ago

djvs commented 1 year ago

I've kind of lost track of the API at this point and it's not obvious to me from the docs how to do a fan-out with something of a guarantee that anyone subscribed to "foobar" will get every message published to "foobar". My shotgun approach attempt below:

export const runBoss = async () => {
  // ...
  const bossRaw = new PgBoss(options)
  const instance = bossRaw.start()
  return instance
}

export const pgBoss = runBoss()

export const publish = async (event: string, data: object) => {
  const boss = await pgBoss
  await boss.publish(event, data)
}

export const subscribe = async (event: string, callback: (data: any) => void) => {
  const boss = await pgBoss
  const queueName = `${event}-queue`
  boss.work(
    queueName,
    {
      teamSize: 30,
      teamConcurrency: 30,
      batchSize: 30,
      newJobCheckInterval: 500,
    },
    callback,
  )
  await boss.subscribe(event, queueName)
}

Running this locally, maybe 1/3 of the time it will publish a message but not run the callback.

timgit commented 1 year ago

Try using send() to create a job and work() to poll for jobs and run the callback function like in the readme. Pub/Sub is a composition pattern when you want to have 2 queues run from a single event.

djvs commented 1 year ago

That's what I mean - I want every listening worker to process the event.

djvs commented 1 year ago

It's apparent that publish and subscribe are probably the wrong choice here - is there a sane architecture to do this with pg-boss though?

Appears not to be the case and like I should be using pg-pubsub or equivalent for this use case, and pg-boss for conventional jobs.

timgit commented 1 year ago

The architecture of pg-boss is similar to services like AWS SQS and less similar to services like SNS. Pub/Sub was added to pg-boss as a fan-out enhancement to create jobs in queues. While you could decrease the polling interval for your workers to 100ms, it's not designed for low latency.