Skip to content

Commit

Permalink
createQueue should not throw if it exists (#478)
Browse files Browse the repository at this point in the history
* createQueue() should not throw if queue already exists

* 10.0.4 versioning

* update readme [skip ci]
  • Loading branch information
timgit authored Aug 27, 2024
1 parent a7334db commit 80bcb42
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ async function readme() {
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.
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pg-boss",
"version": "10.0.3",
"version": "10.0.4",
"description": "Queueing jobs in Postgres from Node.js like a boss",
"main": "./src/index.js",
"engines": {
Expand Down
12 changes: 11 additions & 1 deletion src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,8 +226,10 @@ function createQueueFunction (schema) {
$$
DECLARE
table_name varchar := 'j' || encode(sha224(queue_name::bytea), 'hex');
queue_created_on timestamptz;
BEGIN
WITH q as (
INSERT INTO ${schema}.queue (
name,
policy,
Expand All @@ -249,7 +251,15 @@ function createQueueFunction (schema) {
(options->>'retentionMinutes')::int,
options->>'deadLetter',
table_name
);
)
ON CONFLICT DO NOTHING
RETURNING created_on
)
SELECT created_on into queue_created_on from q;
IF queue_created_on IS NULL THEN
RETURN;
END IF;
EXECUTE format('CREATE TABLE ${schema}.%I (LIKE ${schema}.job INCLUDING DEFAULTS)', table_name);
Expand Down
8 changes: 8 additions & 0 deletions test/queueTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@ describe('queues', function () {
await boss.createQueue(queue)
})

it('createQueue should work if queue already exists', async function () {
const boss = this.test.boss = await helper.start({ ...this.test.bossConfig, noDefault: true })
const queue = this.test.bossConfig.schema

await boss.createQueue(queue)
await boss.createQueue(queue)
})

it('should reject a queue with invalid characters', async function () {
const boss = this.test.boss = await helper.start({ ...this.test.bossConfig, noDefault: true })
const queue = `*${this.test.bossConfig.schema}`
Expand Down
5 changes: 4 additions & 1 deletion test/readme.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ async function readme () {

const queue = 'readme-queue'

await boss.deleteQueue(queue)
await boss.createQueue(queue)

const id = await boss.send(queue, { arg1: 'read me' })
Expand All @@ -27,3 +26,7 @@ async function readme () {
}

readme()
.catch(err => {
console.log(err)
process.exit(1)
})

0 comments on commit 80bcb42

Please sign in to comment.