Skip to content

Commit

Permalink
Merge pull request #358 from abelsoares/enhancement/destroy-option
Browse files Browse the repository at this point in the history
Add support for destroy option
  • Loading branch information
timgit committed Jan 5, 2023
2 parents 6e7a077 + 1f315b1 commit 7fe7d48
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/readme.md
Expand Up @@ -438,6 +438,9 @@ By default, calling `stop()` without any arguments will gracefully wait for all

* `options`: object

* `destroy`, bool
Default: `false`. If `true` and the database connection is managed by pg-boss, it will destroy the connection pool.

* `graceful`, bool

Default: `true`. If `true`, the PgBoss instance will wait for any workers that are currently processing jobs to finish, up to the specified timeout. During this period, new jobs will not be processed, but active jobs will be allowed to finish.
Expand Down
6 changes: 5 additions & 1 deletion src/index.js
Expand Up @@ -126,7 +126,7 @@ class PgBoss extends EventEmitter {
this.emit(events.stopped)
}

let { graceful = true, timeout = 30000 } = options
let { destroy = false, graceful = true, timeout = 30000 } = options

timeout = Math.max(timeout, 1000)

Expand All @@ -139,6 +139,10 @@ class PgBoss extends EventEmitter {
this.stopped = true
this.stoppingOn = null

if (this.db.isOurs && this.db.opened && destroy) {
await this.db.close()
}

this.emit(events.stopped)
}

Expand Down
17 changes: 17 additions & 0 deletions test/opsTest.js
Expand Up @@ -63,6 +63,23 @@ describe('ops', function () {
await boss.stop({ graceful: false })
})

it('should destroy the connection pool', async function () {
const boss = this.test.boss = await helper.start({ ...this.test.bossConfig, ...defaults })
await boss.stop({ destroy: true, graceful: false })

assert(boss.db.pool.totalCount === 0)
})

it('should destroy the connection pool gracefully', async function () {
const boss = this.test.boss = await helper.start({ ...this.test.bossConfig, ...defaults })
await boss.stop({ destroy: true })
await new Promise((resolve) => {
boss.on('stopped', () => resolve())
})

assert(boss.db.pool.totalCount === 0)
})

it('should emit error during graceful stop if worker is busy', async function () {
const boss = await helper.start({ ...this.test.bossConfig, ...defaults, __test__throw_stop: true })
const queue = this.test.bossConfig.schema
Expand Down
1 change: 1 addition & 0 deletions types.d.ts
Expand Up @@ -242,6 +242,7 @@ declare namespace PgBoss {
}

interface StopOptions {
destroy?: boolean,
graceful?: boolean,
timeout?: number
}
Expand Down

0 comments on commit 7fe7d48

Please sign in to comment.