@zintrust/redis-rpc 2.4.7 → 2.4.9

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/README.md CHANGED
@@ -52,6 +52,7 @@ The server and client read these variables:
52
52
  | `REDIS_RPC_REDIS_PASSWORD` | `REDIS_PASSWORD` | Redis password used by the RPC backend. |
53
53
  | `REDIS_RPC_REDIS_DB` | `REDIS_QUEUE_DB`, `REDIS_DB`, or `0` | Redis database used for queue operations. |
54
54
  | `REDIS_RPC_BULLMQ_PREFIX` | `BULLMQ_PREFIX` or `bull` | BullMQ key prefix used by the backend. |
55
+ | `REDIS_RPC_STALE_ACTIVE_MS`| `max(visibilityTimeoutMs * 2, 120000)` | Pull-worker stale-active recovery threshold before failing abandoned active jobs. |
55
56
  | `REDIS_RPC_TIMEOUT_MS` | `30000` | Client-side timeout used by integrations. |
56
57
  | `REDIS_RPC_RETRY_MAX` | `2` | Client-side retry count used by integrations. |
57
58
  | `REDIS_RPC_RETRY_DELAY_MS` | `500` | Client-side retry delay used by integrations. |
@@ -205,6 +206,10 @@ Queue requests identify the queue with `payload.target`, `payload.queueName`, or
205
206
 
206
207
  `dequeue` is for pull-based runtimes. It uses BullMQ's own atomic waiting-to-active transition and returns `{ id, name, payload, attempts }`. The caller must report the result with `ack` or `fail` / `nack`. `ack` accepts an optional return value as `payload.returnValue`, `payload.returnvalue`, or the second positional arg. `fail` / `nack` accepts an optional failure reason as `payload.reason` or the second positional arg.
207
208
 
209
+ As of `@zintrust/redis-rpc@2.4.8`, `dequeue` also scans up to 100 active jobs for the queue and recovers stale pull-worker jobs whose `processedOn` age exceeds `REDIS_RPC_STALE_ACTIVE_MS`. When the variable is unset, the backend uses `max(visibilityTimeoutMs * 2, 120000)`. Recovered jobs are marked failed and their pull-claim keys are released so normal replay policy can handle follow-up processing.
210
+
211
+ The local source also discards recovered stale jobs before `moveToFailed(...)` so they become terminal failures instead of delayed retries when attempts remain. Queue-monitor snapshot and recent-job reads run the same recovery pass, which keeps stale active jobs from lingering when no pull worker is polling. Manual recovery can use `queue.fail` with `force: true` or `discard: true` to recreate the pull-worker lock, discard retries, and fail the job.
212
+
208
213
  ### `worker`
209
214
 
210
215
  Supported methods:
package/dist/backend.js CHANGED
@@ -100,6 +100,10 @@ const numberFrom = (value, fallback) => {
100
100
  const parsed = Number(value);
101
101
  return Number.isFinite(parsed) ? parsed : fallback;
102
102
  };
103
+ const positiveNumberFrom = (value) => {
104
+ const parsed = Number(value);
105
+ return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
106
+ };
103
107
  const boolFrom = (value, fallback = false) => typeof value === 'boolean' ? value : fallback;
104
108
  const normalizeStates = (value) => {
105
109
  if (!isArray(value)) {
@@ -264,12 +268,62 @@ const releaseClaim = async (state, queueName, jobId) => {
264
268
  connection.disconnect();
265
269
  }
266
270
  };
271
+ const ensurePullWorkerJobLock = async (queue, jobId, ttlMs) => {
272
+ const client = await queue.client;
273
+ await client.set(queue.toKey(jobId) + ':lock', PULL_WORKER_TOKEN, 'PX', Math.max(1, ttlMs));
274
+ };
275
+ const staleActiveThresholdMs = (visibilityTimeoutMs) => {
276
+ const override = positiveNumberFrom(process.env['REDIS_RPC_STALE_ACTIVE_MS']);
277
+ if (override !== null) {
278
+ return override;
279
+ }
280
+ return Math.max(visibilityTimeoutMs * 2, 120_000);
281
+ };
282
+ const recoverStaleActiveJobs = async (state, queueName, queue, visibilityTimeoutMs) => {
283
+ const thresholdMs = staleActiveThresholdMs(visibilityTimeoutMs);
284
+ const activeJobs = await queue.getJobs(['active'], 0, 99, true);
285
+ for (const job of activeJobs) {
286
+ if (isUndefinedOrNull(job.processedOn) || isUndefinedOrNull(job.id)) {
287
+ continue;
288
+ }
289
+ const processedOn = Number(job.processedOn);
290
+ if (!Number.isFinite(processedOn)) {
291
+ continue;
292
+ }
293
+ const ageMs = Date.now() - processedOn;
294
+ if (ageMs <= thresholdMs) {
295
+ continue;
296
+ }
297
+ const jobId = String(job.id);
298
+ try {
299
+ job.discard();
300
+ // eslint-disable-next-line no-await-in-loop
301
+ await ensurePullWorkerJobLock(queue, jobId, visibilityTimeoutMs);
302
+ // eslint-disable-next-line no-await-in-loop
303
+ await job.moveToFailed(new Error(`stale active job recovered after ${ageMs}ms without ack/fail`), PULL_WORKER_TOKEN, false);
304
+ // eslint-disable-next-line no-await-in-loop
305
+ await releaseClaim(state, queueName, jobId);
306
+ }
307
+ catch (error) {
308
+ Logger.warn('Redis RPC stale active recovery failed', {
309
+ queueName,
310
+ jobId,
311
+ thresholdMs,
312
+ processedOn,
313
+ ageMs,
314
+ error,
315
+ });
316
+ }
317
+ }
318
+ };
267
319
  const dequeueQueueJob = async (state, queueName, payload) => {
268
320
  const queue = getQueue(state, queueName);
269
- queue.opts.lockDuration = Math.max(1, numberFrom(payload.visibilityTimeoutMs, 30_000));
321
+ const visibilityTimeoutMs = Math.max(1, numberFrom(payload.visibilityTimeoutMs, 30_000));
322
+ queue.opts.lockDuration = visibilityTimeoutMs;
270
323
  const scripts = queue.scripts;
271
324
  if (scripts === undefined)
272
325
  return undefined;
326
+ await recoverStaleActiveJobs(state, queueName, queue, visibilityTimeoutMs);
273
327
  const client = await queue.client;
274
328
  const [jobData, jobId] = await scripts.moveToActive(client, PULL_WORKER_TOKEN, '');
275
329
  if (!jobData || isUndefinedOrNull(jobId))
@@ -303,6 +357,12 @@ const failQueueJob = async (state, queueName, payload) => {
303
357
  const reason = isNonEmptyString(firstDefined(args[1], payload.reason))
304
358
  ? String(firstDefined(args[1], payload.reason))
305
359
  : 'failed by pull worker';
360
+ if (boolFrom(payload.force, false) || boolFrom(payload.discard, false)) {
361
+ if (!isUndefinedOrNull(job.id)) {
362
+ await ensurePullWorkerJobLock(getQueue(state, queueName), String(job.id), Math.max(1, numberFrom(payload.visibilityTimeoutMs, 30_000)));
363
+ }
364
+ job.discard();
365
+ }
306
366
  await job.moveToFailed(new Error(reason), PULL_WORKER_TOKEN, false);
307
367
  if (!isUndefinedOrNull(job.id)) {
308
368
  await releaseClaim(state, queueName, String(job.id));
@@ -548,7 +608,9 @@ const dispatchMonitor = async (state, method, payload) => {
548
608
  }
549
609
  const queues = await Promise.all(queueNames.map(async (name) => {
550
610
  const queueName = requireString(name, 'queueName');
551
- return { name: queueName, counts: await getQueue(state, queueName).getJobCounts() };
611
+ const queue = getQueue(state, queueName);
612
+ await recoverStaleActiveJobs(state, queueName, queue, numberFrom(payload.visibilityTimeoutMs, 30_000));
613
+ return { name: queueName, counts: await queue.getJobCounts() };
552
614
  }));
553
615
  return { status: 'ok', startedAt: new Date().toISOString(), queues };
554
616
  }
@@ -560,6 +622,7 @@ const dispatchMonitor = async (state, method, payload) => {
560
622
  }
561
623
  case 'getRecentJobsForQueue': {
562
624
  const queueName = requireString(firstDefined(args[0], payload.queueName, payload.queue), 'queueName');
625
+ await recoverStaleActiveJobs(state, queueName, getQueue(state, queueName), numberFrom(payload.visibilityTimeoutMs, 30_000));
563
626
  return dispatchQueue(state, 'getJobs', {
564
627
  ...payload,
565
628
  args: [],
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zintrust/redis-rpc",
3
- "version": "2.4.7",
3
+ "version": "2.4.9",
4
4
  "description": "Redis RPC backend for BullMQ queue operations in ZinTrust.",
5
5
  "private": false,
6
6
  "type": "module",