From 877387e38e0cde85d9b4bc9324c81dc82669e895 Mon Sep 17 00:00:00 2001 From: Tim Jones Date: Tue, 27 Aug 2024 16:57:40 -0500 Subject: [PATCH] Retry limit default for vanilla SQL (#482) * add node namespace to core modules * migration for vanilla sql retry_limit default * versioning --- package-lock.json | 4 +-- package.json | 2 +- src/attorney.js | 2 +- src/boss.js | 2 +- src/contractor.js | 21 +++++++-------- src/db.js | 2 +- src/index.js | 2 +- src/manager.js | 6 ++--- src/migrationStore.js | 13 +++++++++- src/plans.js | 4 +-- src/timekeeper.js | 2 +- src/tools.js | 2 +- test/archiveTest.js | 2 +- test/backgroundErrorTest.js | 2 +- test/cancelTest.js | 2 +- test/completeTest.js | 2 +- test/configTest.js | 2 +- test/databaseTest.js | 2 +- test/delayTest.js | 2 +- test/deleteTest.js | 2 +- test/expireTest.js | 2 +- test/exportTest.js | 2 +- test/failureTest.js | 2 +- test/fetchTest.js | 2 +- test/insertTest.js | 2 +- test/maintenanceTest.js | 2 +- test/managerTest.js | 2 +- test/migrationTest.js | 52 ++++++++++++++++++++++++------------- test/moduleTest.js | 2 +- test/monitoringTest.js | 2 +- test/multiMasterTest.js | 4 +-- test/opsTest.js | 2 +- test/priorityTest.js | 2 +- test/publishTest.js | 2 +- test/queueTest.js | 2 +- test/resumeTest.js | 2 +- test/retryTest.js | 2 +- test/scheduleTest.js | 2 +- test/sendTest.js | 2 +- test/speedTest.js | 2 +- test/throttleTest.js | 2 +- test/workTest.js | 2 +- version.json | 2 +- 43 files changed, 101 insertions(+), 75 deletions(-) diff --git a/package-lock.json b/package-lock.json index 03cc2846..8676e1d1 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "pg-boss", - "version": "10.0.5", + "version": "10.0.6", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "pg-boss", - "version": "10.0.5", + "version": "10.0.6", "license": "MIT", "dependencies": { "cron-parser": "^4.0.0", diff --git a/package.json b/package.json index 8c7a0fc3..7842c7a4 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "pg-boss", - "version": "10.0.5", + "version": "10.0.6", "description": "Queueing jobs in Postgres from Node.js like a boss", "main": "./src/index.js", "engines": { diff --git a/src/attorney.js b/src/attorney.js index 9f51cdd0..500b057b 100644 --- a/src/attorney.js +++ b/src/attorney.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const { DEFAULT_SCHEMA } = require('./plans') const POLICY = { diff --git a/src/boss.js b/src/boss.js index 94c293f8..a86bf0a3 100644 --- a/src/boss.js +++ b/src/boss.js @@ -1,4 +1,4 @@ -const EventEmitter = require('events') +const EventEmitter = require('node:events') const plans = require('./plans') const { delay } = require('./tools') diff --git a/src/contractor.js b/src/contractor.js index 1ae7766e..5e190c67 100644 --- a/src/contractor.js +++ b/src/contractor.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const plans = require('./plans') const { DEFAULT_SCHEMA } = plans const migrationStore = require('./migrationStore') @@ -46,8 +46,7 @@ class Contractor { const version = await this.schemaVersion() if (schemaVersion > version) { - throw new Error('Migrations are not supported to v10') - // await this.migrate(version) + await this.migrate(version) } } else { await this.create() @@ -86,15 +85,15 @@ class Contractor { } } - // async next (version) { - // const commands = migrationStore.next(this.config.schema, version, this.migrations) - // await this.db.executeSql(commands) - // } + async next (version) { + const commands = migrationStore.next(this.config.schema, version, this.migrations) + await this.db.executeSql(commands) + } - // async rollback (version) { - // const commands = migrationStore.rollback(this.config.schema, version, this.migrations) - // await this.db.executeSql(commands) - // } + async rollback (version) { + const commands = migrationStore.rollback(this.config.schema, version, this.migrations) + await this.db.executeSql(commands) + } } module.exports = Contractor diff --git a/src/db.js b/src/db.js index 39a52fee..59759311 100644 --- a/src/db.js +++ b/src/db.js @@ -1,4 +1,4 @@ -const EventEmitter = require('events') +const EventEmitter = require('node:events') const pg = require('pg') class Db extends EventEmitter { diff --git a/src/index.js b/src/index.js index df998442..04f36865 100644 --- a/src/index.js +++ b/src/index.js @@ -1,4 +1,4 @@ -const EventEmitter = require('events') +const EventEmitter = require('node:events') const plans = require('./plans') const Attorney = require('./attorney') const Contractor = require('./contractor') diff --git a/src/manager.js b/src/manager.js index fddf46ea..caf72627 100644 --- a/src/manager.js +++ b/src/manager.js @@ -1,6 +1,6 @@ -const assert = require('assert') -const EventEmitter = require('events') -const { randomUUID } = require('crypto') +const assert = require('node:assert') +const EventEmitter = require('node:events') +const { randomUUID } = require('node:crypto') const { serializeError: stringify } = require('serialize-error') const { delay } = require('./tools') const Attorney = require('./attorney') diff --git a/src/migrationStore.js b/src/migrationStore.js index abe6cb25..cb080b75 100644 --- a/src/migrationStore.js +++ b/src/migrationStore.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const plans = require('./plans') module.exports = { @@ -64,5 +64,16 @@ function migrate (value, version, migrations) { function getAll (schema) { return [ + { + release: '10.0.6', + version: 22, + previous: 21, + install: [ + `ALTER TABLE ${schema}.job ALTER COLUMN retry_limit SET DEFAULT 2` + ], + uninstall: [ + `ALTER TABLE ${schema}.job ALTER COLUMN retry_limit SET DEFAULT 0` + ] + } ] } diff --git a/src/plans.js b/src/plans.js index 3ff342ce..a172c4e2 100644 --- a/src/plans.js +++ b/src/plans.js @@ -65,7 +65,7 @@ module.exports = { DEFAULT_SCHEMA } -const assert = require('assert') +const assert = require('node:assert') function create (schema, version) { const commands = [ @@ -179,7 +179,7 @@ function createTableJob (schema) { priority integer not null default(0), data jsonb, state ${schema}.job_state not null default('${JOB_STATES.created}'), - retry_limit integer not null default(0), + retry_limit integer not null default(2), retry_count integer not null default(0), retry_delay integer not null default(0), retry_backoff boolean not null default false, diff --git a/src/timekeeper.js b/src/timekeeper.js index 20a4462d..44ef93de 100644 --- a/src/timekeeper.js +++ b/src/timekeeper.js @@ -1,4 +1,4 @@ -const EventEmitter = require('events') +const EventEmitter = require('node:events') const plans = require('./plans') const cronParser = require('cron-parser') const Attorney = require('./attorney') diff --git a/src/tools.js b/src/tools.js index 7a04c3dc..42f12325 100644 --- a/src/tools.js +++ b/src/tools.js @@ -3,7 +3,7 @@ module.exports = { } function delay (ms, error) { - const { setTimeout } = require('timers/promises') + const { setTimeout } = require('node:timers/promises') const ac = new AbortController() const promise = new Promise((resolve, reject) => { diff --git a/test/archiveTest.js b/test/archiveTest.js index c45f54b4..466804b2 100644 --- a/test/archiveTest.js +++ b/test/archiveTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') const { delay } = require('../src/tools') const { JOB_STATES } = require('../src/plans') diff --git a/test/backgroundErrorTest.js b/test/backgroundErrorTest.js index f17291f0..0fe3eebf 100644 --- a/test/backgroundErrorTest.js +++ b/test/backgroundErrorTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const PgBoss = require('../') const { delay } = require('../src/tools') diff --git a/test/cancelTest.js b/test/cancelTest.js index b4ae2e4e..7d84e7b0 100644 --- a/test/cancelTest.js +++ b/test/cancelTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('cancel', function () { diff --git a/test/completeTest.js b/test/completeTest.js index e5969b03..3089278c 100644 --- a/test/completeTest.js +++ b/test/completeTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') const PgBoss = require('../') diff --git a/test/configTest.js b/test/configTest.js index 03b810f1..1909c538 100644 --- a/test/configTest.js +++ b/test/configTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const PgBoss = require('../') const helper = require('./testHelper') diff --git a/test/databaseTest.js b/test/databaseTest.js index caf974d0..c2d22e4b 100644 --- a/test/databaseTest.js +++ b/test/databaseTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const PgBoss = require('../') describe('database', function () { diff --git a/test/delayTest.js b/test/delayTest.js index 37a13983..b4770110 100644 --- a/test/delayTest.js +++ b/test/delayTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') const { delay } = require('../src/tools') diff --git a/test/deleteTest.js b/test/deleteTest.js index 3ebf8b2c..0c1b5d64 100644 --- a/test/deleteTest.js +++ b/test/deleteTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('delete', async function () { diff --git a/test/expireTest.js b/test/expireTest.js index 533c01c2..fc1b65f4 100644 --- a/test/expireTest.js +++ b/test/expireTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') const { delay } = require('../src/tools') diff --git a/test/exportTest.js b/test/exportTest.js index 433c4e46..827768e2 100644 --- a/test/exportTest.js +++ b/test/exportTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const PgBoss = require('../') const currentSchemaVersion = require('../version.json').schema diff --git a/test/failureTest.js b/test/failureTest.js index be1c4d15..983a49e3 100644 --- a/test/failureTest.js +++ b/test/failureTest.js @@ -1,5 +1,5 @@ const { delay } = require('../src/tools') -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('failure', function () { diff --git a/test/fetchTest.js b/test/fetchTest.js index bc583580..914bf2c3 100644 --- a/test/fetchTest.js +++ b/test/fetchTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('fetch', function () { diff --git a/test/insertTest.js b/test/insertTest.js index 05bf5199..e83296f6 100644 --- a/test/insertTest.js +++ b/test/insertTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const { randomUUID } = require('crypto') const helper = require('./testHelper') diff --git a/test/maintenanceTest.js b/test/maintenanceTest.js index db965277..d455b8bf 100644 --- a/test/maintenanceTest.js +++ b/test/maintenanceTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') const { delay } = require('../src/tools') diff --git a/test/managerTest.js b/test/managerTest.js index 15007be6..2531cc84 100644 --- a/test/managerTest.js +++ b/test/managerTest.js @@ -1,5 +1,5 @@ const { delay } = require('../src/tools') -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('manager', function () { diff --git a/test/migrationTest.js b/test/migrationTest.js index 2d95f863..8df40934 100644 --- a/test/migrationTest.js +++ b/test/migrationTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const PgBoss = require('../') const helper = require('./testHelper') const Contractor = require('../src/contractor') @@ -32,23 +32,39 @@ describe('migration', function () { } }) - it.skip('should migrate to previous version and back again', async function () { + it('should migrate to previous version and back again', async function () { const { contractor } = this.test await contractor.create() await contractor.rollback(currentSchemaVersion) - const oldVersion = await contractor.version() + const oldVersion = await contractor.schemaVersion() assert.notStrictEqual(oldVersion, currentSchemaVersion) await contractor.migrate(oldVersion) - const newVersion = await contractor.version() + const newVersion = await contractor.schemaVersion() assert.strictEqual(newVersion, currentSchemaVersion) }) - it.skip('should migrate to latest during start if on previous schema version', async function () { + it('should install next version via contractor', async function () { + const { contractor } = this.test + + await contractor.create() + + await contractor.rollback(currentSchemaVersion) + + const oneVersionAgo = await contractor.schemaVersion() + + await contractor.next(oneVersionAgo) + + const version = await contractor.schemaVersion() + + assert.strictEqual(version, currentSchemaVersion) + }) + + it('should migrate to latest during start if on previous schema version', async function () { const { contractor } = this.test await contractor.create() @@ -61,7 +77,7 @@ describe('migration', function () { await boss.start() - const version = await contractor.version() + const version = await contractor.schemaVersion() assert.strictEqual(version, currentSchemaVersion) }) @@ -88,22 +104,22 @@ describe('migration', function () { await boss.send(queue) await contractor.rollback(currentSchemaVersion) - const oneVersionAgo = await contractor.version() + const oneVersionAgo = await contractor.schemaVersion() assert.notStrictEqual(oneVersionAgo, currentSchemaVersion) await contractor.rollback(oneVersionAgo) - const twoVersionsAgo = await contractor.version() + const twoVersionsAgo = await contractor.schemaVersion() assert.notStrictEqual(twoVersionsAgo, oneVersionAgo) await contractor.next(twoVersionsAgo) - const oneVersionAgoPart2 = await contractor.version() + const oneVersionAgoPart2 = await contractor.schemaVersion() assert.strictEqual(oneVersionAgo, oneVersionAgoPart2) await contractor.next(oneVersionAgo) - const version = await contractor.version() + const version = await contractor.schemaVersion() assert.strictEqual(version, currentSchemaVersion) @@ -118,18 +134,18 @@ describe('migration', function () { await contractor.create() await contractor.rollback(currentSchemaVersion) - const oneVersionAgo = await contractor.version() + const oneVersionAgo = await contractor.schemaVersion() assert.strictEqual(oneVersionAgo, currentSchemaVersion - 1) await contractor.rollback(oneVersionAgo) - const twoVersionsAgo = await contractor.version() + const twoVersionsAgo = await contractor.schemaVersion() assert.strictEqual(twoVersionsAgo, currentSchemaVersion - 2) const config = { ...this.test.bossConfig } const boss = this.test.boss = new PgBoss(config) await boss.start() - const version = await contractor.version() + const version = await contractor.schemaVersion() assert.strictEqual(version, currentSchemaVersion) }) @@ -146,7 +162,7 @@ describe('migration', function () { } }) - it.skip('should roll back an error during a migration', async function () { + it('should roll back an error during a migration', async function () { const { contractor } = this.test const config = { ...this.test.bossConfig } @@ -158,7 +174,7 @@ describe('migration', function () { await contractor.create() await contractor.rollback(currentSchemaVersion) - const oneVersionAgo = await contractor.version() + const oneVersionAgo = await contractor.schemaVersion() const boss1 = new PgBoss(config) @@ -170,7 +186,7 @@ describe('migration', function () { await boss1.stop({ graceful: false, wait: false }) } - const version1 = await contractor.version() + const version1 = await contractor.schemaVersion() assert.strictEqual(version1, oneVersionAgo) @@ -181,7 +197,7 @@ describe('migration', function () { await boss2.start() - const version2 = await contractor.version() + const version2 = await contractor.schemaVersion() assert.strictEqual(version2, currentSchemaVersion) @@ -199,7 +215,7 @@ describe('migration', function () { } }) - it.skip('should not migrate if migrate option is false', async function () { + it('should not migrate if migrate option is false', async function () { const { contractor } = this.test await contractor.create() diff --git a/test/moduleTest.js b/test/moduleTest.js index 244eb2b3..b22e88e3 100644 --- a/test/moduleTest.js +++ b/test/moduleTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') describe('module', function () { it('should export states object', function () { diff --git a/test/monitoringTest.js b/test/monitoringTest.js index ad9ef23d..9c77e130 100644 --- a/test/monitoringTest.js +++ b/test/monitoringTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('monitoring', function () { diff --git a/test/multiMasterTest.js b/test/multiMasterTest.js index 6b63c2d4..02717c8f 100644 --- a/test/multiMasterTest.js +++ b/test/multiMasterTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') const PgBoss = require('../') const Contractor = require('../src/contractor') @@ -39,7 +39,7 @@ describe('multi-master', function () { await contractor.rollback(currentSchemaVersion) - const oldVersion = await contractor.version() + const oldVersion = await contractor.schemaVersion() assert.notStrictEqual(oldVersion, currentSchemaVersion) diff --git a/test/opsTest.js b/test/opsTest.js index c41f6a7a..cc1d2f28 100644 --- a/test/opsTest.js +++ b/test/opsTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') const { randomUUID } = require('crypto') diff --git a/test/priorityTest.js b/test/priorityTest.js index 231c05a2..5ab73f68 100644 --- a/test/priorityTest.js +++ b/test/priorityTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('priority', function () { diff --git a/test/publishTest.js b/test/publishTest.js index 18ecbce1..a7a5c799 100644 --- a/test/publishTest.js +++ b/test/publishTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('pubsub', function () { diff --git a/test/queueTest.js b/test/queueTest.js index 62504f4a..7e154a21 100644 --- a/test/queueTest.js +++ b/test/queueTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('queues', function () { diff --git a/test/resumeTest.js b/test/resumeTest.js index bedc55cc..d244642b 100644 --- a/test/resumeTest.js +++ b/test/resumeTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('cancel', function () { diff --git a/test/retryTest.js b/test/retryTest.js index aaf6b647..abadbed7 100644 --- a/test/retryTest.js +++ b/test/retryTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') const { delay } = require('../src/tools') diff --git a/test/scheduleTest.js b/test/scheduleTest.js index 5ce049d6..2592aab7 100644 --- a/test/scheduleTest.js +++ b/test/scheduleTest.js @@ -1,5 +1,5 @@ const { delay } = require('../src/tools') -const assert = require('assert') +const assert = require('node:assert') const { DateTime } = require('luxon') const helper = require('./testHelper') const plans = require('../src/plans') diff --git a/test/sendTest.js b/test/sendTest.js index e80c6f3c..cf01da2a 100644 --- a/test/sendTest.js +++ b/test/sendTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('send', function () { diff --git a/test/speedTest.js b/test/speedTest.js index 5a28495c..89cb34af 100644 --- a/test/speedTest.js +++ b/test/speedTest.js @@ -1,5 +1,5 @@ const helper = require('./testHelper') -const assert = require('assert') +const assert = require('node:assert') describe('speed', function () { const expectedSeconds = 9 diff --git a/test/throttleTest.js b/test/throttleTest.js index a3b46d97..7a11ff9b 100644 --- a/test/throttleTest.js +++ b/test/throttleTest.js @@ -1,4 +1,4 @@ -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') const { delay } = require('../src/tools') diff --git a/test/workTest.js b/test/workTest.js index 8ae97ccf..8a9e0b38 100644 --- a/test/workTest.js +++ b/test/workTest.js @@ -1,5 +1,5 @@ const { delay } = require('../src/tools') -const assert = require('assert') +const assert = require('node:assert') const helper = require('./testHelper') describe('work', function () { diff --git a/version.json b/version.json index a295918c..65ca78b4 100644 --- a/version.json +++ b/version.json @@ -1,3 +1,3 @@ { - "schema": 21 + "schema": 22 }