graphile-worker
Advanced tools
Comparing version 0.1.0-alpha.0 to 0.1.0
@@ -13,3 +13,3 @@ "use strict"; | ||
try { | ||
assert(!!options.taskDirectory !== !!options.taskList, "Exactly one of either taskDirectory or taskList should be set"); | ||
assert(!options.taskDirectory || !options.taskList, "Exactly one of either `taskDirectory` or `taskList` should be set"); | ||
let taskList; | ||
@@ -27,3 +27,3 @@ if (options.taskList) { | ||
} | ||
assert(!!options.pgPool !== !!options.connectionString, "Exactly one of either pgPool or connectionString should be set"); | ||
assert(!options.pgPool || !options.connectionString, "Both `pgPool` and `connectionString` are set, at most one of these options should be provided"); | ||
let pgPool; | ||
@@ -44,2 +44,14 @@ if (options.pgPool) { | ||
} | ||
pgPool.on("error", err => { | ||
/* | ||
* This handler is required so that client connection errors don't bring | ||
* the server down (via `unhandledError`). | ||
* | ||
* `pg` will automatically terminate the client and remove it from the | ||
* pool, so we don't actually need to take any action here, just ensure | ||
* that the event listener is registered. | ||
*/ | ||
// eslint-disable-next-line no-console | ||
console.error("PostgreSQL client generated error: ", err.message); | ||
}); | ||
const withPgClient = helpers_1.makeWithPgClientFromPool(pgPool); | ||
@@ -46,0 +58,0 @@ // Migrate |
{ | ||
"name": "graphile-worker", | ||
"version": "0.1.0-alpha.0", | ||
"version": "0.1.0", | ||
"description": "Job queue for PostgreSQL", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
102
README.md
@@ -15,2 +15,17 @@ # graphile-worker | ||
## Crowd-funded open-source software | ||
To help us develop this software sustainably under the MIT license, we ask | ||
all individuals and businesses that use it to help support its ongoing | ||
maintenance and development via sponsorship. | ||
### [Click here to find out more about sponsors and sponsorship.](https://www.graphile.org/sponsor/) | ||
And please give some love to our featured sponsors 🤩: | ||
<table><tr> | ||
<td align="center"><a href="http://chads.website/"><img src="https://www.graphile.org/images/sponsors/chadf.png" width="90" height="90" alt="Chad Furman" /><br />Chad Furman</a></td> | ||
<td align="center"><a href="https://timescale.com/"><img src="https://www.graphile.org/images/sponsors/timescale.svg" width="90" height="90" alt="Timescale" /><br />Timescale</a></td> | ||
</tr></table> | ||
## Quickstart: CLI | ||
@@ -71,16 +86,23 @@ | ||
```js | ||
import { run } from "graphile-worker"; | ||
const { run } = require("graphile-worker"); | ||
const runner = await run({ | ||
connectionString: "postgres:///", | ||
concurrency: 5, | ||
pollInterval: 1000, | ||
// you can set the taskList or taskDirectory but not both | ||
taskList: { | ||
testTask: async (payload, helpers) => { | ||
console.log("working on task..."); | ||
async function main() { | ||
const runner = await run({ | ||
connectionString: "postgres:///", | ||
concurrency: 5, | ||
pollInterval: 1000, | ||
// you can set the taskList or taskDirectory but not both | ||
taskList: { | ||
testTask: async (payload, helpers) => { | ||
console.log("working on task..."); | ||
}, | ||
}, | ||
}, | ||
// or: | ||
// taskDirectory: `${__dirname}/tasks`, | ||
// or: | ||
// taskDirectory: `${__dirname}/tasks`, | ||
}); | ||
} | ||
main().catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
@@ -139,2 +161,3 @@ ``` | ||
you have them installed in a different schema (unlikely) you may face issues. | ||
Making alias functions in the public schema, should solve this issue. | ||
@@ -181,2 +204,57 @@ \* Might work with older versions, but has not been tested. | ||
## Library usage | ||
`graphile-worker` can be used as a library inside your Node.js application. It exposes the `run(options)` and `runOnce(options)` functions. | ||
`run(options)` will run until either stopped by a signal event like `SIGINT` or by calling the `stop()` function on the object returned by `run()`. | ||
`runOnce(options)` is the equivalent of running the cli with the `--once` option. The function will run until there are no runnable jobs left. | ||
The following options for both methods are available. | ||
- `concurrency`: The equivalent of the cli `--jobs` option with the same default value. | ||
- `pollInterval`: The equivalent of the cli `--poll-interval` option with the same default value. | ||
- the database is identified through one of these options: | ||
- `connectionString`: A PostgreSQL connection string to the database containing the job queue, or | ||
- `pgPool`: A `pg.Pool` instance to use | ||
- the tasks to execute are identified through one of these options: | ||
- `taskDirectory`: A path string to a directory containing the task handlers. | ||
- `taskList`: An object with the task names as keys and a corresponding task handler functions as values | ||
Exactly one of either `taskDirectory` or `taskList` must be provided. The same applies to `connectionString` and `pgPool`. | ||
**Example** | ||
```js | ||
const { Pool } = require("pg"); | ||
const { run } = require("graphile-worker"); | ||
const pgPool = new Pool({ | ||
connectionString: "postgres://postgres:postgres@localhost:5432/postgres", | ||
}); | ||
async function main() { | ||
const runner = await run({ | ||
pgPool, | ||
// or: connectionString: process.env.DATABASE_URL, | ||
concurrency: 1, | ||
pollInterval: 2000, | ||
taskList: { | ||
testTask: async (payload, { debug }) => { | ||
debug(`Received ${JSON.stringify(payload)}`); | ||
}, | ||
}, | ||
// or: taskDirectory: `${__dirname}/tasks`, | ||
}); | ||
// to clean up: runner.stop() | ||
} | ||
main().catch(err => { | ||
console.error(err); | ||
process.exit(1); | ||
}); | ||
``` | ||
## Creating task executors | ||
@@ -183,0 +261,0 @@ |
Sorry, the diff of this file is not supported yet
92631
1055
525