Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

createQueue should not throw if it exists #478

Merged
merged 3 commits into from
Aug 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
})