bullmq 6.6.0 → 6.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/classes/child.js +8 -1
- package/dist/cjs/classes/main-base.js +2 -0
- package/dist/cjs/classes/queue-getters.js +12 -2
- package/dist/cjs/classes/redis-queue-backend.js +6 -4
- package/dist/cjs/commands/getCounts-1.lua +9 -2
- package/dist/cjs/postgres/postgres-queue-backend.js +13 -1
- package/dist/cjs/postgres/sql-scripts.js +2 -1
- package/dist/cjs/scripts/getCounts-1.js +9 -2
- package/dist/cjs/tsconfig-cjs.tsbuildinfo +1 -1
- package/dist/cjs/version.js +1 -1
- package/dist/esm/classes/child.js +8 -1
- package/dist/esm/classes/main-base.js +2 -0
- package/dist/esm/classes/queue-getters.d.ts +4 -1
- package/dist/esm/classes/queue-getters.js +12 -2
- package/dist/esm/classes/redis-queue-backend.d.ts +1 -1
- package/dist/esm/classes/redis-queue-backend.js +6 -4
- package/dist/esm/commands/getCounts-1.lua +9 -2
- package/dist/esm/interfaces/queue-backend.d.ts +5 -1
- package/dist/esm/postgres/postgres-queue-backend.d.ts +1 -1
- package/dist/esm/postgres/postgres-queue-backend.js +13 -1
- package/dist/esm/postgres/sql-scripts.js +2 -1
- package/dist/esm/scripts/getCounts-1.js +9 -2
- package/dist/esm/tsconfig.tsbuildinfo +1 -1
- package/dist/esm/version.d.ts +1 -1
- package/dist/esm/version.js +1 -1
- package/package.json +2 -2
|
@@ -127,7 +127,14 @@ class Child extends node_events_1.EventEmitter {
|
|
|
127
127
|
this.childProcess.kill(signal);
|
|
128
128
|
}
|
|
129
129
|
else if (this.worker) {
|
|
130
|
-
|
|
130
|
+
if (signal === 'SIGTERM') {
|
|
131
|
+
// A worker thread can't receive signals, so ask it to finish its
|
|
132
|
+
// current job and exit, like a child process does on SIGTERM.
|
|
133
|
+
this.worker.postMessage({ cmd: enums_1.ChildCommand.Stop });
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
this.worker.terminate();
|
|
137
|
+
}
|
|
131
138
|
}
|
|
132
139
|
}
|
|
133
140
|
async kill(signal = 'SIGKILL', timeoutMs) {
|
|
@@ -19,6 +19,8 @@ exports.default = (send, receiver) => {
|
|
|
19
19
|
await childProcessor.start(msg.job, msg === null || msg === void 0 ? void 0 : msg.token);
|
|
20
20
|
break;
|
|
21
21
|
case enums_1.ChildCommand.Stop:
|
|
22
|
+
// Sent to worker threads instead of SIGTERM.
|
|
23
|
+
await childProcessor.waitForCurrentJobAndExit();
|
|
22
24
|
break;
|
|
23
25
|
case enums_1.ChildCommand.Cancel:
|
|
24
26
|
childProcessor.cancel(msg.value);
|
|
@@ -220,9 +220,19 @@ class QueueGetters extends queue_base_1.QueueBase {
|
|
|
220
220
|
}
|
|
221
221
|
/**
|
|
222
222
|
* Returns the number of jobs in delayed status.
|
|
223
|
+
*
|
|
224
|
+
* @param timestamp - Optional Unix timestamp as a safe integer in milliseconds.
|
|
225
|
+
* When provided, only counts jobs scheduled strictly before this timestamp.
|
|
223
226
|
*/
|
|
224
|
-
getDelayedCount() {
|
|
225
|
-
|
|
227
|
+
async getDelayedCount(timestamp) {
|
|
228
|
+
if (timestamp === undefined) {
|
|
229
|
+
return this.getJobCountByTypes('delayed');
|
|
230
|
+
}
|
|
231
|
+
if (!Number.isSafeInteger(timestamp)) {
|
|
232
|
+
throw new Error('timestamp must be a safe integer in milliseconds');
|
|
233
|
+
}
|
|
234
|
+
const [count] = await this.backend.getCounts(['delayed'], timestamp);
|
|
235
|
+
return count;
|
|
226
236
|
}
|
|
227
237
|
/**
|
|
228
238
|
* Returns the number of jobs in active status.
|
|
@@ -934,18 +934,20 @@ class RedisQueueBackend extends node_events_1.EventEmitter {
|
|
|
934
934
|
const args = this.getJobsArgs(types, start, end, asc);
|
|
935
935
|
return await this.execCommand(client, 'getJobs', args);
|
|
936
936
|
}
|
|
937
|
-
getCountsArgs(types) {
|
|
937
|
+
getCountsArgs(types, delayedTimestamp) {
|
|
938
938
|
const queueKeys = this.queue.keys;
|
|
939
939
|
const transformedTypes = types.map(type => {
|
|
940
940
|
return type === 'waiting' ? 'wait' : type;
|
|
941
941
|
});
|
|
942
942
|
const keys = [queueKeys['']];
|
|
943
|
-
const args =
|
|
943
|
+
const args = delayedTimestamp === undefined
|
|
944
|
+
? transformedTypes
|
|
945
|
+
: [delayedTimestamp, ...transformedTypes];
|
|
944
946
|
return keys.concat(args);
|
|
945
947
|
}
|
|
946
|
-
async getCounts(types) {
|
|
948
|
+
async getCounts(types, delayedTimestamp) {
|
|
947
949
|
const client = await this.queue.client;
|
|
948
|
-
const args = this.getCountsArgs(types);
|
|
950
|
+
const args = this.getCountsArgs(types, delayedTimestamp);
|
|
949
951
|
return await this.execCommand(client, 'getCounts', args);
|
|
950
952
|
}
|
|
951
953
|
getCountsPerPriorityArgs(priorities) {
|
|
@@ -4,13 +4,16 @@
|
|
|
4
4
|
Input:
|
|
5
5
|
KEYS[1] 'prefix'
|
|
6
6
|
|
|
7
|
-
ARGV[1
|
|
7
|
+
ARGV[1] optional delayed timestamp cutoff (milliseconds)
|
|
8
|
+
ARGV[...] types (starts at 1 when no cutoff is provided)
|
|
8
9
|
]]
|
|
9
10
|
local rcall = redis.call;
|
|
10
11
|
local prefix = KEYS[1]
|
|
11
12
|
local results = {}
|
|
13
|
+
local delayedTimestamp = tonumber(ARGV[1])
|
|
14
|
+
local firstType = delayedTimestamp and 2 or 1
|
|
12
15
|
|
|
13
|
-
for i =
|
|
16
|
+
for i = firstType, #ARGV do
|
|
14
17
|
local stateKey = prefix .. ARGV[i]
|
|
15
18
|
if ARGV[i] == "wait" or ARGV[i] == "paused" then
|
|
16
19
|
-- Markers in waitlist DEPRECATED in v5: Remove in v6.
|
|
@@ -28,6 +31,10 @@ for i = 1, #ARGV do
|
|
|
28
31
|
end
|
|
29
32
|
elseif ARGV[i] == "active" then
|
|
30
33
|
results[#results+1] = rcall("LLEN", stateKey)
|
|
34
|
+
elseif ARGV[i] == "delayed" and delayedTimestamp then
|
|
35
|
+
-- The low 12 score bits order jobs within a millisecond. Subtracting one
|
|
36
|
+
-- excludes every job scheduled at the cutoff timestamp or later.
|
|
37
|
+
results[#results+1] = rcall("ZCOUNT", stateKey, "-inf", delayedTimestamp * 0x1000 - 1)
|
|
31
38
|
else
|
|
32
39
|
results[#results+1] = rcall("ZCARD", stateKey)
|
|
33
40
|
end
|
|
@@ -1235,7 +1235,19 @@ class PostgresQueueBackend extends node_events_1.EventEmitter {
|
|
|
1235
1235
|
]);
|
|
1236
1236
|
return Number(rows[0].ttl);
|
|
1237
1237
|
}
|
|
1238
|
-
async getCounts(types) {
|
|
1238
|
+
async getCounts(types, delayedTimestamp) {
|
|
1239
|
+
if (delayedTimestamp !== undefined && types.includes('delayed')) {
|
|
1240
|
+
const otherTypes = types.filter(type => type !== 'delayed');
|
|
1241
|
+
const [{ rows }, otherCounts] = await Promise.all([
|
|
1242
|
+
this.run('get_delayed_count', [
|
|
1243
|
+
this.queueName,
|
|
1244
|
+
delayedTimestamp,
|
|
1245
|
+
]),
|
|
1246
|
+
otherTypes.length ? this.getCounts(otherTypes) : Promise.resolve([]),
|
|
1247
|
+
]);
|
|
1248
|
+
let index = 0;
|
|
1249
|
+
return types.map(type => type === 'delayed' ? Number(rows[0].count) : otherCounts[index++]);
|
|
1250
|
+
}
|
|
1239
1251
|
const { rows } = await this.run('get_counts', [
|
|
1240
1252
|
this.queueName,
|
|
1241
1253
|
]);
|
|
@@ -34,6 +34,7 @@ exports.SQL_COMMANDS = {
|
|
|
34
34
|
"get_counts.sql": "-- Job counts by state for a queue. Param: $1 queue.\n-- \"prioritized\" = waiting with priority > 0; \"waiting\" = waiting with priority 0.\nSELECT\n COUNT(*) FILTER (WHERE state = 'active') AS active,\n COUNT(*) FILTER (WHERE state = 'completed') AS completed,\n COUNT(*) FILTER (WHERE state = 'failed') AS failed,\n COUNT(*) FILTER (WHERE state = 'delayed') AS delayed,\n COUNT(*) FILTER (WHERE state = 'waiting' AND priority = 0) AS waiting,\n COUNT(*) FILTER (WHERE state = 'waiting' AND priority > 0) AS prioritized,\n COUNT(*) FILTER (WHERE state = 'waiting-children') AS \"waiting-children\",\n (SELECT value FROM meta WHERE queue = $1 AND field = 'paused') AS paused\nFROM job\nWHERE queue = $1;\n",
|
|
35
35
|
"get_counts_per_priority.sql": "-- Waiting-job counts per priority. Params: $1 queue, $2 priorities (bigint[]).\n-- Returns one row per requested priority, in the input array order, with the\n-- number of waiting jobs at that priority. Pausing is an O(1) meta flag that\n-- leaves jobs in the 'waiting' state, so the counts are identical whether or\n-- not the queue is paused (mirrors Redis, where pausing does not touch the\n-- wait/prioritized sets).\nSELECT COUNT(j.id) AS cnt\nFROM unnest($2::bigint[]) WITH ORDINALITY AS pr(priority, ord)\nLEFT JOIN job j\n ON j.queue = $1\n AND j.state = 'waiting'\n AND j.priority = pr.priority\nGROUP BY pr.ord\nORDER BY pr.ord;\n",
|
|
36
36
|
"get_deduplication_job_id.sql": "-- The current \"winner\" job id for a deduplication key, or none when absent or\n-- expired. Params: $1 queue, $2 dedup_id, $3 now (ms).\nSELECT job_id\nFROM dedup\nWHERE queue = $1\n AND dedup_id = $2\n AND (expire_at_ms IS NULL OR expire_at_ms > $3);\n",
|
|
37
|
+
"get_delayed_count.sql": "-- Delayed jobs scheduled strictly before $2 (Unix milliseconds). $1 is queue.\nSELECT COUNT(*) AS count\nFROM job\nWHERE queue = $1 AND state = 'delayed' AND process_at_ms < $2::bigint;\n",
|
|
37
38
|
"get_dependencies.sql": "-- All of a parent job's child dependencies with their status and stored value.\n-- Params: $1 parent_queue, $2 parent_id. Status maps to the public categories:\n-- processed → processed (value = return value), pending → unprocessed,\n-- ignored → ignored (value = reason), failed → failed.\nSELECT status::text AS status, child_key, value\nFROM job_dependency\nWHERE parent_queue = $1 AND parent_id = $2\nORDER BY child_id;\n",
|
|
38
39
|
"get_dependencies_page.sql": "-- One page of a parent's child dependencies in a single status category.\n-- Params: $1 parent_queue, $2 parent_id, $3 status, $4 offset, $5 count.\nSELECT child_key, value\nFROM job_dependency\nWHERE parent_queue = $1 AND parent_id = $2 AND status = $3::dep_status\nORDER BY child_id\nOFFSET $4 LIMIT $5;\n",
|
|
39
40
|
"get_dependency_counts.sql": "-- Child-dependency counts per status for a parent job.\n-- Params: $1 parent_queue, $2 parent_id. (\"unprocessed\" = pending children.)\nSELECT\n COUNT(*) FILTER (WHERE status = 'processed') AS processed,\n COUNT(*) FILTER (WHERE status = 'pending') AS unprocessed,\n COUNT(*) FILTER (WHERE status = 'ignored') AS ignored,\n COUNT(*) FILTER (WHERE status = 'failed') AS failed\nFROM job_dependency\nWHERE parent_queue = $1 AND parent_id = $2;\n",
|
|
@@ -45,7 +46,7 @@ exports.SQL_COMMANDS = {
|
|
|
45
46
|
"get_job_scheduler.sql": "-- Fetch a single scheduler's stored metadata and next-run score.\n-- Params: $1 queue, $2 scheduler_id.\nSELECT name, iteration_count, limit_count, start_date_ms, end_date_ms, tz,\n pattern, every_ms, offset_ms, template_data, template_opts, next_run_ms\n FROM scheduler\n WHERE queue = $1 AND scheduler_id = $2;\n",
|
|
46
47
|
"get_job_schedulers_count.sql": "-- Number of registered schedulers in a queue. Param: $1 queue.\nSELECT count(*)::int AS count FROM scheduler WHERE queue = $1;\n",
|
|
47
48
|
"get_job_schedulers_range.sql": "-- Fetch a page of schedulers ordered by next-run time. Params: $1 queue,\n-- $2 asc (boolean), $3 offset, $4 count (NULL = all remaining).\nSELECT scheduler_id, next_run_ms\n FROM scheduler\n WHERE queue = $1\n ORDER BY CASE WHEN $2 THEN next_run_ms END ASC,\n CASE WHEN NOT $2 THEN next_run_ms END DESC,\n scheduler_id\n OFFSET $3\n LIMIT $4;\n",
|
|
48
|
-
"get_metrics.sql": "-- Metrics for a queue/kind: the
|
|
49
|
+
"get_metrics.sql": "-- Metrics for a queue/kind: the meta (`count`, `prev_ts`, `prev_count`) and the\n-- per-minute data points (newest first), sliced to [start, end] with Redis\n-- LRANGE semantics: index 0 is the newest point and a negative index counts\n-- from the oldest (-1 is the oldest point, -2 the one before it).\n-- `num_points` is the length of the whole series, not of the slice, like LLEN\n-- in getMetrics-2.lua.\n-- Params: $1 queue, $2 kind ('completed' | 'failed'), $3 start, $4 end.\nWITH series AS (\n SELECT count, prev_ts, prev_count, data, cardinality(data) AS num_points\n FROM metrics\n WHERE queue = $1 AND kind = $2\n), bounds AS (\n -- Arrays are 1-based, hence the + 1. A start before the first point is\n -- clamped to it; an empty or inverted range yields an empty array.\n SELECT (CASE WHEN $3 < 0 THEN GREATEST(num_points + $3, 0) ELSE $3 END) + 1 AS first_index,\n (CASE WHEN $4 < 0 THEN num_points + $4 ELSE $4 END) + 1 AS last_index\n FROM series\n)\nSELECT\n COALESCE((SELECT count FROM series), 0)::bigint AS total,\n COALESCE((SELECT prev_ts FROM series), 0)::bigint AS prev_ts,\n COALESCE((SELECT prev_count FROM series), 0)::bigint AS prev_count,\n COALESCE((SELECT num_points FROM series), 0)::bigint AS num_points,\n COALESCE((\n SELECT data[first_index : last_index] FROM series, bounds\n ), ARRAY[]::bigint[]) AS data;\n",
|
|
49
50
|
"get_processed_children_values.sql": "-- Processed children of a parent as (child_key → serialized value) pairs. The\n-- value is returned as JSON text so the caller can JSON.parse it (mirrors the\n-- Redis `<jobId>:processed` hash of stringified values). Params: $1 parent_queue,\n-- $2 parent_id.\nSELECT child_key, value::text AS value\nFROM job_dependency\nWHERE parent_queue = $1 AND parent_id = $2 AND status = 'processed'\nORDER BY child_id;\n",
|
|
50
51
|
"get_queue_meta.sql": "-- The full queue metadata hash. Param: $1 queue.\nSELECT field, value FROM meta WHERE queue = $1;\n",
|
|
51
52
|
"get_queue_meta_field.sql": "-- A single queue metadata field's value. Params: $1 queue, $2 field.\nSELECT value FROM meta WHERE queue = $1 AND field = $2;\n",
|
|
@@ -5,12 +5,15 @@ const content = `--[[
|
|
|
5
5
|
Get counts per provided states
|
|
6
6
|
Input:
|
|
7
7
|
KEYS[1] 'prefix'
|
|
8
|
-
ARGV[1
|
|
8
|
+
ARGV[1] optional delayed timestamp cutoff (milliseconds)
|
|
9
|
+
ARGV[...] types (starts at 1 when no cutoff is provided)
|
|
9
10
|
]]
|
|
10
11
|
local rcall = redis.call;
|
|
11
12
|
local prefix = KEYS[1]
|
|
12
13
|
local results = {}
|
|
13
|
-
|
|
14
|
+
local delayedTimestamp = tonumber(ARGV[1])
|
|
15
|
+
local firstType = delayedTimestamp and 2 or 1
|
|
16
|
+
for i = firstType, #ARGV do
|
|
14
17
|
local stateKey = prefix .. ARGV[i]
|
|
15
18
|
if ARGV[i] == "wait" or ARGV[i] == "paused" then
|
|
16
19
|
-- Markers in waitlist DEPRECATED in v5: Remove in v6.
|
|
@@ -28,6 +31,10 @@ for i = 1, #ARGV do
|
|
|
28
31
|
end
|
|
29
32
|
elseif ARGV[i] == "active" then
|
|
30
33
|
results[#results+1] = rcall("LLEN", stateKey)
|
|
34
|
+
elseif ARGV[i] == "delayed" and delayedTimestamp then
|
|
35
|
+
-- The low 12 score bits order jobs within a millisecond. Subtracting one
|
|
36
|
+
-- excludes every job scheduled at the cutoff timestamp or later.
|
|
37
|
+
results[#results+1] = rcall("ZCOUNT", stateKey, "-inf", delayedTimestamp * 0x1000 - 1)
|
|
31
38
|
else
|
|
32
39
|
results[#results+1] = rcall("ZCARD", stateKey)
|
|
33
40
|
end
|