@zintrust/workers 2.4.2 → 2.4.4
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/SLAMonitor.d.ts +7 -0
- package/dist/SLAMonitor.js +17 -0
- package/dist/WorkerFactory.js +17 -0
- package/dist/build-manifest.json +27 -19
- package/dist/dashboard/workerManifest.d.ts +15 -0
- package/dist/dashboard/workerManifest.js +67 -0
- package/dist/dashboard/workers-api.js +173 -30
- package/dist/http/WorkerController.js +192 -7
- package/dist/index.d.ts +1 -1
- package/dist/routes/workers.js +9 -10
- package/package.json +1 -1
- package/src/SLAMonitor.ts +27 -0
- package/src/WorkerFactory.ts +24 -0
- package/src/dashboard/workerManifest.ts +112 -0
- package/src/dashboard/workers-api.ts +227 -31
- package/src/http/WorkerController.ts +257 -21
- package/src/index.ts +8 -1
- package/src/routes/workers.ts +18 -10
package/dist/SLAMonitor.d.ts
CHANGED
|
@@ -82,6 +82,11 @@ export type ISLAReport = {
|
|
|
82
82
|
complianceRate: number;
|
|
83
83
|
}>;
|
|
84
84
|
};
|
|
85
|
+
export type ISLAGate = {
|
|
86
|
+
allowed: boolean;
|
|
87
|
+
reason?: string;
|
|
88
|
+
status?: ISLAStatus;
|
|
89
|
+
};
|
|
85
90
|
/**
|
|
86
91
|
* SLA Monitor - Sealed namespace
|
|
87
92
|
*/
|
|
@@ -90,10 +95,12 @@ export declare const SLAMonitor: Readonly<{
|
|
|
90
95
|
* Define SLA for a worker
|
|
91
96
|
*/
|
|
92
97
|
defineSLA(config: ISLAConfig): void;
|
|
98
|
+
hasSLA(workerName: string): boolean;
|
|
93
99
|
/**
|
|
94
100
|
* Check SLA compliance for a worker
|
|
95
101
|
*/
|
|
96
102
|
checkCompliance(workerName: string): Promise<ISLAStatus>;
|
|
103
|
+
canSendWorkerRequest(workerName: string): Promise<ISLAGate>;
|
|
97
104
|
/**
|
|
98
105
|
* Get SLA violations for a worker
|
|
99
106
|
*/
|
package/dist/SLAMonitor.js
CHANGED
|
@@ -175,6 +175,9 @@ export const SLAMonitor = Object.freeze({
|
|
|
175
175
|
slaConfigs.set(config.workerName, { ...config });
|
|
176
176
|
Logger.info(`SLA defined for worker "${config.workerName}"`);
|
|
177
177
|
},
|
|
178
|
+
hasSLA(workerName) {
|
|
179
|
+
return slaConfigs.has(workerName);
|
|
180
|
+
},
|
|
178
181
|
/**
|
|
179
182
|
* Check SLA compliance for a worker
|
|
180
183
|
*/
|
|
@@ -211,6 +214,20 @@ export const SLAMonitor = Object.freeze({
|
|
|
211
214
|
checks,
|
|
212
215
|
};
|
|
213
216
|
},
|
|
217
|
+
async canSendWorkerRequest(workerName) {
|
|
218
|
+
if (!slaConfigs.has(workerName)) {
|
|
219
|
+
return { allowed: true, reason: 'sla_not_configured' };
|
|
220
|
+
}
|
|
221
|
+
const status = await SLAMonitor.checkCompliance(workerName);
|
|
222
|
+
if (status.status === 'compliant') {
|
|
223
|
+
return { allowed: true, status };
|
|
224
|
+
}
|
|
225
|
+
return {
|
|
226
|
+
allowed: false,
|
|
227
|
+
reason: `sla_${status.status}`,
|
|
228
|
+
status,
|
|
229
|
+
};
|
|
230
|
+
},
|
|
214
231
|
/**
|
|
215
232
|
* Get SLA violations for a worker
|
|
216
233
|
*/
|
package/dist/WorkerFactory.js
CHANGED
|
@@ -27,6 +27,7 @@ import { Observability } from './Observability.js';
|
|
|
27
27
|
import { PluginManager } from './PluginManager.js';
|
|
28
28
|
import { PriorityQueue } from './PriorityQueue.js';
|
|
29
29
|
import { ResourceMonitor } from './ResourceMonitor.js';
|
|
30
|
+
import { SLAMonitor } from './SLAMonitor.js';
|
|
30
31
|
import { WorkerMetrics } from './WorkerMetrics.js';
|
|
31
32
|
import { WorkerRegistry } from './WorkerRegistry.js';
|
|
32
33
|
import { WorkerVersioning } from './WorkerVersioning.js';
|
|
@@ -1803,6 +1804,17 @@ const buildWorkerRecord = (config, status) => {
|
|
|
1803
1804
|
connectionState: undefined,
|
|
1804
1805
|
};
|
|
1805
1806
|
};
|
|
1807
|
+
const assertSlaAllowsWorkerRequest = async (workerName) => {
|
|
1808
|
+
const gate = await SLAMonitor.canSendWorkerRequest(workerName);
|
|
1809
|
+
if (gate.allowed)
|
|
1810
|
+
return;
|
|
1811
|
+
throw ErrorFactory.createConfigError(`Worker "${workerName}" blocked because SLA status is not compliant`, {
|
|
1812
|
+
kind: 'worker_sla_blocked',
|
|
1813
|
+
workerName,
|
|
1814
|
+
reason: gate.reason,
|
|
1815
|
+
status: gate.status?.status,
|
|
1816
|
+
});
|
|
1817
|
+
};
|
|
1806
1818
|
const buildDefaultAutoScalerConfig = () => ({
|
|
1807
1819
|
enabled: workersConfig.autoScaling.enabled,
|
|
1808
1820
|
checkInterval: workersConfig.autoScaling.interval,
|
|
@@ -2179,6 +2191,9 @@ export const WorkerFactory = Object.freeze({
|
|
|
2179
2191
|
if (workers.has(name)) {
|
|
2180
2192
|
throw ErrorFactory.createWorkerError(`Worker "${name}" already exists`);
|
|
2181
2193
|
}
|
|
2194
|
+
if (autoStart) {
|
|
2195
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
2196
|
+
}
|
|
2182
2197
|
// Resolve the correct store for this worker configuration
|
|
2183
2198
|
const store = await getStoreForWorker(config);
|
|
2184
2199
|
// Save initial status as "creating"
|
|
@@ -2514,6 +2529,7 @@ export const WorkerFactory = Object.freeze({
|
|
|
2514
2529
|
if (persisted?.activeStatus === false) {
|
|
2515
2530
|
throw ErrorFactory.createConfigError(`Worker "${name}" is inactive`);
|
|
2516
2531
|
}
|
|
2532
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
2517
2533
|
const version = instance.config.version ?? '1.0.0';
|
|
2518
2534
|
await WorkerRegistry.start(name, version);
|
|
2519
2535
|
instance.status = WorkerCreationStatus.RUNNING;
|
|
@@ -2569,6 +2585,7 @@ export const WorkerFactory = Object.freeze({
|
|
|
2569
2585
|
if (record.activeStatus === false) {
|
|
2570
2586
|
throw ErrorFactory.createConfigError(`Worker "${name}" is inactive`);
|
|
2571
2587
|
}
|
|
2588
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
2572
2589
|
let processor;
|
|
2573
2590
|
try {
|
|
2574
2591
|
processor = await resolveStartFromPersistedProcessor(name, record, discovered);
|
package/dist/build-manifest.json
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@zintrust/workers",
|
|
3
|
-
"version": "2.4.
|
|
4
|
-
"buildDate": "2026-
|
|
3
|
+
"version": "2.4.3",
|
|
4
|
+
"buildDate": "2026-06-01T13:36:10.862Z",
|
|
5
5
|
"buildEnvironment": {
|
|
6
6
|
"node": "v22.22.1",
|
|
7
7
|
"platform": "darwin",
|
|
8
8
|
"arch": "arm64"
|
|
9
9
|
},
|
|
10
10
|
"git": {
|
|
11
|
-
"commit": "
|
|
11
|
+
"commit": "98a0caf4",
|
|
12
12
|
"branch": "release"
|
|
13
13
|
},
|
|
14
14
|
"package": {
|
|
@@ -167,20 +167,20 @@
|
|
|
167
167
|
"sha256": "3ae42ff5538ccd3e1a83830281b497fd612bdafa17e06a69d6a9073ae945cbe6"
|
|
168
168
|
},
|
|
169
169
|
"SLAMonitor.d.ts": {
|
|
170
|
-
"size":
|
|
171
|
-
"sha256": "
|
|
170
|
+
"size": 2929,
|
|
171
|
+
"sha256": "61f2c745a992e6ca7010eae9fa9080d6bf40fc276a392ffa62a375ce09d551a3"
|
|
172
172
|
},
|
|
173
173
|
"SLAMonitor.js": {
|
|
174
|
-
"size":
|
|
175
|
-
"sha256": "
|
|
174
|
+
"size": 10798,
|
|
175
|
+
"sha256": "25a06778c8f7c058aa78b45a598e98e8a92b7c6e7e16b9c25a5baba9facbecb5"
|
|
176
176
|
},
|
|
177
177
|
"WorkerFactory.d.ts": {
|
|
178
178
|
"size": 7764,
|
|
179
179
|
"sha256": "ea53fa39f3cec0f5841cf1ff556a41a70747b40cc64e8c9190e10ff360acc786"
|
|
180
180
|
},
|
|
181
181
|
"WorkerFactory.js": {
|
|
182
|
-
"size":
|
|
183
|
-
"sha256": "
|
|
182
|
+
"size": 112328,
|
|
183
|
+
"sha256": "fe2f8691cb3cce0a046a224c75396e2dcb0f8f1c19d1d5671c1d456211c28403"
|
|
184
184
|
},
|
|
185
185
|
"WorkerInit.d.ts": {
|
|
186
186
|
"size": 3284,
|
|
@@ -224,7 +224,7 @@
|
|
|
224
224
|
},
|
|
225
225
|
"build-manifest.json": {
|
|
226
226
|
"size": 19322,
|
|
227
|
-
"sha256": "
|
|
227
|
+
"sha256": "697064e1813050a1d930cbdda1f55c5535378f1141b14a42fbab0d9594b18489"
|
|
228
228
|
},
|
|
229
229
|
"config/workerConfig.d.ts": {
|
|
230
230
|
"size": 282,
|
|
@@ -258,13 +258,21 @@
|
|
|
258
258
|
"size": 11,
|
|
259
259
|
"sha256": "8e609bb71c20b858c77f0e9f90bb1319db8477b13f9f965f1a1e18524bf50881"
|
|
260
260
|
},
|
|
261
|
+
"dashboard/workerManifest.d.ts": {
|
|
262
|
+
"size": 643,
|
|
263
|
+
"sha256": "0fb2e2edf839aad96c613f84877c2a1ca3169c4b5d18546a49fe534c203efa0e"
|
|
264
|
+
},
|
|
265
|
+
"dashboard/workerManifest.js": {
|
|
266
|
+
"size": 2508,
|
|
267
|
+
"sha256": "858709fcb555180b5a591d66aae50a55f104ca6cd9db6d8bf952304b82a94d2b"
|
|
268
|
+
},
|
|
261
269
|
"dashboard/workers-api.d.ts": {
|
|
262
270
|
"size": 353,
|
|
263
271
|
"sha256": "8e0e04329e1119d8ae835dd4458efead084293bcc2c263c09dd5a19d467e5ca4"
|
|
264
272
|
},
|
|
265
273
|
"dashboard/workers-api.js": {
|
|
266
|
-
"size":
|
|
267
|
-
"sha256": "
|
|
274
|
+
"size": 39186,
|
|
275
|
+
"sha256": "4ab8b20822876dc616a54b702b5c0582403b4aa3ced897bf96843913365ab491"
|
|
268
276
|
},
|
|
269
277
|
"helper/index.d.ts": {
|
|
270
278
|
"size": 475,
|
|
@@ -287,8 +295,8 @@
|
|
|
287
295
|
"sha256": "48e9a6b0ec9d5821622e231f3ac6d6343eb86da30b4e95675f226f6c47d76996"
|
|
288
296
|
},
|
|
289
297
|
"http/WorkerController.js": {
|
|
290
|
-
"size":
|
|
291
|
-
"sha256": "
|
|
298
|
+
"size": 59802,
|
|
299
|
+
"sha256": "6372023bc736e5770de4de797bbed2666b562cfbf1f763975fdddc8f8c4a0aab"
|
|
292
300
|
},
|
|
293
301
|
"http/WorkerMonitoringService.d.ts": {
|
|
294
302
|
"size": 302,
|
|
@@ -403,12 +411,12 @@
|
|
|
403
411
|
"sha256": "c6a06c1e5ab72bf5f8c14dc23a979090e1389e6cb78d87543cd65d974a73815c"
|
|
404
412
|
},
|
|
405
413
|
"index.d.ts": {
|
|
406
|
-
"size":
|
|
407
|
-
"sha256": "
|
|
414
|
+
"size": 2828,
|
|
415
|
+
"sha256": "17ff95c19f86b2585015419ae495d4eab323e0983e6ca3ec4572dc634c123b11"
|
|
408
416
|
},
|
|
409
417
|
"index.js": {
|
|
410
418
|
"size": 2428,
|
|
411
|
-
"sha256": "
|
|
419
|
+
"sha256": "40a421af3c45f32455032bf256f32f5bcd45028d66d2ba7bed98710b2f9d5ffa"
|
|
412
420
|
},
|
|
413
421
|
"queueMonitorHistory.d.ts": {
|
|
414
422
|
"size": 433,
|
|
@@ -431,8 +439,8 @@
|
|
|
431
439
|
"sha256": "37b522f9b4498a3d5c3ee8006e796a9024e8606eedf9599922276f2d67293ece"
|
|
432
440
|
},
|
|
433
441
|
"routes/workers.js": {
|
|
434
|
-
"size":
|
|
435
|
-
"sha256": "
|
|
442
|
+
"size": 7085,
|
|
443
|
+
"sha256": "22dc4aeb6f51da59c58a479923f4c51e24e200a4a9fc25568739351f38cdb4bd"
|
|
436
444
|
},
|
|
437
445
|
"storage/WorkerStore.d.ts": {
|
|
438
446
|
"size": 1977,
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type WorkerManifestEntry = Readonly<{
|
|
2
|
+
name?: string;
|
|
3
|
+
workerName?: string;
|
|
4
|
+
queueName?: string;
|
|
5
|
+
processorSpec?: string | null;
|
|
6
|
+
concurrency?: number;
|
|
7
|
+
autoStart?: boolean;
|
|
8
|
+
activeStatus?: boolean;
|
|
9
|
+
status?: string;
|
|
10
|
+
}>;
|
|
11
|
+
export declare const readWorkerManifestGlobalKeys: () => string[];
|
|
12
|
+
export declare const getWorkerManifestEntries: () => WorkerManifestEntry[];
|
|
13
|
+
export declare const getWorkerManifestQueueNames: () => string[];
|
|
14
|
+
export declare const getWorkerManifestRecord: (workerName: string) => WorkerManifestEntry | undefined;
|
|
15
|
+
export declare const getWorkerManifestStatus: (workerName: string) => boolean;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { Env } from '@zintrust/core/config';
|
|
2
|
+
const DEFAULT_WORKER_MANIFEST_GLOBAL_KEYS = Object.freeze([
|
|
3
|
+
'__zintrustWorkersManifest',
|
|
4
|
+
'__zintrustWorkerManifest',
|
|
5
|
+
'__zintrustAppWorkerManifest',
|
|
6
|
+
]);
|
|
7
|
+
const normalizeKeyList = (values) => {
|
|
8
|
+
return Array.from(new Set(values.map((value) => value.trim()).filter((value) => value.length > 0)));
|
|
9
|
+
};
|
|
10
|
+
const readKeyListValue = (value) => {
|
|
11
|
+
if (!Array.isArray(value) || !value.every((item) => typeof item === 'string')) {
|
|
12
|
+
return undefined;
|
|
13
|
+
}
|
|
14
|
+
return normalizeKeyList(value);
|
|
15
|
+
};
|
|
16
|
+
export const readWorkerManifestGlobalKeys = () => {
|
|
17
|
+
const manifestGlobal = globalThis;
|
|
18
|
+
const runtimeKeys = readKeyListValue(manifestGlobal.workerManifestGlobalKeys) ??
|
|
19
|
+
readKeyListValue(manifestGlobal.__zintrustWorkerManifestGlobalKeys);
|
|
20
|
+
if (runtimeKeys && runtimeKeys.length > 0) {
|
|
21
|
+
return runtimeKeys;
|
|
22
|
+
}
|
|
23
|
+
const raw = Env.get('WORKER_MANIFEST_GLOBAL_KEYS', '').trim();
|
|
24
|
+
if (raw.length > 0) {
|
|
25
|
+
try {
|
|
26
|
+
const parsed = JSON.parse(raw);
|
|
27
|
+
const jsonKeys = readKeyListValue(parsed);
|
|
28
|
+
if (jsonKeys && jsonKeys.length > 0) {
|
|
29
|
+
return jsonKeys;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
catch {
|
|
33
|
+
// fall through to CSV parsing
|
|
34
|
+
}
|
|
35
|
+
const csvKeys = normalizeKeyList(raw
|
|
36
|
+
.split(',')
|
|
37
|
+
.map((value) => value.trim())
|
|
38
|
+
.filter((value) => value.length > 0));
|
|
39
|
+
if (csvKeys.length > 0) {
|
|
40
|
+
return csvKeys;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return [...DEFAULT_WORKER_MANIFEST_GLOBAL_KEYS];
|
|
44
|
+
};
|
|
45
|
+
export const getWorkerManifestEntries = () => {
|
|
46
|
+
const manifestGlobal = globalThis;
|
|
47
|
+
for (const key of readWorkerManifestGlobalKeys()) {
|
|
48
|
+
const value = manifestGlobal[key];
|
|
49
|
+
if (Array.isArray(value)) {
|
|
50
|
+
return value;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return [];
|
|
54
|
+
};
|
|
55
|
+
export const getWorkerManifestQueueNames = () => {
|
|
56
|
+
return Array.from(new Set(getWorkerManifestEntries()
|
|
57
|
+
.map((entry) => entry.queueName)
|
|
58
|
+
.filter((queueName) => typeof queueName === 'string')
|
|
59
|
+
.map((queueName) => queueName.trim())
|
|
60
|
+
.filter((queueName) => queueName.length > 0))).sort((left, right) => left.localeCompare(right));
|
|
61
|
+
};
|
|
62
|
+
export const getWorkerManifestRecord = (workerName) => {
|
|
63
|
+
return getWorkerManifestEntries().find((entry) => (entry.name ?? entry.workerName) === workerName);
|
|
64
|
+
};
|
|
65
|
+
export const getWorkerManifestStatus = (workerName) => {
|
|
66
|
+
return getWorkerManifestRecord(workerName)?.activeStatus === false;
|
|
67
|
+
};
|
|
@@ -1,12 +1,126 @@
|
|
|
1
|
-
import { Env, queueConfig } from '@zintrust/core/config';
|
|
1
|
+
import { Cloudflare, Env, queueConfig } from '@zintrust/core/config';
|
|
2
2
|
import { ErrorFactory } from '@zintrust/core/errors';
|
|
3
3
|
import { Logger } from '@zintrust/core/logger';
|
|
4
4
|
import { WorkerFactory } from '../WorkerFactory.js';
|
|
5
5
|
import { WorkerMetrics as WorkerMetricsManager } from '../WorkerMetrics.js';
|
|
6
6
|
import { WorkerRegistry } from '../WorkerRegistry.js';
|
|
7
7
|
import { maskInfrastructurePasswords } from '../helper/index.js';
|
|
8
|
+
import { getWorkerManifestEntries, getWorkerManifestQueueNames, getWorkerManifestRecord, } from './workerManifest.js';
|
|
8
9
|
const DEFAULT_PAGE_SIZE = 100;
|
|
9
10
|
const MAX_PAGE_SIZE = 200;
|
|
11
|
+
const shouldUseRedisRpcWorkerApi = () => Cloudflare.getWorkersEnv() !== null &&
|
|
12
|
+
Env.getBool('USE_REDIS_PROXY', false) &&
|
|
13
|
+
Env.get('REDIS_RPC_URL', '').trim().length > 0;
|
|
14
|
+
const createRequestId = () => {
|
|
15
|
+
const cryptoApi = globalThis.crypto;
|
|
16
|
+
if (typeof cryptoApi?.randomUUID === 'function')
|
|
17
|
+
return cryptoApi.randomUUID();
|
|
18
|
+
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
19
|
+
};
|
|
20
|
+
const callRedisRpc = async (service, method, payload = {}) => {
|
|
21
|
+
const baseUrl = Env.get('REDIS_RPC_URL', '').trim();
|
|
22
|
+
const endpoint = new URL('/rpc', baseUrl);
|
|
23
|
+
const secret = Env.get('REDIS_RPC_SECRET', Env.get('REDIS_PROXY_SECRET', Env.APP_KEY));
|
|
24
|
+
const response = await fetch(endpoint, {
|
|
25
|
+
method: 'POST',
|
|
26
|
+
headers: {
|
|
27
|
+
'content-type': 'application/json',
|
|
28
|
+
...(secret.trim() === '' ? {} : { 'x-redis-rpc-secret': secret }),
|
|
29
|
+
},
|
|
30
|
+
body: JSON.stringify({ requestId: createRequestId(), service, method, payload }),
|
|
31
|
+
});
|
|
32
|
+
const body = (await response.json().catch(() => ({})));
|
|
33
|
+
if (!response.ok || body.ok !== true) {
|
|
34
|
+
throw ErrorFactory.createTryCatchError(body.error?.message ?? `Redis RPC request failed with status ${response.status}`);
|
|
35
|
+
}
|
|
36
|
+
return body.result;
|
|
37
|
+
};
|
|
38
|
+
const manifestWorkers = () => transformToWorkerData(getWorkerManifestEntries()
|
|
39
|
+
.map((entry) => {
|
|
40
|
+
const name = entry.name ?? entry.workerName;
|
|
41
|
+
if (typeof name !== 'string' || name.trim() === '')
|
|
42
|
+
return null;
|
|
43
|
+
const autoStart = entry.autoStart ?? false;
|
|
44
|
+
let status;
|
|
45
|
+
if (entry.status) {
|
|
46
|
+
status = normalizeStatus(entry.status);
|
|
47
|
+
}
|
|
48
|
+
else if (autoStart) {
|
|
49
|
+
status = 'running';
|
|
50
|
+
}
|
|
51
|
+
else {
|
|
52
|
+
status = 'stopped';
|
|
53
|
+
}
|
|
54
|
+
const trimmedName = name.trim();
|
|
55
|
+
return {
|
|
56
|
+
name: trimmedName,
|
|
57
|
+
queueName: typeof entry.queueName === 'string' ? entry.queueName : `${trimmedName}-queue`,
|
|
58
|
+
status,
|
|
59
|
+
autoStart,
|
|
60
|
+
activeStatus: entry.activeStatus ?? true,
|
|
61
|
+
};
|
|
62
|
+
})
|
|
63
|
+
.filter((entry) => entry !== null), 'redis');
|
|
64
|
+
const mergeManifestWorkers = (backendWorkers, manifest = manifestWorkers()) => {
|
|
65
|
+
const manifestQueues = new Set(manifest.map((worker) => worker.queueName));
|
|
66
|
+
const byName = new Map();
|
|
67
|
+
for (const worker of manifest) {
|
|
68
|
+
byName.set(worker.name, worker);
|
|
69
|
+
}
|
|
70
|
+
for (const worker of backendWorkers) {
|
|
71
|
+
const isPlaceholder = worker.name === `${worker.queueName}:redis-rpc` && manifestQueues.has(worker.queueName);
|
|
72
|
+
if (isPlaceholder)
|
|
73
|
+
continue;
|
|
74
|
+
byName.set(worker.name, { ...byName.get(worker.name), ...worker });
|
|
75
|
+
}
|
|
76
|
+
return Array.from(byName.values()).sort((left, right) => left.name.localeCompare(right.name));
|
|
77
|
+
};
|
|
78
|
+
const redisRpcPersistence = async (limit) => {
|
|
79
|
+
const manifest = manifestWorkers();
|
|
80
|
+
try {
|
|
81
|
+
const records = await callRedisRpc('worker', 'list');
|
|
82
|
+
const backendWorkers = transformToWorkerData(records
|
|
83
|
+
.map((record) => {
|
|
84
|
+
const name = record.workerName ?? record.name;
|
|
85
|
+
if (typeof name !== 'string' || name.trim() === '')
|
|
86
|
+
return null;
|
|
87
|
+
return {
|
|
88
|
+
name: name.trim(),
|
|
89
|
+
queueName: record.queueName,
|
|
90
|
+
status: normalizeStatus(record.status ?? 'stopped'),
|
|
91
|
+
autoStart: record.autoStart ?? false,
|
|
92
|
+
details: {
|
|
93
|
+
configuration: {
|
|
94
|
+
processorSpec: record.processorSpec ?? null,
|
|
95
|
+
concurrency: record.concurrency ?? null,
|
|
96
|
+
},
|
|
97
|
+
health: {},
|
|
98
|
+
metrics: {},
|
|
99
|
+
recentLogs: [],
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
})
|
|
103
|
+
.filter((record) => record !== null), 'redis');
|
|
104
|
+
const workers = mergeManifestWorkers(backendWorkers, manifest);
|
|
105
|
+
return {
|
|
106
|
+
workers,
|
|
107
|
+
total: workers.length,
|
|
108
|
+
drivers: ['redis'],
|
|
109
|
+
effectiveLimit: limit,
|
|
110
|
+
prePaginated: false,
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
catch (error) {
|
|
114
|
+
Logger.debug('[getWorkers] Redis RPC persistence unavailable; returning manifest result', error);
|
|
115
|
+
return {
|
|
116
|
+
workers: manifest,
|
|
117
|
+
total: manifest.length,
|
|
118
|
+
drivers: ['redis'],
|
|
119
|
+
effectiveLimit: limit,
|
|
120
|
+
prePaginated: false,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
};
|
|
10
124
|
// Helper for timeout handling
|
|
11
125
|
async function withTimeout(promise, timeoutMs, errorMsg) {
|
|
12
126
|
let timer;
|
|
@@ -27,6 +141,9 @@ async function withTimeout(promise, timeoutMs, errorMsg) {
|
|
|
27
141
|
}
|
|
28
142
|
}
|
|
29
143
|
async function fetchPersistenceWithTimeout(page, limit, query) {
|
|
144
|
+
if (shouldUseRedisRpcWorkerApi()) {
|
|
145
|
+
return redisRpcPersistence(limit);
|
|
146
|
+
}
|
|
30
147
|
const driver = Env.get('WORKER_PERSISTENCE_DRIVER', 'memory');
|
|
31
148
|
try {
|
|
32
149
|
const result = await withTimeout(getWorkersFromPersistence(page, limit, query.driver, query), 5000, 'Persistence timeout');
|
|
@@ -320,6 +437,24 @@ const buildWorkerFromRecord = (record, driver) => {
|
|
|
320
437
|
};
|
|
321
438
|
return buildWorkerFromRaw(rawData, driver);
|
|
322
439
|
};
|
|
440
|
+
const resolveWorkerDetailsFromPersistence = async (name, driver) => {
|
|
441
|
+
if (driver === 'database') {
|
|
442
|
+
const dbRecord = await WorkerFactory.getPersisted(name, { driver: 'database' });
|
|
443
|
+
if (dbRecord)
|
|
444
|
+
return buildWorkerFromRecord(dbRecord, 'database');
|
|
445
|
+
const redisRecord = await WorkerFactory.getPersisted(name, { driver: 'redis' });
|
|
446
|
+
if (redisRecord)
|
|
447
|
+
return buildWorkerFromRecord(redisRecord, 'redis');
|
|
448
|
+
return undefined;
|
|
449
|
+
}
|
|
450
|
+
const record = await WorkerFactory.getPersisted(name, { driver });
|
|
451
|
+
if (record)
|
|
452
|
+
return buildWorkerFromRecord(record, driver);
|
|
453
|
+
const fileBacked = await WorkerFactory.getFileBackedRecord(name);
|
|
454
|
+
if (fileBacked)
|
|
455
|
+
return buildWorkerFromRecord(fileBacked, 'memory');
|
|
456
|
+
return undefined;
|
|
457
|
+
};
|
|
323
458
|
const buildWorkerFromRaw = (workerData, driver) => {
|
|
324
459
|
const status = normalizeStatus(workerData.status ?? 'stopped');
|
|
325
460
|
return {
|
|
@@ -474,6 +609,10 @@ async function getRedisQueueData() {
|
|
|
474
609
|
}
|
|
475
610
|
const monitor = QueueMonitor.create({
|
|
476
611
|
knownQueues: async () => {
|
|
612
|
+
const manifestQueues = getWorkerManifestQueueNames();
|
|
613
|
+
if (shouldUseRedisRpcWorkerApi() && manifestQueues.length > 0) {
|
|
614
|
+
return manifestQueues;
|
|
615
|
+
}
|
|
477
616
|
const records = await WorkerFactory.listPersistedRecords();
|
|
478
617
|
return Array.from(new Set(records
|
|
479
618
|
.map((record) => record.queueName)
|
|
@@ -509,7 +648,12 @@ async function getRedisQueueData() {
|
|
|
509
648
|
};
|
|
510
649
|
}
|
|
511
650
|
catch (error) {
|
|
512
|
-
|
|
651
|
+
if (shouldUseRedisRpcWorkerApi()) {
|
|
652
|
+
Logger.debug('Error fetching Redis queue data:', error);
|
|
653
|
+
}
|
|
654
|
+
else {
|
|
655
|
+
Logger.error('Error fetching Redis queue data:', error);
|
|
656
|
+
}
|
|
513
657
|
return {
|
|
514
658
|
driver: 'redis',
|
|
515
659
|
totalQueues: 0,
|
|
@@ -854,38 +998,37 @@ const getLiveWorkerMetricsByName = () => {
|
|
|
854
998
|
}
|
|
855
999
|
return liveMetrics;
|
|
856
1000
|
};
|
|
1001
|
+
const buildRedisRpcManifestWorker = (name, manifestRecord) => {
|
|
1002
|
+
if (!manifestRecord)
|
|
1003
|
+
return undefined;
|
|
1004
|
+
const trimmedName = (manifestRecord.name ?? manifestRecord.workerName ?? name).trim();
|
|
1005
|
+
return buildWorkerFromRaw({
|
|
1006
|
+
name: trimmedName,
|
|
1007
|
+
queueName: typeof manifestRecord.queueName === 'string' && manifestRecord.queueName.trim().length > 0
|
|
1008
|
+
? manifestRecord.queueName.trim()
|
|
1009
|
+
: `${trimmedName}-queue`,
|
|
1010
|
+
status: normalizeStatus(manifestRecord.status ?? 'stopped'),
|
|
1011
|
+
autoStart: manifestRecord.autoStart ?? false,
|
|
1012
|
+
activeStatus: manifestRecord.activeStatus ?? true,
|
|
1013
|
+
}, 'redis');
|
|
1014
|
+
};
|
|
1015
|
+
const resolveRedisRpcWorkerDetails = async (name) => {
|
|
1016
|
+
const persistence = await redisRpcPersistence(5000);
|
|
1017
|
+
const persistedWorker = persistence.workers.find((candidate) => candidate.name === name);
|
|
1018
|
+
if (persistedWorker)
|
|
1019
|
+
return persistedWorker;
|
|
1020
|
+
return buildRedisRpcManifestWorker(name, getWorkerManifestRecord(name));
|
|
1021
|
+
};
|
|
857
1022
|
export async function toggleAutoStart(name, enabled) {
|
|
858
1023
|
await WorkerFactory.setAutoStart(name, enabled);
|
|
859
1024
|
}
|
|
860
1025
|
export async function getWorkerDetails(name, driver) {
|
|
861
|
-
const persistenceDriver = (driver ||
|
|
862
|
-
const
|
|
863
|
-
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
worker = buildWorkerFromRecord(dbRecord, 'database');
|
|
868
|
-
}
|
|
869
|
-
else {
|
|
870
|
-
const redisRecord = await WorkerFactory.getPersisted(name, { driver: 'redis' });
|
|
871
|
-
if (redisRecord) {
|
|
872
|
-
worker = buildWorkerFromRecord(redisRecord, 'redis');
|
|
873
|
-
}
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
else {
|
|
877
|
-
const normalizedDriver = normalizeDriver(persistenceDriver);
|
|
878
|
-
const record = await WorkerFactory.getPersisted(name, { driver: normalizedDriver });
|
|
879
|
-
if (record) {
|
|
880
|
-
worker = buildWorkerFromRecord(record, normalizedDriver);
|
|
881
|
-
}
|
|
882
|
-
}
|
|
883
|
-
if (!worker) {
|
|
884
|
-
const fileBacked = await WorkerFactory.getFileBackedRecord(name);
|
|
885
|
-
if (fileBacked) {
|
|
886
|
-
worker = buildWorkerFromRecord(fileBacked, 'memory');
|
|
887
|
-
}
|
|
888
|
-
}
|
|
1026
|
+
const persistenceDriver = (driver || Env.get('WORKER_PERSISTENCE_DRIVER', 'memory')) ?? 'memory';
|
|
1027
|
+
const normalizedDriver = normalizeDriver(String(persistenceDriver).toLowerCase());
|
|
1028
|
+
const fromRedisRpc = shouldUseRedisRpcWorkerApi() && (driver === undefined || normalizedDriver === 'redis');
|
|
1029
|
+
const worker = fromRedisRpc
|
|
1030
|
+
? await resolveRedisRpcWorkerDetails(name)
|
|
1031
|
+
: await resolveWorkerDetailsFromPersistence(name, normalizedDriver);
|
|
889
1032
|
if (!worker) {
|
|
890
1033
|
throw ErrorFactory.createWorkerError(`Worker ${name} not found`);
|
|
891
1034
|
}
|