@zintrust/queue-monitor 2.4.4 → 2.4.6

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.
@@ -1,14 +1,14 @@
1
1
  {
2
2
  "name": "@zintrust/queue-monitor",
3
- "version": "2.4.3",
4
- "buildDate": "2026-06-01T13:35:49.849Z",
3
+ "version": "2.4.5",
4
+ "buildDate": "2026-06-03T09:57:58.476Z",
5
5
  "buildEnvironment": {
6
6
  "node": "v22.22.1",
7
7
  "platform": "darwin",
8
8
  "arch": "arm64"
9
9
  },
10
10
  "git": {
11
- "commit": "98a0caf4",
11
+ "commit": "feec4c80",
12
12
  "branch": "release"
13
13
  },
14
14
  "package": {
@@ -42,8 +42,8 @@
42
42
  "sha256": "3b7e85e6a0ba5dd958010bdf023052b1fb66cfbb52047554b326b4465b817719"
43
43
  },
44
44
  "build-manifest.json": {
45
- "size": 3869,
46
- "sha256": "c1ba9522a69a44edce77119e8fd9df36dff9e8797d8a040448a8659da8b0721c"
45
+ "size": 3871,
46
+ "sha256": "2b7d6b37d81ec677fb07cbd8e69566cc5fcc953b5d40d3187b1ad75c85ab57e8"
47
47
  },
48
48
  "connection.d.ts": {
49
49
  "size": 287,
@@ -78,12 +78,12 @@
78
78
  "sha256": "fb91df973acd13a88cca443e50ecdd34944c66e1a1d48333f63099079a00f3c9"
79
79
  },
80
80
  "driver.d.ts": {
81
- "size": 1782,
82
- "sha256": "a1629f48367efab3c0031ace21c5304821d01ca1d7027cc7a50b62570339c848"
81
+ "size": 1417,
82
+ "sha256": "9aa9e6ac31d8a0a3d4145a8228bd61180be89e7170b02d1290fcb732a4c217a1"
83
83
  },
84
84
  "driver.js": {
85
- "size": 12473,
86
- "sha256": "162ee0db3fe8a0cbc6378133a22d6df723a900e08ea74817c562b830a88338db"
85
+ "size": 12494,
86
+ "sha256": "038d017128abb2c1319039bfa6d1fccc72c1eaea472d2296e1efc153dd17cfd6"
87
87
  },
88
88
  "index.d.ts": {
89
89
  "size": 2344,
@@ -91,7 +91,7 @@
91
91
  },
92
92
  "index.js": {
93
93
  "size": 17226,
94
- "sha256": "79f16f77f5e56c815212ffc43718fee07cf490c4f7fd9ff0e68fa656337b8a76"
94
+ "sha256": "66b89ca171cc49187182d84d3fb9a9ae112785bd1a3cc8ea2ee103a508fcafd3"
95
95
  },
96
96
  "metrics-index.d.ts": {
97
97
  "size": 239,
@@ -118,12 +118,12 @@
118
118
  "sha256": "7aa5c97aef19ec36e1309b9abc922481924672e4017bc3fe198b5eff7dabda86"
119
119
  },
120
120
  "worker.d.ts": {
121
- "size": 338,
122
- "sha256": "6bb39529c68675846d1877b34ccb2fa2bbe9bc56a223388add24e239f24ddfc3"
121
+ "size": 347,
122
+ "sha256": "fb7751246ae88f5f470107e35f25f561da5919fd2619fe70b8b182d0c878ac0e"
123
123
  },
124
124
  "worker.js": {
125
- "size": 1239,
126
- "sha256": "46f116eb169fca1dc6dfd48d299001635c48a2da8d6e10115a98fcd33187b53e"
125
+ "size": 1376,
126
+ "sha256": "4f114198579d049482e284738cbdf35576d48bb269cc0936053911548efacb5d"
127
127
  }
128
128
  }
129
129
  }
@@ -637,6 +637,14 @@ const getRenderJobsRowHelpersFunction = () => `
637
637
  return tr;
638
638
  }
639
639
 
640
+ function formatDateTime(value) {
641
+ const date = value instanceof Date ? value : new Date(value);
642
+ if (Number.isNaN(date.getTime())) {
643
+ return '—';
644
+ }
645
+ return date.toLocaleString();
646
+ }
647
+
640
648
  function updateExistingJobRow(tr, job, idx) {
641
649
  const jobId = getJobId(job);
642
650
  const statusInfo = getJobStatusInfo(job);
@@ -651,7 +659,7 @@ const getRenderJobsRowHelpersFunction = () => `
651
659
  '<td>' + (job.queue || currentQueue) + '</td>' +
652
660
  '<td><span class="status-badge ' + statusInfo.cls + '">' + statusInfo.label + '</span></td>' +
653
661
  '<td>' + job.attempts + '</td>' +
654
- '<td>' + new Date(job.timestamp).toLocaleTimeString() + '</td>' +
662
+ '<td>' + formatDateTime(job.timestamp) + '</td>' +
655
663
  '<td>' + getJobRetryMarkup(job) + '</td>';
656
664
 
657
665
  if (job.failedReason) {
@@ -835,7 +843,7 @@ const getLockHelperFunctions = () => `
835
843
 
836
844
  function updateExistingLockRow(row, lock, idx) {
837
845
  const ttl = typeof lock.ttl === 'number' ? Math.round(lock.ttl / 1000) + 's' : '—';
838
- const expires = lock.expires ? new Date(lock.expires).toLocaleTimeString() : '—';
846
+ const expires = lock.expires ? formatDateTime(lock.expires) : '—';
839
847
 
840
848
  const ttlCell = row.children[1];
841
849
  const expiresCell = row.children[2];
@@ -851,7 +859,7 @@ const getLockHelperFunctions = () => `
851
859
  tr.dataset.lockIndex = idx;
852
860
 
853
861
  const ttl = typeof lock.ttl === 'number' ? Math.round(lock.ttl / 1000) + 's' : '—';
854
- const expires = lock.expires ? new Date(lock.expires).toLocaleTimeString() : '—';
862
+ const expires = lock.expires ? formatDateTime(lock.expires) : '—';
855
863
  const isExpanded = expandedLockKeys.has(lock.key);
856
864
 
857
865
  tr.innerHTML =
@@ -1133,7 +1141,7 @@ const getDashboardScriptEventStream = () => `
1133
1141
 
1134
1142
  const lastUpdated = document.getElementById('last-updated');
1135
1143
  if (lastUpdated) {
1136
- lastUpdated.textContent = new Date().toLocaleTimeString();
1144
+ lastUpdated.textContent = formatDateTime(new Date());
1137
1145
  }
1138
1146
  markStreamHealthy();
1139
1147
  }
@@ -1168,7 +1176,7 @@ const getFetchDataFunction = () => `
1168
1176
  updateQueueSelect(data.queues);
1169
1177
  handleQueueSelection(data);
1170
1178
  await fetchLocks();
1171
- document.getElementById('last-updated').textContent = new Date().toLocaleTimeString();
1179
+ document.getElementById('last-updated').textContent = formatDateTime(new Date());
1172
1180
  } catch (e) {
1173
1181
  showError(e.message);
1174
1182
  }
package/dist/driver.d.ts CHANGED
@@ -2,16 +2,7 @@ import type { Job, JobsOptions } from 'bullmq';
2
2
  import { type RedisConfig } from './connection.js';
3
3
  export type JobPayload<T = unknown> = T;
4
4
  export type JobCounts = Record<string, number>;
5
- /**
6
- * Standard BullMQ job-count buckets, all zero. Used when the backing store is
7
- * unavailable so a queue is still listed (with empty counts) instead of being
8
- * dropped from the snapshot entirely.
9
- */
10
5
  export declare const emptyCounts: () => JobCounts;
11
- /**
12
- * Map a list of known queue names to entries with empty counts. Keeps the
13
- * dashboard showing the configured queues during transient backend outages.
14
- */
15
6
  export declare const emptyQueueStats: (queueNames: ReadonlyArray<string>) => Array<{
16
7
  name: string;
17
8
  counts: JobCounts;
package/dist/driver.js CHANGED
@@ -2,13 +2,16 @@ import { Env } from '@zintrust/core/config';
2
2
  import { ErrorFactory } from '@zintrust/core/errors';
3
3
  import { Logger } from '@zintrust/core/logger';
4
4
  import { getBullMQSafeQueueName } from '@zintrust/core/redis';
5
- import { Queue } from 'bullmq';
6
5
  import { createRedisConnection } from './connection.js';
7
- /**
8
- * Standard BullMQ job-count buckets, all zero. Used when the backing store is
9
- * unavailable so a queue is still listed (with empty counts) instead of being
10
- * dropped from the snapshot entirely.
11
- */
6
+ let QueueCtor;
7
+ const ensureBullmqLoaded = async () => {
8
+ if (QueueCtor !== undefined)
9
+ return QueueCtor;
10
+ const bullmqPkg = 'bullmq';
11
+ const loaded = (await import(bullmqPkg)).Queue;
12
+ QueueCtor = loaded;
13
+ return loaded;
14
+ };
12
15
  export const emptyCounts = () => ({
13
16
  waiting: 0,
14
17
  active: 0,
@@ -17,10 +20,6 @@ export const emptyCounts = () => ({
17
20
  delayed: 0,
18
21
  paused: 0,
19
22
  });
20
- /**
21
- * Map a list of known queue names to entries with empty counts. Keeps the
22
- * dashboard showing the configured queues during transient backend outages.
23
- */
24
23
  export const emptyQueueStats = (queueNames) => queueNames.map((name) => ({ name, counts: emptyCounts() }));
25
24
  const shouldUseRedisRpcMonitorDriver = () => Env.USE_REDIS_PROXY === true && Env.get('REDIS_RPC_URL', '').trim() !== '';
26
25
  const resolveRpcBaseUrl = () => {
@@ -66,83 +65,88 @@ const createRpcClient = async () => Object.freeze({
66
65
  queue: (method, payload = {}) => callRedisRpc('queue', method, payload),
67
66
  monitor: (method, payload = {}) => callRedisRpc('queue-monitor', method, payload),
68
67
  });
69
- const createRedisRpcDriver = () => {
70
- const enqueue = async (name, payload, options) => {
68
+ const createRedisRpcEnqueue = async (name, payload, options) => {
69
+ const client = await createRpcClient();
70
+ const result = await client.queue('add', {
71
+ target: name,
72
+ args: ['default', payload, options ?? {}],
73
+ });
74
+ if (result.id === undefined || result.id === null) {
75
+ throw ErrorFactory.createTryCatchError(`Queue job id missing for ${name}`);
76
+ }
77
+ return String(result.id);
78
+ };
79
+ const createRedisRpcGetJob = async (queueName, jobId) => {
80
+ const client = await createRpcClient();
81
+ try {
82
+ return await client.queue('getJob', { target: queueName, args: [jobId] });
83
+ }
84
+ catch (error) {
85
+ Logger.warn('[queue-monitor] Redis RPC job lookup failed; returning no job', error);
86
+ return undefined;
87
+ }
88
+ };
89
+ const createRedisRpcGetJobCounts = async (queueName) => {
90
+ try {
71
91
  const client = await createRpcClient();
72
- const result = await client.queue('add', {
73
- target: name,
74
- args: ['default', payload, options ?? {}],
92
+ return client.queue('getJobCounts', { target: queueName });
93
+ }
94
+ catch (error) {
95
+ Logger.warn('[queue-monitor] Redis RPC job counts failed; returning empty counts', error);
96
+ return {};
97
+ }
98
+ };
99
+ const createRedisRpcGetJobCountsMany = async (queueNames) => {
100
+ const client = await createRpcClient();
101
+ try {
102
+ const snapshot = await client.monitor('getSnapshot', { args: [queueNames] });
103
+ return snapshot.queues ?? emptyQueueStats(queueNames);
104
+ }
105
+ catch (error) {
106
+ Logger.warn('[queue-monitor] Redis RPC batch counts failed; returning known queues with empty counts', error);
107
+ return emptyQueueStats(queueNames);
108
+ }
109
+ };
110
+ const createRedisRpcRecentJobs = async (queueName, limit = 100) => {
111
+ const client = await createRpcClient();
112
+ try {
113
+ return await client.monitor('getRecentJobsForQueue', {
114
+ args: [queueName, limit],
75
115
  });
76
- if (result.id === undefined || result.id === null) {
77
- throw ErrorFactory.createTryCatchError(`Queue job id missing for ${name}`);
78
- }
79
- return String(result.id);
80
- };
81
- return Object.freeze({
82
- enqueue,
83
- async getJob(queueName, jobId) {
84
- const client = await createRpcClient();
85
- try {
86
- return (await client.queue('getJob', { target: queueName, args: [jobId] }));
87
- }
88
- catch (error) {
89
- Logger.warn('[queue-monitor] Redis RPC job lookup failed; returning no job', error);
90
- return undefined;
91
- }
92
- },
93
- async getJobCounts(queueName) {
94
- const client = await createRpcClient();
95
- try {
96
- return client.queue('getJobCounts', { target: queueName });
97
- }
98
- catch (error) {
99
- Logger.warn('[queue-monitor] Redis RPC job counts failed; returning empty counts', error);
100
- return {};
101
- }
102
- },
103
- async getJobCountsMany(queueNames) {
104
- const client = await createRpcClient();
105
- try {
106
- const snapshot = await client.monitor('getSnapshot', { args: [queueNames] });
107
- return snapshot.queues ?? emptyQueueStats(queueNames);
108
- }
109
- catch (error) {
110
- Logger.warn('[queue-monitor] Redis RPC batch counts failed; returning known queues with empty counts', error);
111
- return emptyQueueStats(queueNames);
112
- }
113
- },
114
- async getRecentJobs(queueName, limit = 100) {
115
- const client = await createRpcClient();
116
- try {
117
- return (await client.monitor('getRecentJobsForQueue', {
118
- args: [queueName, limit],
119
- }));
120
- }
121
- catch (error) {
122
- Logger.warn('[queue-monitor] Redis RPC recent jobs failed; returning no jobs', error);
123
- return [];
124
- }
125
- },
126
- async retryJob(queueName, jobId) {
127
- const client = await createRpcClient();
128
- return client.queue('retryJob', { target: queueName, args: [jobId] });
129
- },
130
- async getQueues() {
131
- const client = await createRpcClient();
132
- try {
133
- const snapshot = await client.monitor('getSnapshot');
134
- return (snapshot.queues ?? []).map((queue) => queue.name);
135
- }
136
- catch (error) {
137
- Logger.warn('[queue-monitor] Redis RPC queue discovery failed; returning no queues', error);
138
- return [];
139
- }
140
- },
141
- async close() {
142
- await Promise.resolve();
143
- },
144
- });
116
+ }
117
+ catch (error) {
118
+ Logger.warn('[queue-monitor] Redis RPC recent jobs failed; returning no jobs', error);
119
+ return [];
120
+ }
121
+ };
122
+ const createRedisRpcRetryJob = async (queueName, jobId) => {
123
+ const client = await createRpcClient();
124
+ return client.queue('retryJob', { target: queueName, args: [jobId] });
125
+ };
126
+ const createRedisRpcGetQueues = async () => {
127
+ const client = await createRpcClient();
128
+ try {
129
+ const snapshot = await client.monitor('getSnapshot');
130
+ return (snapshot.queues ?? []).map((queue) => queue.name);
131
+ }
132
+ catch (error) {
133
+ Logger.warn('[queue-monitor] Redis RPC queue discovery failed; returning no queues', error);
134
+ return [];
135
+ }
136
+ };
137
+ const createRedisRpcClose = async () => {
138
+ await Promise.resolve();
145
139
  };
140
+ const createRedisRpcDriver = () => Object.freeze({
141
+ enqueue: createRedisRpcEnqueue,
142
+ getJob: createRedisRpcGetJob,
143
+ getJobCounts: createRedisRpcGetJobCounts,
144
+ getJobCountsMany: createRedisRpcGetJobCountsMany,
145
+ getRecentJobs: createRedisRpcRecentJobs,
146
+ retryJob: createRedisRpcRetryJob,
147
+ getQueues: createRedisRpcGetQueues,
148
+ close: createRedisRpcClose,
149
+ });
146
150
  async function enrichJobsWithState(jobs) {
147
151
  await Promise.all(jobs.map(async (job) => {
148
152
  try {
@@ -183,18 +187,12 @@ async function discoverQueuesFromRedis(redis, inMemoryQueues) {
183
187
  }
184
188
  return Array.from(found.values());
185
189
  }
186
- // eslint-disable-next-line max-lines-per-function
187
- export const createBullMQDriver = (config) => {
188
- if (shouldUseRedisRpcMonitorDriver())
189
- return createRedisRpcDriver();
190
- const queues = new Map();
191
- const redis = createRedisConnection(config, 3, {
192
- subsystem: 'queue-monitor',
193
- });
194
- const getQueue = (name) => {
190
+ const createBullMQQueueGetter = (queues, redis) => {
191
+ return async (name) => {
195
192
  if (!queues.has(name)) {
193
+ const QueueCtor = await ensureBullmqLoaded();
196
194
  const prefix = getBullMQSafeQueueName();
197
- const queue = new Queue(name, { prefix, connection: redis });
195
+ const queue = new QueueCtor(name, { prefix, connection: redis });
198
196
  queues.set(name, queue);
199
197
  }
200
198
  const queue = queues.get(name);
@@ -203,120 +201,126 @@ export const createBullMQDriver = (config) => {
203
201
  }
204
202
  return queue;
205
203
  };
206
- const enqueue = async (name, payload, options) => {
207
- const queue = getQueue(name);
208
- const job = await queue.add('default', payload, {
209
- removeOnComplete: true,
210
- removeOnFail: false,
211
- ...options,
204
+ };
205
+ const createBullMQEnqueue = (getQueue) => async (name, payload, options) => {
206
+ const queue = await getQueue(name);
207
+ const job = await queue.add('default', payload, {
208
+ removeOnComplete: true,
209
+ removeOnFail: false,
210
+ ...options,
211
+ });
212
+ if (job.id === undefined || job.id === null) {
213
+ throw ErrorFactory.createTryCatchError(`Queue job id missing for ${name}`);
214
+ }
215
+ return String(job.id);
216
+ };
217
+ const createBullMQGetJob = (getQueue) => async (queueName, jobId) => {
218
+ const queue = await getQueue(queueName);
219
+ return (await queue.getJob(jobId)) || undefined;
220
+ };
221
+ const createBullMQGetJobCounts = (getQueue) => async (queueName) => {
222
+ const queue = await getQueue(queueName);
223
+ return queue.getJobCounts();
224
+ };
225
+ const createBullMQGetJobCountsMany = (getJobCounts) => async (queueNames) => {
226
+ const uniqueQueueNames = Array.from(new Set(queueNames.filter((queueName) => typeof queueName === 'string' && queueName.trim().length > 0)));
227
+ const QUEUE_MONITOR_LOGGING_ENABLED = Env.getBool('QUEUE_MONITOR_LOGGING_ENABLED', false);
228
+ if (QUEUE_MONITOR_LOGGING_ENABLED) {
229
+ Logger.info('[queue-monitor] getJobCountsMany start', {
230
+ requestedCount: queueNames.length,
231
+ uniqueCount: uniqueQueueNames.length,
212
232
  });
213
- if (job.id === undefined || job.id === null) {
214
- throw ErrorFactory.createTryCatchError(`Queue job id missing for ${name}`);
215
- }
216
- return String(job.id);
217
- };
218
- const getJob = async (queueName, jobId) => {
219
- const queue = getQueue(queueName);
220
- return (await queue.getJob(jobId)) || undefined;
221
- };
222
- const getJobCounts = async (queueName) => {
223
- const queue = getQueue(queueName);
224
- return queue.getJobCounts();
225
- };
226
- const getJobCountsMany = async (queueNames) => {
227
- const uniqueQueueNames = Array.from(new Set(queueNames.filter((queueName) => typeof queueName === 'string' && queueName.trim().length > 0)));
228
- const QUEUE_MONITOR_LOGGING_ENABLED = Env.getBool('QUEUE_MONITOR_LOGGING_ENABLED', false);
229
- if (QUEUE_MONITOR_LOGGING_ENABLED) {
230
- Logger.info('[queue-monitor] getJobCountsMany start', {
231
- requestedCount: queueNames.length,
232
- uniqueCount: uniqueQueueNames.length,
233
- });
234
- }
235
- const startedAt = Date.now();
236
- if (uniqueQueueNames.length === 0) {
237
- if (QUEUE_MONITOR_LOGGING_ENABLED) {
238
- Logger.info('[queue-monitor] getJobCountsMany complete', {
239
- durationMs: Date.now() - startedAt,
240
- requestedCount: queueNames.length,
241
- uniqueCount: uniqueQueueNames.length,
242
- pipelineCount: 0,
243
- });
244
- }
245
- return [];
246
- }
247
- const stats = await Promise.all(uniqueQueueNames.map(async (name) => {
248
- const counts = await getJobCounts(name);
249
- return { name, counts };
250
- }));
233
+ }
234
+ const startedAt = Date.now();
235
+ if (uniqueQueueNames.length === 0) {
251
236
  if (QUEUE_MONITOR_LOGGING_ENABLED) {
252
237
  Logger.info('[queue-monitor] getJobCountsMany complete', {
253
238
  durationMs: Date.now() - startedAt,
254
239
  requestedCount: queueNames.length,
255
240
  uniqueCount: uniqueQueueNames.length,
256
- pipelineCount: uniqueQueueNames.length,
241
+ pipelineCount: 0,
257
242
  });
258
243
  }
259
- return stats;
260
- };
261
- const requeueFromSnapshot = async (queue, snapshot) => {
262
- try {
263
- const requeued = await queue.add(snapshot.name ?? 'default', snapshot.data, snapshot.opts);
264
- return {
265
- ok: true,
266
- status: 'requeued_from_snapshot',
267
- newJobId: requeued.id === undefined || requeued.id === null ? undefined : String(requeued.id),
268
- };
269
- }
270
- catch (error) {
271
- return {
272
- ok: false,
273
- status: 'not_retryable',
274
- reason: error instanceof Error ? error.message : String(error),
275
- };
276
- }
277
- };
278
- const getRecentJobs = async (queueName, limit = 100) => {
279
- const queue = getQueue(queueName);
280
- const jobs = await queue.getJobs(['completed', 'failed', 'active', 'waiting', 'delayed', 'paused'], 0, Math.max(0, limit - 1), true);
281
- // Fetch state for each job to ensure accurate status detection
282
- await enrichJobsWithState(jobs);
283
- return jobs;
284
- };
285
- const retryJob = async (queueName, jobId, snapshot) => {
286
- const queue = getQueue(queueName);
287
- const job = await getJob(queueName, jobId);
288
- if (!job) {
289
- if (snapshot)
290
- return requeueFromSnapshot(queue, snapshot);
291
- return { ok: false, status: 'missing' };
292
- }
293
- try {
294
- await job.retry();
295
- return { ok: true, status: 'retried' };
296
- }
297
- catch (error) {
298
- return {
299
- ok: false,
300
- status: 'not_retryable',
301
- reason: error instanceof Error ? error.message : String(error),
302
- };
303
- }
304
- };
305
- const getQueues = async () => {
306
- return discoverQueuesFromRedis(redis, queues);
307
- };
308
- const close = async () => {
309
- const closes = Array.from(queues.values()).map((q) => q.close());
310
- await Promise.all(closes);
311
- };
244
+ return [];
245
+ }
246
+ const stats = await Promise.all(uniqueQueueNames.map(async (name) => {
247
+ const counts = await getJobCounts(name);
248
+ return { name, counts };
249
+ }));
250
+ if (QUEUE_MONITOR_LOGGING_ENABLED) {
251
+ Logger.info('[queue-monitor] getJobCountsMany complete', {
252
+ durationMs: Date.now() - startedAt,
253
+ requestedCount: queueNames.length,
254
+ uniqueCount: uniqueQueueNames.length,
255
+ pipelineCount: uniqueQueueNames.length,
256
+ });
257
+ }
258
+ return stats;
259
+ };
260
+ const createBullMQRequeueFromSnapshot = (queue) => async (snapshot) => {
261
+ try {
262
+ const requeued = await queue.add(snapshot.name ?? 'default', snapshot.data, snapshot.opts);
263
+ return {
264
+ ok: true,
265
+ status: 'requeued_from_snapshot',
266
+ newJobId: requeued.id === undefined || requeued.id === null ? undefined : String(requeued.id),
267
+ };
268
+ }
269
+ catch (error) {
270
+ return {
271
+ ok: false,
272
+ status: 'not_retryable',
273
+ reason: error instanceof Error ? error.message : String(error),
274
+ };
275
+ }
276
+ };
277
+ const createBullMQRetryJob = (getQueue, getJob) => async (queueName, jobId, snapshot) => {
278
+ const queue = await getQueue(queueName);
279
+ const job = await getJob(queueName, jobId);
280
+ if (!job) {
281
+ if (snapshot)
282
+ return createBullMQRequeueFromSnapshot(queue)(snapshot);
283
+ return { ok: false, status: 'missing' };
284
+ }
285
+ try {
286
+ await job.retry();
287
+ return { ok: true, status: 'retried' };
288
+ }
289
+ catch (error) {
290
+ return {
291
+ ok: false,
292
+ status: 'not_retryable',
293
+ reason: error instanceof Error ? error.message : String(error),
294
+ };
295
+ }
296
+ };
297
+ const createBullMQGetRecentJobs = (getQueue) => async (queueName, limit = 100) => {
298
+ const queue = await getQueue(queueName);
299
+ const jobs = await queue.getJobs(['completed', 'failed', 'active', 'waiting', 'delayed', 'paused'], 0, Math.max(0, limit - 1), true);
300
+ await enrichJobsWithState(jobs);
301
+ return jobs;
302
+ };
303
+ const createBullMQGetQueues = (redis, queues) => async () => discoverQueuesFromRedis(redis, queues);
304
+ const createBullMQClose = (queues) => async () => {
305
+ const closes = Array.from(queues.values()).map((q) => q.close());
306
+ await Promise.all(closes);
307
+ };
308
+ export const createBullMQDriver = (config) => {
309
+ if (shouldUseRedisRpcMonitorDriver())
310
+ return createRedisRpcDriver();
311
+ const queues = new Map();
312
+ const redis = createRedisConnection(config, 3, {
313
+ subsystem: 'queue-monitor',
314
+ });
315
+ const getQueue = createBullMQQueueGetter(queues, redis);
312
316
  return Object.freeze({
313
- enqueue,
314
- getJob,
315
- getJobCounts,
316
- getJobCountsMany,
317
- getRecentJobs,
318
- retryJob,
319
- getQueues,
320
- close,
317
+ enqueue: createBullMQEnqueue(getQueue),
318
+ getJob: createBullMQGetJob(getQueue),
319
+ getJobCounts: createBullMQGetJobCounts(getQueue),
320
+ getJobCountsMany: createBullMQGetJobCountsMany(createBullMQGetJobCounts(getQueue)),
321
+ getRecentJobs: createBullMQGetRecentJobs(getQueue),
322
+ retryJob: createBullMQRetryJob(getQueue, createBullMQGetJob(getQueue)),
323
+ getQueues: createBullMQGetQueues(redis, queues),
324
+ close: createBullMQClose(queues),
321
325
  });
322
326
  };
package/dist/worker.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { type Processor } from 'bullmq';
1
+ import type { Processor } from 'bullmq';
2
2
  import { type RedisConfig } from './connection.js';
3
3
  import type { Metrics } from './metrics.js';
4
4
  export type QueueWorker = {
5
5
  close: () => Promise<void>;
6
6
  };
7
- export declare const createWorker: (queueName: string, processor: Processor, redisConfig: RedisConfig, metrics: Metrics) => QueueWorker;
7
+ export declare const createWorker: (queueName: string, processor: Processor, redisConfig: RedisConfig, metrics: Metrics) => Promise<QueueWorker>;
package/dist/worker.js CHANGED
@@ -1,9 +1,11 @@
1
1
  import { getBullMQSafeQueueName } from '@zintrust/core/redis';
2
- import { Worker } from 'bullmq';
3
2
  import { createRedisConnection } from './connection.js';
4
- export const createWorker = (queueName, processor, redisConfig, metrics) => {
3
+ export const createWorker = async (queueName, processor, redisConfig, metrics) => {
5
4
  const connection = createRedisConnection(redisConfig, 3, { subsystem: 'queue-monitor-worker' });
6
5
  const prefix = getBullMQSafeQueueName();
6
+ // Variable specifier so bundlers do not inline bullmq into the Workers bundle.
7
+ const bullmqPkg = 'bullmq';
8
+ const { Worker } = await import(bullmqPkg);
7
9
  const worker = new Worker(queueName, processor, {
8
10
  connection: connection,
9
11
  prefix,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zintrust/queue-monitor",
3
- "version": "2.4.4",
3
+ "version": "2.4.6",
4
4
  "description": "Queue monitoring package for ZinTrust with BullMQ and Redis.",
5
5
  "private": false,
6
6
  "type": "module",