Skip to content

Commit

Permalink
Retry limit default for vanilla SQL (#482)
Browse files Browse the repository at this point in the history
* add node namespace to core modules

* migration for vanilla sql retry_limit default

* versioning
  • Loading branch information
timgit authored Aug 27, 2024
1 parent ac94885 commit 877387e
Show file tree
Hide file tree
Showing 43 changed files with 101 additions and 75 deletions.
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.5",
"version": "10.0.6",
"description": "Queueing jobs in Postgres from Node.js like a boss",
"main": "./src/index.js",
"engines": {
Expand Down
2 changes: 1 addition & 1 deletion src/attorney.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const { DEFAULT_SCHEMA } = require('./plans')

const POLICY = {
Expand Down
2 changes: 1 addition & 1 deletion src/boss.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const EventEmitter = require('events')
const EventEmitter = require('node:events')
const plans = require('./plans')
const { delay } = require('./tools')

Expand Down
21 changes: 10 additions & 11 deletions src/contractor.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion src/db.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const EventEmitter = require('events')
const EventEmitter = require('node:events')
const pg = require('pg')

class Db extends EventEmitter {
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
6 changes: 3 additions & 3 deletions src/manager.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
13 changes: 12 additions & 1 deletion src/migrationStore.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const plans = require('./plans')

module.exports = {
Expand Down Expand Up @@ -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`
]
}
]
}
4 changes: 2 additions & 2 deletions src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ module.exports = {
DEFAULT_SCHEMA
}

const assert = require('assert')
const assert = require('node:assert')

function create (schema, version) {
const commands = [
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/timekeeper.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
2 changes: 1 addition & 1 deletion src/tools.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
2 changes: 1 addition & 1 deletion test/archiveTest.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
2 changes: 1 addition & 1 deletion test/backgroundErrorTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const PgBoss = require('../')
const { delay } = require('../src/tools')

Expand Down
2 changes: 1 addition & 1 deletion test/cancelTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const helper = require('./testHelper')

describe('cancel', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/completeTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const helper = require('./testHelper')
const PgBoss = require('../')

Expand Down
2 changes: 1 addition & 1 deletion test/configTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const PgBoss = require('../')
const helper = require('./testHelper')

Expand Down
2 changes: 1 addition & 1 deletion test/databaseTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const PgBoss = require('../')

describe('database', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/delayTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const helper = require('./testHelper')
const { delay } = require('../src/tools')

Expand Down
2 changes: 1 addition & 1 deletion test/deleteTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const helper = require('./testHelper')

describe('delete', async function () {
Expand Down
2 changes: 1 addition & 1 deletion test/expireTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const helper = require('./testHelper')
const { delay } = require('../src/tools')

Expand Down
2 changes: 1 addition & 1 deletion test/exportTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const PgBoss = require('../')
const currentSchemaVersion = require('../version.json').schema

Expand Down
2 changes: 1 addition & 1 deletion test/failureTest.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
2 changes: 1 addition & 1 deletion test/fetchTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const helper = require('./testHelper')

describe('fetch', function () {
Expand Down
2 changes: 1 addition & 1 deletion test/insertTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const { randomUUID } = require('crypto')
const helper = require('./testHelper')

Expand Down
2 changes: 1 addition & 1 deletion test/maintenanceTest.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const assert = require('assert')
const assert = require('node:assert')
const helper = require('./testHelper')
const { delay } = require('../src/tools')

Expand Down
2 changes: 1 addition & 1 deletion test/managerTest.js
Original file line number Diff line number Diff line change
@@ -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 () {
Expand Down
52 changes: 34 additions & 18 deletions test/migrationTest.js
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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()
Expand All @@ -61,7 +77,7 @@ describe('migration', function () {

await boss.start()

const version = await contractor.version()
const version = await contractor.schemaVersion()

assert.strictEqual(version, currentSchemaVersion)
})
Expand All @@ -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)

Expand All @@ -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)
})
Expand All @@ -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 }
Expand All @@ -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)

Expand All @@ -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)

Expand All @@ -181,7 +197,7 @@ describe('migration', function () {

await boss2.start()

const version2 = await contractor.version()
const version2 = await contractor.schemaVersion()

assert.strictEqual(version2, currentSchemaVersion)

Expand All @@ -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()
Expand Down
Loading

0 comments on commit 877387e

Please sign in to comment.