Comparing version 1.2.0-beta2 to 2.0.0-beta1
@@ -343,3 +343,3 @@ 'use strict'; | ||
assert(result.rowCount === 1, 'Job ' + id + ' could not be completed.'); | ||
return id; | ||
return result.rows[0]; | ||
}); | ||
@@ -352,3 +352,3 @@ } | ||
assert(result.rowCount === 1, 'Job ' + id + ' could not be cancelled.'); | ||
return id; | ||
return result.rows[0]; | ||
}); | ||
@@ -361,3 +361,3 @@ } | ||
assert(result.rowCount === 1, 'Job ' + id + ' could not be marked as failed.'); | ||
return id; | ||
return result.rows[0]; | ||
}); | ||
@@ -364,0 +364,0 @@ } |
@@ -5,2 +5,12 @@ 'use strict'; | ||
var states = { | ||
created: 'created', | ||
retry: 'retry', | ||
active: 'active', | ||
complete: 'complete', | ||
expired: 'expired', | ||
cancelled: 'cancelled', | ||
failed: 'failed' | ||
}; | ||
module.exports = { | ||
@@ -19,2 +29,3 @@ create: create, | ||
countStates: countStates, | ||
states: states, | ||
expireJobSuffix: expireJobSuffix | ||
@@ -38,7 +49,7 @@ }; | ||
// base type is numeric and first values are less than last values | ||
return '\n CREATE TYPE ' + schema + '.job_state AS ENUM (\n \'created\',\n \'retry\',\n \'active\',\t\n \'complete\',\n \'expired\',\n \'cancelled\',\n \'failed\'\n )\n '; | ||
return '\n CREATE TYPE ' + schema + '.job_state AS ENUM (\n \'' + states.created + '\',\n \'' + states.retry + '\',\n \'' + states.active + '\',\t\n \'' + states.complete + '\',\n \'' + states.expired + '\',\n \'' + states.cancelled + '\',\n \'' + states.failed + '\'\n )\n '; | ||
} | ||
function createJobTable(schema) { | ||
return '\n CREATE TABLE IF NOT EXISTS ' + schema + '.job (\n id uuid primary key not null,\n name text not null,\n priority integer not null default(0),\n data jsonb,\n state ' + schema + '.job_state not null default(\'created\'),\n retryLimit integer not null default(0),\n retryCount integer not null default(0),\n startIn interval not null default(interval \'0\'),\n startedOn timestamp with time zone,\n singletonKey text,\n singletonOn timestamp without time zone,\n expireIn interval,\n createdOn timestamp with time zone not null default now(),\n completedOn timestamp with time zone\n )\n '; | ||
return '\n CREATE TABLE IF NOT EXISTS ' + schema + '.job (\n id uuid primary key not null,\n name text not null,\n priority integer not null default(0),\n data jsonb,\n state ' + schema + '.job_state not null default(\'' + states.created + '\'),\n retryLimit integer not null default(0),\n retryCount integer not null default(0),\n startIn interval not null default(interval \'0\'),\n startedOn timestamp with time zone,\n singletonKey text,\n singletonOn timestamp without time zone,\n expireIn interval,\n createdOn timestamp with time zone not null default now(),\n completedOn timestamp with time zone\n )\n '; | ||
} | ||
@@ -48,3 +59,3 @@ | ||
// anything with singletonKey means "only 1 job can be queued or active at a time" | ||
return '\n CREATE UNIQUE INDEX job_singletonKey ON ' + schema + '.job (name, singletonKey) WHERE state < \'complete\' AND singletonOn IS NULL\n '; | ||
return '\n CREATE UNIQUE INDEX job_singletonKey ON ' + schema + '.job (name, singletonKey) WHERE state < \'' + states.complete + '\' AND singletonOn IS NULL\n '; | ||
} | ||
@@ -54,3 +65,3 @@ | ||
// anything with singletonOn means "only 1 job within this time period, queued, active or completed" | ||
return '\n CREATE UNIQUE INDEX job_singletonOn ON ' + schema + '.job (name, singletonOn) WHERE state < \'expired\' AND singletonKey IS NULL\n '; | ||
return '\n CREATE UNIQUE INDEX job_singletonOn ON ' + schema + '.job (name, singletonOn) WHERE state < \'' + states.expired + '\' AND singletonKey IS NULL\n '; | ||
} | ||
@@ -60,3 +71,3 @@ | ||
// anything with both singletonOn and singletonKey means "only 1 job within this time period with this key, queued, active or completed" | ||
return '\n CREATE UNIQUE INDEX job_singletonKeyOn ON ' + schema + '.job (name, singletonOn, singletonKey) WHERE state < \'expired\'\n '; | ||
return '\n CREATE UNIQUE INDEX job_singletonKeyOn ON ' + schema + '.job (name, singletonOn, singletonKey) WHERE state < \'' + states.expired + '\'\n '; | ||
} | ||
@@ -77,31 +88,31 @@ | ||
function fetchNextJob(schema) { | ||
return '\n WITH nextJob as (\n SELECT id\n FROM ' + schema + '.job\n WHERE state < \'active\'\n AND name = $1\n AND (createdOn + startIn) < now()\n ORDER BY priority desc, createdOn, id\n LIMIT 1\n FOR UPDATE SKIP LOCKED\n )\n UPDATE ' + schema + '.job SET\n state = \'active\',\n startedOn = now(),\n retryCount = CASE WHEN state = \'retry\' THEN retryCount + 1 ELSE retryCount END\n FROM nextJob\n WHERE ' + schema + '.job.id = nextJob.id\n RETURNING ' + schema + '.job.id, ' + schema + '.job.data\n '; | ||
return '\n WITH nextJob as (\n SELECT id\n FROM ' + schema + '.job\n WHERE state < \'' + states.active + '\'\n AND name = $1\n AND (createdOn + startIn) < now()\n ORDER BY priority desc, createdOn, id\n LIMIT 1\n FOR UPDATE SKIP LOCKED\n )\n UPDATE ' + schema + '.job SET\n state = \'' + states.active + '\',\n startedOn = now(),\n retryCount = CASE WHEN state = \'' + states.retry + '\' THEN retryCount + 1 ELSE retryCount END\n FROM nextJob\n WHERE ' + schema + '.job.id = nextJob.id\n RETURNING ' + schema + '.job.id, ' + schema + '.job.data\n '; | ||
} | ||
function completeJob(schema) { | ||
return '\n UPDATE ' + schema + '.job\n SET completedOn = now(),\n state = \'complete\'\n WHERE id = $1\n AND state = \'active\'\n '; | ||
return '\n UPDATE ' + schema + '.job\n SET completedOn = now(),\n state = \'' + states.complete + '\'\n WHERE id = $1\n AND state = \'' + states.active + '\'\n RETURNING id, name, data\n '; | ||
} | ||
function cancelJob(schema) { | ||
return '\n UPDATE ' + schema + '.job\n SET completedOn = now(),\n state = \'cancelled\'\n WHERE id = $1\n AND state < \'complete\'\n '; | ||
return '\n UPDATE ' + schema + '.job\n SET completedOn = now(),\n state = \'' + states.cancelled + '\'\n WHERE id = $1\n AND state < \'' + states.complete + '\'\n RETURNING id, name, data\n '; | ||
} | ||
function failJob(schema) { | ||
return '\n UPDATE ' + schema + '.job\n SET completedOn = now(),\n state = \'failed\'\n WHERE id = $1\n AND state < \'complete\'\n '; | ||
return '\n UPDATE ' + schema + '.job\n SET completedOn = now(),\n state = \'' + states.failed + '\'\n WHERE id = $1\n AND state < \'' + states.complete + '\'\n RETURNING id, name, data\n '; | ||
} | ||
function insertJob(schema) { | ||
return '\n INSERT INTO ' + schema + '.job (id, name, priority, state, retryLimit, startIn, expireIn, data, singletonKey, singletonOn)\n VALUES (\n $1, $2, $3, \'created\', $4, CAST($5 as interval), CAST($6 as interval), $7, $8,\n CASE WHEN $9::integer IS NOT NULL THEN \'epoch\'::timestamp + \'1 second\'::interval * ($9 * floor((date_part(\'epoch\', now()) + $10) / $9)) ELSE NULL END\n )\n ON CONFLICT DO NOTHING\n '; | ||
return '\n INSERT INTO ' + schema + '.job (id, name, priority, state, retryLimit, startIn, expireIn, data, singletonKey, singletonOn)\n VALUES (\n $1, $2, $3, \'' + states.created + '\', $4, CAST($5 as interval), CAST($6 as interval), $7, $8,\n CASE WHEN $9::integer IS NOT NULL THEN \'epoch\'::timestamp + \'1 second\'::interval * ($9 * floor((date_part(\'epoch\', now()) + $10) / $9)) ELSE NULL END\n )\n ON CONFLICT DO NOTHING\n '; | ||
} | ||
function expire(schema) { | ||
return '\n WITH expired AS (\n UPDATE ' + schema + '.job\n SET state = CASE WHEN retryCount < retryLimit THEN \'retry\'::' + schema + '.job_state ELSE \'expired\'::' + schema + '.job_state END, \n completedOn = CASE WHEN retryCount < retryLimit THEN NULL ELSE now() END\n WHERE state = \'active\'\n AND (startedOn + expireIn) < now() \n RETURNING id, name, state, data\n )\n SELECT id, name, data FROM expired WHERE state = \'expired\';\n '; | ||
return '\n WITH expired AS (\n UPDATE ' + schema + '.job\n SET state = CASE WHEN retryCount < retryLimit THEN \'' + states.retry + '\'::' + schema + '.job_state ELSE \'' + states.expired + '\'::' + schema + '.job_state END, \n completedOn = CASE WHEN retryCount < retryLimit THEN NULL ELSE now() END\n WHERE state = \'' + states.active + '\'\n AND (startedOn + expireIn) < now() \n RETURNING id, name, state, data\n )\n SELECT id, name, data FROM expired WHERE state = \'' + states.expired + '\';\n '; | ||
} | ||
function archive(schema) { | ||
return '\n DELETE FROM ' + schema + '.job\n WHERE (completedOn + CAST($1 as interval) < now())\n OR (state = \'created\' and name like \'%' + expireJobSuffix + '\' and createdOn + CAST($1 as interval) < now()) \n '; | ||
return '\n DELETE FROM ' + schema + '.job\n WHERE (completedOn + CAST($1 as interval) < now())\n OR (state = \'' + states.created + '\' and name like \'%' + expireJobSuffix + '\' and createdOn + CAST($1 as interval) < now()) \n '; | ||
} | ||
function countStates(schema) { | ||
return '\n SELECT\n COUNT(*) FILTER (where state = \'created\') as created,\n COUNT(*) FILTER (where state = \'retry\') as retry,\n COUNT(*) FILTER (where state = \'active\') as active,\n COUNT(*) FILTER (where state = \'complete\') as complete,\n COUNT(*) FILTER (where state = \'expired\') as expired,\n COUNT(*) FILTER (where state = \'cancelled\') as cancelled,\n COUNT(*) FILTER (where state = \'failed\') as failed\n FROM ' + schema + '.job\n '; | ||
return '\n SELECT\n COUNT(*) FILTER (where state = \'' + states.created + '\') as created,\n COUNT(*) FILTER (where state = \'' + states.retry + '\') as retry,\n COUNT(*) FILTER (where state = \'' + states.active + '\') as active,\n COUNT(*) FILTER (where state = \'' + states.complete + '\') as complete,\n COUNT(*) FILTER (where state = \'' + states.expired + '\') as expired,\n COUNT(*) FILTER (where state = \'' + states.cancelled + '\') as cancelled,\n COUNT(*) FILTER (where state = \'' + states.failed + '\') as failed\n FROM ' + schema + '.job\n '; | ||
} |
{ | ||
"name": "pg-boss", | ||
"version": "1.2.0-beta2", | ||
"version": "2.0.0-beta1", | ||
"description": "Queueing jobs in Node.js using PostgreSQL like a boss", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
133457
933