Skip to content

Commit

Permalink
Merge pull request #366 from timgit/failed-archive-config
Browse files Browse the repository at this point in the history
Failed archive config
  • Loading branch information
timgit authored Feb 8, 2023
2 parents a15aaaa + f549669 commit 32f5ba2
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 71 deletions.
6 changes: 6 additions & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,12 @@ Queue options contain the following constructor-only settings.

Default: 12 hours

* **archiveFailedAfterSeconds**

Specifies how long in seconds failed jobs get archived. Note: a warning will be emitted if set to lower than 60s and cron processing will be disabled.

Default: `archiveCompletedAfterSeconds`

**Monitoring options**

* **monitorStateIntervalSeconds** - int, default undefined
Expand Down
116 changes: 58 additions & 58 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": "8.3.1",
"version": "8.4.0",
"description": "Queueing jobs in Node.js using PostgreSQL like a boss",
"main": "./src/index.js",
"engines": {
Expand Down
14 changes: 14 additions & 0 deletions src/attorney.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@ function getConfig (value) {
applyDatabaseConfig(config)
applyMaintenanceConfig(config)
applyArchiveConfig(config)
applyArchiveFailedConfig(config)
applyDeleteConfig(config)
applyMonitoringConfig(config)
applyUuidConfig(config)
Expand Down Expand Up @@ -195,6 +196,19 @@ function applyArchiveConfig (config) {
}
}

function applyArchiveFailedConfig (config) {
assert(!('archiveFailedAfterSeconds' in config) || config.archiveFailedAfterSeconds >= 1,
'configuration assert: archiveFailedAfterSeconds must be at least every second and less than ')

config.archiveFailedSeconds = config.archiveFailedAfterSeconds || config.archiveSeconds
config.archiveFailedInterval = `${config.archiveFailedSeconds} seconds`

// Do not emit warning twice
if (config.archiveFailedSeconds < 60 && config.archiveSeconds >= 60) {
emitWarning(WARNINGS.CRON_DISABLED)
}
}

function applyCompletionConfig (config, defaults) {
assert(!('onComplete' in config) || config.onComplete === true || config.onComplete === false,
'configuration assert: onComplete must be either true or false')
Expand Down
2 changes: 1 addition & 1 deletion src/boss.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class Boss extends EventEmitter {
this.events = events

this.expireCommand = plans.locked(config.schema, plans.expire(config.schema))
this.archiveCommand = plans.locked(config.schema, plans.archive(config.schema, config.archiveInterval))
this.archiveCommand = plans.locked(config.schema, plans.archive(config.schema, config.archiveInterval, config.archiveFailedInterval))
this.purgeCommand = plans.locked(config.schema, plans.purge(config.schema, config.deleteAfter))
this.getMaintenanceTimeCommand = plans.getMaintenanceTime(config.schema)
this.setMaintenanceTimeCommand = plans.setMaintenanceTime(config.schema)
Expand Down
12 changes: 8 additions & 4 deletions src/plans.js
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ function insertJobs (schema) {
keepUntil,
on_complete
)
SELECT
SELECT
COALESCE(id, gen_random_uuid()) as id,
name,
data,
Expand Down Expand Up @@ -639,12 +639,16 @@ function purge (schema, interval) {
`
}

function archive (schema, interval) {
function archive (schema, completedInterval, failedInterval = completedInterval) {
return `
WITH archived_rows AS (
DELETE FROM ${schema}.job
WHERE
completedOn < (now() - interval '${interval}')
WHERE (
state <> '${states.failed}' AND completedOn < (now() - interval '${completedInterval}')
)
OR (
state = '${states.failed}' AND completedOn < (now() - interval '${failedInterval}')
)
OR (
state < '${states.active}' AND keepUntil < now()
)
Expand Down
2 changes: 1 addition & 1 deletion src/timekeeper.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Timekeeper extends EventEmitter {

async start () {
// setting the archive config too low breaks the cron 60s debounce interval so don't even try
if (this.config.archiveSeconds < 60) {
if (this.config.archiveSeconds < 60 || this.config.archiveFailedSeconds < 60) {
return
}

Expand Down
Loading

0 comments on commit 32f5ba2

Please sign in to comment.