pg-boss

Queueing jobs in Node.js using PostgreSQL like a boss

README

Queueing jobs in Node.js using PostgreSQL like a boss.
PostgreSql Version npm version Build Status Coverage Status

  1. ``` js
  2. async function readme() {
  3.   const PgBoss = require('pg-boss');
  4.   const boss = new PgBoss('postgres://user:pass@host/database');

  5.   boss.on('error', error => console.error(error));

  6.   await boss.start();

  7.   const queue = 'some-queue';

  8.   let jobId = await boss.send(queue, { param1: 'foo' })

  9.   console.log(`created job in queue ${queue}: ${jobId}`);

  10.   await boss.work(queue, someAsyncJobHandler);
  11. }

  12. async function someAsyncJobHandler(job) {
  13.   console.log(`job ${job.id} received with data:`);
  14.   console.log(JSON.stringify(job.data));

  15.   await doSomethingAsyncWithThis(job.data);
  16. }
  17. ```

pg-boss is a job queue built in Node.js on top of PostgreSQL in order to provide background processing and reliable asynchronous execution to Node.js applications.

pg-boss relies on SKIP LOCKED, a feature added to postgres specifically for message queues, in order to resolve record locking challenges inherent with relational databases. This brings the safety of guaranteed atomic commits of a relational database to your asynchronous job processing.

This will likely cater the most to teams already familiar with the simplicity of relational database semantics and operations (SQL, querying, and backups). It will be especially useful to those already relying on PostgreSQL that want to limit how many systems are required to monitor and support in their architecture.

Features

Exactly-once job delivery
Backpressure-compatible polling workers
Cron scheduling
Pub/sub API for fan-out queue relationships
Deferral, retries (with exponential backoff), rate limiting, debouncing
Completion jobs for orchestrations/sagas
Direct table access for bulk loads via COPY or INSERT
Multi-master compatible (for example, in a Kubernetes ReplicaSet)
Automatic creation and migration of storage tables
Automatic maintenance operations to manage table growth

Requirements

Node 14 or higher
PostgreSQL 9.5 or higher

Installation


  1. ``` bash
  2. # npm
  3. npm install pg-boss

  4. # yarn
  5. yarn add pg-boss
  6. ```

Documentation


Contributing


To setup a development environment for this library:

  1. ``` sh
  2. git clone https://github.com/timgit/pg-boss.git
  3. npm install

  4. ```

To run the test suite you will need to pgboss access to an empty postgres database. You can set one up using the following commands on a local postgres instance:

  1. ```sql
  2. CREATE DATABASE pgboss;
  3. CREATE user postgres WITH PASSWORD 'postgres';
  4. GRANT ALL PRIVILEGES ON DATABASE pgboss to postgres;
  5. -- run the following command in the context of the pgboss database
  6. CREATE EXTENSION pgcrypto;
  7. ```

If you use a different database name, username or password, or want to run the test suite against a database that is running on a remote machine then you will need to edit the test/config.json file with the appropriate connection values.

You can then run the linter and test suite using

  1. ``` sh
  2. npm test
  3. ```