-
-
Notifications
You must be signed in to change notification settings - Fork 164
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
18 changed files
with
983 additions
and
914 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
Queueing jobs in Postgres from Node.js like a boss. | ||
|
||
[![npm version](https://badge.fury.io/js/pg-boss.svg)](https://badge.fury.io/js/pg-boss) | ||
[![Build](https://github.com/timgit/pg-boss/actions/workflows/ci.yml/badge.svg?branch=master)](https://github.com/timgit/pg-boss/actions/workflows/ci.yml) | ||
[![Coverage Status](https://coveralls.io/repos/github/timgit/pg-boss/badge.svg?branch=master)](https://coveralls.io/github/timgit/pg-boss?branch=master) | ||
|
||
```js | ||
async function readme() { | ||
const PgBoss = require('pg-boss'); | ||
const boss = new PgBoss('postgres://user:pass@host/database'); | ||
|
||
boss.on('error', console.error) | ||
|
||
await boss.start() | ||
|
||
const queue = 'readme-queue' | ||
|
||
await boss.createQueue(queue) | ||
|
||
const id = await boss.send(queue, { arg1: 'read me' }) | ||
|
||
console.log(`created job ${id} in queue ${queue}`) | ||
|
||
await boss.work(queue, async ([ job ]) => { | ||
console.log(`received job ${job.id} with data ${JSON.stringify(job.data)}`) | ||
}) | ||
} | ||
|
||
readme() | ||
.catch(err => { | ||
console.log(err) | ||
process.exit(1) | ||
}) | ||
``` | ||
|
||
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](https://www.2ndquadrant.com/en/blog/what-is-select-skip-locked-for-in-postgresql-9-5/), a feature built specifically for message queues to resolve record locking challenges inherent with relational databases. This provides exactly-once delivery and the safety of guaranteed atomic commits to 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. | ||
|
||
|
||
## Summary <!-- {docsify-ignore-all} --> | ||
* Exactly-once job delivery | ||
* Create jobs within your existing database transaction | ||
* Backpressure-compatible polling workers | ||
* Cron scheduling | ||
* Queue storage policies to support a variety of rate limiting, debouncing, and concurrency use cases | ||
* Priority queues, dead letter queues, job deferral, automatic retries with exponential backoff | ||
* Pub/sub API for fan-out queue relationships | ||
* Raw SQL support for non-Node.js runtimes via INSERT or COPY | ||
* Serverless function compatible | ||
* Multi-master compatible (for example, in a Kubernetes ReplicaSet) | ||
|
||
## Requirements | ||
* Node 20 or higher | ||
* PostgreSQL 13 or higher | ||
|
||
## Installation | ||
|
||
```bash | ||
# npm | ||
npm install pg-boss | ||
|
||
# yarn | ||
yarn add pg-boss | ||
``` | ||
|
||
## Documentation | ||
* [Docs](https://timgit.github.io/pg-boss/) | ||
|
||
## Contributing | ||
To setup a development environment for this library: | ||
|
||
```bash | ||
git clone https://github.com/timgit/pg-boss.git | ||
npm install | ||
``` | ||
|
||
To run the test suite, linter and code coverage: | ||
```bash | ||
npm run cover | ||
``` | ||
|
||
The test suite will try and create a new database named pgboss. The [config.json](test/config.json) file has the default credentials to connect to postgres. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
* [Home](/) | ||
* [Introduction](introduction.md) | ||
* [Install](install.md) | ||
* API | ||
* * [Constructor](./api/constructor.md) | ||
* * [Events](./api/events.md) | ||
* * [Operations](./api/ops.md) | ||
* * [Queues](./api/queues.md) | ||
* * [Jobs](./api/jobs.md) | ||
* * [Scheduling](./api/scheduling.md) | ||
* * [PubSub](./api/pubsub.md) | ||
* * [Workers](./api/workers.md) | ||
* * [Utils](./api/utils.md) | ||
* [SQL](sql.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
# Constructor | ||
|
||
### `new(connectionString)` | ||
|
||
Passing a string argument to the constructor implies a PostgreSQL connection string in one of the formats specified by the [pg](https://github.com/brianc/node-postgres) package. Some examples are currently posted in the [pg docs](https://github.com/brianc/node-postgres/wiki/pg). | ||
|
||
```js | ||
const boss = new PgBoss('postgres://user:pass@host:port/database?ssl=require'); | ||
``` | ||
|
||
### `new(options)` | ||
|
||
The following options can be set as properties in an object for additional configurations. | ||
|
||
**Connection options** | ||
|
||
* **host** - string, defaults to "127.0.0.1" | ||
|
||
* **port** - int, defaults to 5432 | ||
|
||
* **ssl** - boolean or object | ||
|
||
* **database** - string, *required* | ||
|
||
* **user** - string, *required* | ||
|
||
* **password** - string | ||
|
||
* **connectionString** - string | ||
|
||
PostgreSQL connection string will be parsed and used instead of `host`, `port`, `ssl`, `database`, `user`, `password`. | ||
|
||
* **max** - int, defaults to 10 | ||
|
||
Maximum number of connections that will be shared by all operations in this instance | ||
|
||
* **application_name** - string, defaults to "pgboss" | ||
|
||
* **db** - object | ||
|
||
Passing an object named db allows you "bring your own database connection". This option may be beneficial if you'd like to use an existing database service with its own connection pool. Setting this option will bypass the above configuration. | ||
|
||
The expected interface is a function named `executeSql` that allows the following code to run without errors. | ||
|
||
|
||
```js | ||
const text = "select $1 as input" | ||
const values = ['arg1'] | ||
|
||
const { rows } = await executeSql(text, values) | ||
|
||
assert(rows[0].input === 'arg1') | ||
``` | ||
|
||
* **schema** - string, defaults to "pgboss" | ||
|
||
Database schema that contains all required storage objects. Only alphanumeric and underscore allowed, length: <= 50 characters | ||
|
||
|
||
**Maintenance options** | ||
|
||
Maintenance operations include checking active jobs for expiration, caching queue stats, and deleting completed jobs. | ||
|
||
* **supervise**, bool, default true | ||
|
||
If this is set to false, maintenance and monitoring operations will be disabled on this instance. This is an advanced use case, as bypassing maintenance operations is not something you would want to do under normal circumstances. | ||
|
||
* **schedule**, bool, default true | ||
|
||
If this is set to false, this instance will not monitor or created scheduled jobs during. This is an advanced use case you may want to do for testing or if the clock of the server is skewed and you would like to disable the skew warnings. | ||
|
||
* **migrate**, bool, default true | ||
|
||
If this is set to false, this instance will skip attempts to run schema migratations during `start()`. If schema migrations exist, `start()` will throw and error and block usage. This is an advanced use case when the configured user account does not have schema mutation privileges. | ||
|
||
|
||
**Maintenance interval** | ||
|
||
How often maintenance operations are run. | ||
|
||
* **maintenanceIntervalSeconds**, int | ||
|
||
maintenance interval in seconds, must be >=1 | ||
|
||
* Default: 60 seconds |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Events | ||
|
||
Each pg-boss instance is an EventEmitter, and contains the following events. | ||
|
||
## `error` | ||
The `error` event could be raised during internal processing, such as scheduling and maintenance. Adding a listener to the error event is strongly encouraged because of the default behavior of Node. | ||
|
||
> If an EventEmitter does not have at least one listener registered for the 'error' event, and an 'error' event is emitted, the error is thrown, a stack trace is printed, and the Node.js process exits. | ||
> | ||
>Source: [Node.js Events > Error Events](https://nodejs.org/api/events.html#events_error_events) | ||
Ideally, code similar to the following example would be used after creating your instance, but before `start()` is called. | ||
|
||
```js | ||
boss.on('error', error => logger.error(error)); | ||
``` | ||
## `warning` | ||
|
||
During monitoring and maintenance, pg-boss may raise warning events. | ||
|
||
Examples are slow queries, large queues, and scheduling clock skew. | ||
|
||
## `wip` | ||
|
||
Emitted at most once every 2 seconds when workers are receiving jobs. The payload is an array that represents each worker in this instance of pg-boss. | ||
|
||
```js | ||
[ | ||
{ | ||
id: 'fc738fb0-1de5-4947-b138-40d6a790749e', | ||
name: 'my-queue', | ||
options: { pollingInterval: 2000 }, | ||
state: 'active', | ||
count: 1, | ||
createdOn: 1620149137015, | ||
lastFetchedOn: 1620149137015, | ||
lastJobStartedOn: 1620149137015, | ||
lastJobEndedOn: null, | ||
lastJobDuration: 343 | ||
lastError: null, | ||
lastErrorOn: null | ||
} | ||
] | ||
``` | ||
|
||
## `stopped` | ||
|
||
Emitted after `stop()` once all workers have completed their work and maintenance has been shut down. |
Oops, something went wrong.