@zintrust/redis-rpc 2.4.6 → 2.4.8
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 +3 -0
- package/dist/backend.js +51 -1
- package/dist/client.js +26 -35
- package/package.json +1 -1
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,8 @@ 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
|
+
Before claiming new work, `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, their pull-claim keys are released, and normal retry or replay policy can handle follow-up processing.
|
|
210
|
+
|
|
208
211
|
### `worker`
|
|
209
212
|
|
|
210
213
|
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,58 @@ const releaseClaim = async (state, queueName, jobId) => {
|
|
|
264
268
|
connection.disconnect();
|
|
265
269
|
}
|
|
266
270
|
};
|
|
271
|
+
const staleActiveThresholdMs = (visibilityTimeoutMs) => {
|
|
272
|
+
const override = positiveNumberFrom(process.env['REDIS_RPC_STALE_ACTIVE_MS']);
|
|
273
|
+
if (override !== null) {
|
|
274
|
+
return override;
|
|
275
|
+
}
|
|
276
|
+
return Math.max(visibilityTimeoutMs * 2, 120_000);
|
|
277
|
+
};
|
|
278
|
+
const recoverStaleActiveJobs = async (state, queueName, queue, visibilityTimeoutMs) => {
|
|
279
|
+
const thresholdMs = staleActiveThresholdMs(visibilityTimeoutMs);
|
|
280
|
+
const activeJobs = await queue.getJobs(['active'], 0, 99, true);
|
|
281
|
+
const client = await queue.client;
|
|
282
|
+
for (const job of activeJobs) {
|
|
283
|
+
if (isUndefinedOrNull(job.processedOn) || isUndefinedOrNull(job.id)) {
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const processedOn = Number(job.processedOn);
|
|
287
|
+
if (!Number.isFinite(processedOn)) {
|
|
288
|
+
continue;
|
|
289
|
+
}
|
|
290
|
+
const ageMs = Date.now() - processedOn;
|
|
291
|
+
if (ageMs <= thresholdMs) {
|
|
292
|
+
continue;
|
|
293
|
+
}
|
|
294
|
+
const jobId = String(job.id);
|
|
295
|
+
try {
|
|
296
|
+
// eslint-disable-next-line no-await-in-loop
|
|
297
|
+
await client.set(queue.toKey(jobId) + ':lock', PULL_WORKER_TOKEN, 'PX', visibilityTimeoutMs);
|
|
298
|
+
// eslint-disable-next-line no-await-in-loop
|
|
299
|
+
await job.moveToFailed(new Error(`stale active job recovered after ${ageMs}ms without ack/fail`), PULL_WORKER_TOKEN, false);
|
|
300
|
+
// eslint-disable-next-line no-await-in-loop
|
|
301
|
+
await releaseClaim(state, queueName, jobId);
|
|
302
|
+
}
|
|
303
|
+
catch (error) {
|
|
304
|
+
Logger.warn('Redis RPC stale active recovery failed', {
|
|
305
|
+
queueName,
|
|
306
|
+
jobId,
|
|
307
|
+
thresholdMs,
|
|
308
|
+
processedOn,
|
|
309
|
+
ageMs,
|
|
310
|
+
error,
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
};
|
|
267
315
|
const dequeueQueueJob = async (state, queueName, payload) => {
|
|
268
316
|
const queue = getQueue(state, queueName);
|
|
269
|
-
|
|
317
|
+
const visibilityTimeoutMs = Math.max(1, numberFrom(payload.visibilityTimeoutMs, 30_000));
|
|
318
|
+
queue.opts.lockDuration = visibilityTimeoutMs;
|
|
270
319
|
const scripts = queue.scripts;
|
|
271
320
|
if (scripts === undefined)
|
|
272
321
|
return undefined;
|
|
322
|
+
await recoverStaleActiveJobs(state, queueName, queue, visibilityTimeoutMs);
|
|
273
323
|
const client = await queue.client;
|
|
274
324
|
const [jobData, jobId] = await scripts.moveToActive(client, PULL_WORKER_TOKEN, '');
|
|
275
325
|
if (!jobData || isUndefinedOrNull(jobId))
|
package/dist/client.js
CHANGED
|
@@ -1,39 +1,30 @@
|
|
|
1
1
|
import { ErrorFactory, isUndefinedOrNull } from '@zintrust/core/runtime';
|
|
2
|
-
import { randomUUID } from 'node:crypto';
|
|
3
|
-
import http from 'node:http';
|
|
4
|
-
import https from 'node:https';
|
|
5
2
|
import { rpcClientHeaders, rpcServerOptions } from './env.js';
|
|
6
|
-
const requestJson = (url, body, headers) => {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
|
-
});
|
|
34
|
-
request.on('error', (error) => reject(ErrorFactory.createConnectionError('Redis RPC request failed', { error })));
|
|
35
|
-
request.end(body);
|
|
36
|
-
});
|
|
3
|
+
const requestJson = async (url, body, headers) => {
|
|
4
|
+
let response;
|
|
5
|
+
try {
|
|
6
|
+
response = await fetch(url, { method: 'POST', headers, body });
|
|
7
|
+
}
|
|
8
|
+
catch (error) {
|
|
9
|
+
throw ErrorFactory.createConnectionError('Redis RPC request failed', { error });
|
|
10
|
+
}
|
|
11
|
+
let text;
|
|
12
|
+
try {
|
|
13
|
+
text = await response.text();
|
|
14
|
+
}
|
|
15
|
+
catch (error) {
|
|
16
|
+
throw ErrorFactory.createTryCatchError('Redis RPC response read failed', { error });
|
|
17
|
+
}
|
|
18
|
+
try {
|
|
19
|
+
return {
|
|
20
|
+
statusCode: response.status,
|
|
21
|
+
ok: response.ok,
|
|
22
|
+
body: isUndefinedOrNull(text.trim()) ? {} : JSON.parse(text),
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
catch (error) {
|
|
26
|
+
throw ErrorFactory.createTryCatchError('Redis RPC response parse failed', { error });
|
|
27
|
+
}
|
|
37
28
|
};
|
|
38
29
|
const createServiceProxy = (client, service, target) => {
|
|
39
30
|
return new Proxy(Object.create(null), {
|
|
@@ -54,7 +45,7 @@ export const createRedisRpcClient = (options = {}) => {
|
|
|
54
45
|
call: async (service, method, payload = {}) => {
|
|
55
46
|
const url = new URL('/rpc', baseUrl);
|
|
56
47
|
const body = JSON.stringify({
|
|
57
|
-
requestId: randomUUID(),
|
|
48
|
+
requestId: globalThis.crypto.randomUUID(),
|
|
58
49
|
service,
|
|
59
50
|
method,
|
|
60
51
|
payload,
|