@zintrust/workers 2.4.3 → 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 +64 -47
- package/dist/http/WorkerController.js +192 -7
- package/dist/index.d.ts +1 -1
- 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 +86 -74
- package/src/http/WorkerController.ts +257 -21
- package/src/index.ts +8 -1
|
@@ -5,6 +5,7 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { Cloudflare } from '@zintrust/core/cloudflare';
|
|
7
7
|
import { Env } from '@zintrust/core/config';
|
|
8
|
+
import { ErrorFactory } from '@zintrust/core/errors';
|
|
8
9
|
import { getValidatedBody } from '@zintrust/core/http';
|
|
9
10
|
import { Logger } from '@zintrust/core/logger';
|
|
10
11
|
import { CanaryController } from '../CanaryController.js';
|
|
@@ -28,6 +29,115 @@ const getBody = (req) => {
|
|
|
28
29
|
};
|
|
29
30
|
// ==================== Core Worker Operations ====================
|
|
30
31
|
const isCloudflareEnv = () => Cloudflare.getWorkersEnv() !== null;
|
|
32
|
+
const shouldUseRedisRpcWorkerLifecycle = (persistenceOverride) => {
|
|
33
|
+
if (!isCloudflareEnv())
|
|
34
|
+
return false;
|
|
35
|
+
if (persistenceOverride?.driver !== 'redis')
|
|
36
|
+
return false;
|
|
37
|
+
return Env.getBool('USE_REDIS_PROXY', false) && Env.get('REDIS_RPC_URL', '').trim().length > 0;
|
|
38
|
+
};
|
|
39
|
+
const createRequestId = () => {
|
|
40
|
+
const cryptoApi = globalThis.crypto;
|
|
41
|
+
if (typeof cryptoApi?.randomUUID === 'function')
|
|
42
|
+
return cryptoApi.randomUUID();
|
|
43
|
+
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
44
|
+
};
|
|
45
|
+
const callRedisRpc = async (service, method, payload = {}) => {
|
|
46
|
+
const baseUrl = Env.get('REDIS_RPC_URL', '').trim();
|
|
47
|
+
const endpoint = new URL('/rpc', baseUrl);
|
|
48
|
+
const secret = Env.get('REDIS_RPC_SECRET', Env.get('REDIS_PROXY_SECRET', Env.APP_KEY));
|
|
49
|
+
const response = await fetch(endpoint, {
|
|
50
|
+
method: 'POST',
|
|
51
|
+
headers: {
|
|
52
|
+
'content-type': 'application/json',
|
|
53
|
+
...(secret.trim() === '' ? {} : { 'x-redis-rpc-secret': secret }),
|
|
54
|
+
},
|
|
55
|
+
body: JSON.stringify({ requestId: createRequestId(), service, method, payload }),
|
|
56
|
+
});
|
|
57
|
+
const body = (await response.json().catch(() => ({})));
|
|
58
|
+
if (!response.ok || body.ok !== true) {
|
|
59
|
+
throw ErrorFactory.createTryCatchError(body.error?.message ?? `Redis RPC request failed with status ${response.status}`);
|
|
60
|
+
}
|
|
61
|
+
return body.result;
|
|
62
|
+
};
|
|
63
|
+
const DEFAULT_WORKER_MANIFEST_GLOBAL_KEYS = Object.freeze([
|
|
64
|
+
'__zintrustWorkersManifest',
|
|
65
|
+
'__zintrustWorkerManifest',
|
|
66
|
+
'__zintrustAppWorkerManifest',
|
|
67
|
+
]);
|
|
68
|
+
const readWorkerManifestGlobalKeys = () => {
|
|
69
|
+
const manifestGlobal = globalThis;
|
|
70
|
+
const fromGlobal = manifestGlobal.__zintrustWorkerManifestGlobalKeys;
|
|
71
|
+
if (Array.isArray(fromGlobal) && fromGlobal.every((key) => typeof key === 'string')) {
|
|
72
|
+
return Array.from(new Set(fromGlobal.map((key) => key.trim()).filter(Boolean)));
|
|
73
|
+
}
|
|
74
|
+
const raw = Env.get('WORKER_MANIFEST_GLOBAL_KEYS', '').trim();
|
|
75
|
+
if (raw.length > 0) {
|
|
76
|
+
try {
|
|
77
|
+
const parsed = JSON.parse(raw);
|
|
78
|
+
if (Array.isArray(parsed)) {
|
|
79
|
+
return Array.from(new Set(parsed.map((key) => String(key).trim()).filter((key) => key.length > 0)));
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
catch {
|
|
83
|
+
// fall through
|
|
84
|
+
}
|
|
85
|
+
const csv = raw
|
|
86
|
+
.split(',')
|
|
87
|
+
.map((key) => key.trim())
|
|
88
|
+
.filter(Boolean);
|
|
89
|
+
if (csv.length > 0)
|
|
90
|
+
return Array.from(new Set(csv));
|
|
91
|
+
}
|
|
92
|
+
return [...DEFAULT_WORKER_MANIFEST_GLOBAL_KEYS];
|
|
93
|
+
};
|
|
94
|
+
const resolveManifestWorker = (workerName) => {
|
|
95
|
+
const manifestGlobal = globalThis;
|
|
96
|
+
const keys = readWorkerManifestGlobalKeys();
|
|
97
|
+
for (const key of keys) {
|
|
98
|
+
const value = manifestGlobal[key];
|
|
99
|
+
if (!Array.isArray(value))
|
|
100
|
+
continue;
|
|
101
|
+
const entries = value;
|
|
102
|
+
const match = entries.find((entry) => (entry.name ?? entry.workerName) === workerName);
|
|
103
|
+
if (!match)
|
|
104
|
+
continue;
|
|
105
|
+
const queueName = typeof match.queueName === 'string' && match.queueName.trim().length > 0
|
|
106
|
+
? match.queueName.trim()
|
|
107
|
+
: `${workerName}-queue`;
|
|
108
|
+
const processorSpec = typeof match.processorSpec === 'string' && match.processorSpec.trim().length > 0
|
|
109
|
+
? match.processorSpec.trim()
|
|
110
|
+
: null;
|
|
111
|
+
const concurrency = Math.max(1, Number(match.concurrency ?? 1) || 1);
|
|
112
|
+
return { workerName, queueName, processorSpec, concurrency };
|
|
113
|
+
}
|
|
114
|
+
return null;
|
|
115
|
+
};
|
|
116
|
+
const isManifestWorkerInactive = (workerName) => {
|
|
117
|
+
const manifestGlobal = globalThis;
|
|
118
|
+
const keys = readWorkerManifestGlobalKeys();
|
|
119
|
+
for (const key of keys) {
|
|
120
|
+
const value = manifestGlobal[key];
|
|
121
|
+
if (!Array.isArray(value))
|
|
122
|
+
continue;
|
|
123
|
+
const entries = value;
|
|
124
|
+
const match = entries.find((entry) => (entry.name ?? entry.workerName) === workerName);
|
|
125
|
+
if (match?.activeStatus === false)
|
|
126
|
+
return true;
|
|
127
|
+
}
|
|
128
|
+
return false;
|
|
129
|
+
};
|
|
130
|
+
const assertSlaAllowsWorkerRequest = async (workerName) => {
|
|
131
|
+
const gate = await SLAMonitor.canSendWorkerRequest(workerName);
|
|
132
|
+
if (gate.allowed)
|
|
133
|
+
return;
|
|
134
|
+
throw ErrorFactory.createConfigError(`Worker "${workerName}" blocked because SLA status is not compliant`, {
|
|
135
|
+
kind: 'worker_sla_blocked',
|
|
136
|
+
workerName,
|
|
137
|
+
reason: gate.reason,
|
|
138
|
+
status: gate.status?.status,
|
|
139
|
+
});
|
|
140
|
+
};
|
|
31
141
|
const validateCreatePayload = (body, res) => {
|
|
32
142
|
if (!body.name || !body.queueName || !body.processor || !body.version) {
|
|
33
143
|
res.setStatus(400).json({
|
|
@@ -39,6 +149,23 @@ const validateCreatePayload = (body, res) => {
|
|
|
39
149
|
}
|
|
40
150
|
return true;
|
|
41
151
|
};
|
|
152
|
+
const isSlaBlockedError = (error) => {
|
|
153
|
+
const details = error?.details;
|
|
154
|
+
if (details?.kind === 'worker_sla_blocked')
|
|
155
|
+
return true;
|
|
156
|
+
return error instanceof Error && error.message.includes('SLA status is not compliant');
|
|
157
|
+
};
|
|
158
|
+
const respondWorkerOperationError = (res, error, fallbackStatus = 500) => {
|
|
159
|
+
if (isSlaBlockedError(error)) {
|
|
160
|
+
res.setStatus(409).json({
|
|
161
|
+
ok: false,
|
|
162
|
+
error: error.message,
|
|
163
|
+
code: 'WORKER_SLA_BLOCKED',
|
|
164
|
+
});
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
res.setStatus(fallbackStatus).json({ error: error.message });
|
|
168
|
+
};
|
|
42
169
|
const respondIfWorkerExists = async (name, persistenceOverride, res) => {
|
|
43
170
|
const existing = await WorkerFactory.getPersisted(name, persistenceOverride);
|
|
44
171
|
if (!existing)
|
|
@@ -140,7 +267,7 @@ async function create(req, res) {
|
|
|
140
267
|
}
|
|
141
268
|
catch (error) {
|
|
142
269
|
Logger.error('WorkerController.create failed', error);
|
|
143
|
-
res
|
|
270
|
+
respondWorkerOperationError(res, error);
|
|
144
271
|
}
|
|
145
272
|
}
|
|
146
273
|
/**
|
|
@@ -159,6 +286,21 @@ async function start(req, res) {
|
|
|
159
286
|
const isActive = await ensureActiveWorker(name, persistenceOverride, res);
|
|
160
287
|
if (!isActive)
|
|
161
288
|
return;
|
|
289
|
+
if (shouldUseRedisRpcWorkerLifecycle(persistenceOverride)) {
|
|
290
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
291
|
+
const manifest = resolveManifestWorker(name);
|
|
292
|
+
if (!manifest) {
|
|
293
|
+
res.setStatus(404).json({ ok: false, error: `Worker ${name} not found in manifest` });
|
|
294
|
+
return;
|
|
295
|
+
}
|
|
296
|
+
const record = await callRedisRpc('worker', 'startAppWorker', manifest);
|
|
297
|
+
res.json({
|
|
298
|
+
ok: true,
|
|
299
|
+
message: `Worker ${name} started (redis-rpc registry updated)`,
|
|
300
|
+
record,
|
|
301
|
+
});
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
162
304
|
const registered = WorkerRegistry.list().includes(name);
|
|
163
305
|
if (!registered) {
|
|
164
306
|
await WorkerFactory.startFromPersisted(name, persistenceOverride);
|
|
@@ -170,7 +312,7 @@ async function start(req, res) {
|
|
|
170
312
|
}
|
|
171
313
|
catch (error) {
|
|
172
314
|
Logger.error('WorkerController.start failed', error);
|
|
173
|
-
res
|
|
315
|
+
respondWorkerOperationError(res, error);
|
|
174
316
|
}
|
|
175
317
|
}
|
|
176
318
|
/**
|
|
@@ -185,12 +327,32 @@ async function stop(req, res) {
|
|
|
185
327
|
const isActive = await ensureActiveWorker(name, persistenceOverride, res);
|
|
186
328
|
if (!isActive)
|
|
187
329
|
return;
|
|
330
|
+
if (name && shouldUseRedisRpcWorkerLifecycle(persistenceOverride)) {
|
|
331
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
332
|
+
// The registry stop operation is idempotent, but may return false if the worker
|
|
333
|
+
// has not been registered yet. In that case, register then stop to ensure the
|
|
334
|
+
// "stopped" state persists after a single click.
|
|
335
|
+
let recordOrFalse = await callRedisRpc('worker', 'stopWorker', { workerName: name });
|
|
336
|
+
if (recordOrFalse === false) {
|
|
337
|
+
const manifest = resolveManifestWorker(name);
|
|
338
|
+
if (manifest) {
|
|
339
|
+
await callRedisRpc('worker', 'startAppWorker', manifest);
|
|
340
|
+
}
|
|
341
|
+
recordOrFalse = await callRedisRpc('worker', 'stopWorker', { workerName: name });
|
|
342
|
+
}
|
|
343
|
+
res.json({
|
|
344
|
+
ok: true,
|
|
345
|
+
message: `Worker ${name} stopped (redis-rpc registry updated)`,
|
|
346
|
+
result: recordOrFalse,
|
|
347
|
+
});
|
|
348
|
+
return;
|
|
349
|
+
}
|
|
188
350
|
await WorkerFactory.stop(name, persistenceOverride);
|
|
189
351
|
res.json({ ok: true, message: `Worker ${name} stopped` });
|
|
190
352
|
}
|
|
191
353
|
catch (error) {
|
|
192
354
|
Logger.error('WorkerController.stop failed', error);
|
|
193
|
-
res
|
|
355
|
+
respondWorkerOperationError(res, error);
|
|
194
356
|
}
|
|
195
357
|
}
|
|
196
358
|
/**
|
|
@@ -205,12 +367,27 @@ async function restart(req, res) {
|
|
|
205
367
|
const isActive = await ensureActiveWorker(name, persistenceOverride, res);
|
|
206
368
|
if (!isActive)
|
|
207
369
|
return;
|
|
370
|
+
if (name && shouldUseRedisRpcWorkerLifecycle(persistenceOverride)) {
|
|
371
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
372
|
+
const manifest = resolveManifestWorker(name);
|
|
373
|
+
if (!manifest) {
|
|
374
|
+
res.setStatus(404).json({ ok: false, error: `Worker ${name} not found in manifest` });
|
|
375
|
+
return;
|
|
376
|
+
}
|
|
377
|
+
const record = await callRedisRpc('worker', 'restartAppWorker', manifest);
|
|
378
|
+
res.json({
|
|
379
|
+
ok: true,
|
|
380
|
+
message: `Worker ${name} restarted (redis-rpc registry updated)`,
|
|
381
|
+
record,
|
|
382
|
+
});
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
208
385
|
await WorkerFactory.restart(name, persistenceOverride);
|
|
209
386
|
res.json({ ok: true, message: `Worker ${name} restarted` });
|
|
210
387
|
}
|
|
211
388
|
catch (error) {
|
|
212
389
|
Logger.error('WorkerController.restart failed', error);
|
|
213
|
-
res
|
|
390
|
+
respondWorkerOperationError(res, error);
|
|
214
391
|
}
|
|
215
392
|
}
|
|
216
393
|
/**
|
|
@@ -354,11 +531,19 @@ const ensureActiveWorker = async (name, persistenceOverride, res) => {
|
|
|
354
531
|
res.setStatus(410).json({ error: 'Worker is inactive', code: 'WORKER_INACTIVE' });
|
|
355
532
|
return false;
|
|
356
533
|
}
|
|
357
|
-
|
|
358
|
-
if
|
|
534
|
+
// In Cloudflare Redis-RPC mode we cannot rely on direct persistence reads. Use the app manifest
|
|
535
|
+
// to determine if the worker is intentionally inactive.
|
|
536
|
+
if (shouldUseRedisRpcWorkerLifecycle(persistenceOverride) && isManifestWorkerInactive(name)) {
|
|
359
537
|
res.setStatus(410).json({ error: 'Worker is inactive', code: 'WORKER_INACTIVE' });
|
|
360
538
|
return false;
|
|
361
539
|
}
|
|
540
|
+
if (!shouldUseRedisRpcWorkerLifecycle(persistenceOverride)) {
|
|
541
|
+
const persisted = await WorkerFactory.getPersisted(name, persistenceOverride);
|
|
542
|
+
if (persisted?.activeStatus === false) {
|
|
543
|
+
res.setStatus(410).json({ error: 'Worker is inactive', code: 'WORKER_INACTIVE' });
|
|
544
|
+
return false;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
362
547
|
return true;
|
|
363
548
|
};
|
|
364
549
|
/**
|
|
@@ -639,7 +824,7 @@ async function getSlaStatus(req, res) {
|
|
|
639
824
|
try {
|
|
640
825
|
const name = getParam(req, 'name');
|
|
641
826
|
const slaStatus = await SLAMonitor.checkCompliance(name);
|
|
642
|
-
res.json({ ok: true, status: slaStatus });
|
|
827
|
+
res.json({ ok: true, allowed: slaStatus.status === 'compliant', status: slaStatus });
|
|
643
828
|
}
|
|
644
829
|
catch (error) {
|
|
645
830
|
Logger.error('WorkerController.getSlaStatus failed', error);
|
package/dist/index.d.ts
CHANGED
|
@@ -37,7 +37,7 @@ export type { RedisConfig, WorkerAutoScalingConfig, WorkerComplianceConfig, Work
|
|
|
37
37
|
export type { Job, Worker, WorkerOptions } from 'bullmq';
|
|
38
38
|
export type { IAnomaly, IAnomalyConfig, IForecast, IMetric, IPrediction, IRecommendation, IRootCauseAnalysis, } from './AnomalyDetection';
|
|
39
39
|
export type { IChaosComparison, IChaosExperiment, IChaosReport, IChaosStatus, } from './ChaosEngineering';
|
|
40
|
-
export type { ISLAConfig, ISLAReport, ISLAStatus, ISLAViolation, ITimeRange } from './SLAMonitor';
|
|
40
|
+
export type { ISLAConfig, ISLAGate, ISLAReport, ISLAStatus, ISLAViolation, ITimeRange, } from './SLAMonitor';
|
|
41
41
|
export type * from './config/workerConfig';
|
|
42
42
|
export type * from './type';
|
|
43
43
|
/**
|
package/package.json
CHANGED
package/src/SLAMonitor.ts
CHANGED
|
@@ -74,6 +74,12 @@ export type ISLAReport = {
|
|
|
74
74
|
}>;
|
|
75
75
|
};
|
|
76
76
|
|
|
77
|
+
export type ISLAGate = {
|
|
78
|
+
allowed: boolean;
|
|
79
|
+
reason?: string;
|
|
80
|
+
status?: ISLAStatus;
|
|
81
|
+
};
|
|
82
|
+
|
|
77
83
|
const slaConfigs = new Map<string, ISLAConfig>();
|
|
78
84
|
const violationHistory = new Map<string, ISLAViolation[]>();
|
|
79
85
|
const lastAlertAt = new Map<string, Date>();
|
|
@@ -291,6 +297,10 @@ export const SLAMonitor = Object.freeze({
|
|
|
291
297
|
Logger.info(`SLA defined for worker "${config.workerName}"`);
|
|
292
298
|
},
|
|
293
299
|
|
|
300
|
+
hasSLA(workerName: string): boolean {
|
|
301
|
+
return slaConfigs.has(workerName);
|
|
302
|
+
},
|
|
303
|
+
|
|
294
304
|
/**
|
|
295
305
|
* Check SLA compliance for a worker
|
|
296
306
|
*/
|
|
@@ -332,6 +342,23 @@ export const SLAMonitor = Object.freeze({
|
|
|
332
342
|
};
|
|
333
343
|
},
|
|
334
344
|
|
|
345
|
+
async canSendWorkerRequest(workerName: string): Promise<ISLAGate> {
|
|
346
|
+
if (!slaConfigs.has(workerName)) {
|
|
347
|
+
return { allowed: true, reason: 'sla_not_configured' };
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
const status = await SLAMonitor.checkCompliance(workerName);
|
|
351
|
+
if (status.status === 'compliant') {
|
|
352
|
+
return { allowed: true, status };
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
return {
|
|
356
|
+
allowed: false,
|
|
357
|
+
reason: `sla_${status.status}`,
|
|
358
|
+
status,
|
|
359
|
+
};
|
|
360
|
+
},
|
|
361
|
+
|
|
335
362
|
/**
|
|
336
363
|
* Get SLA violations for a worker
|
|
337
364
|
*/
|
package/src/WorkerFactory.ts
CHANGED
|
@@ -38,6 +38,7 @@ import { Observability, type ObservabilityConfig } from './Observability';
|
|
|
38
38
|
import { PluginManager } from './PluginManager';
|
|
39
39
|
import { PriorityQueue } from './PriorityQueue';
|
|
40
40
|
import { ResourceMonitor } from './ResourceMonitor';
|
|
41
|
+
import { SLAMonitor } from './SLAMonitor';
|
|
41
42
|
import { WorkerMetrics } from './WorkerMetrics';
|
|
42
43
|
import { WorkerRegistry, type WorkerInstance as RegistryWorkerInstance } from './WorkerRegistry';
|
|
43
44
|
import { WorkerVersioning } from './WorkerVersioning';
|
|
@@ -2558,6 +2559,21 @@ const buildWorkerRecord = (config: WorkerFactoryConfig, status: string): WorkerR
|
|
|
2558
2559
|
};
|
|
2559
2560
|
};
|
|
2560
2561
|
|
|
2562
|
+
const assertSlaAllowsWorkerRequest = async (workerName: string): Promise<void> => {
|
|
2563
|
+
const gate = await SLAMonitor.canSendWorkerRequest(workerName);
|
|
2564
|
+
if (gate.allowed) return;
|
|
2565
|
+
|
|
2566
|
+
throw ErrorFactory.createConfigError(
|
|
2567
|
+
`Worker "${workerName}" blocked because SLA status is not compliant`,
|
|
2568
|
+
{
|
|
2569
|
+
kind: 'worker_sla_blocked',
|
|
2570
|
+
workerName,
|
|
2571
|
+
reason: gate.reason,
|
|
2572
|
+
status: gate.status?.status,
|
|
2573
|
+
}
|
|
2574
|
+
);
|
|
2575
|
+
};
|
|
2576
|
+
|
|
2561
2577
|
const buildDefaultAutoScalerConfig = (): AutoScalerConfig => ({
|
|
2562
2578
|
enabled: workersConfig.autoScaling.enabled,
|
|
2563
2579
|
checkInterval: workersConfig.autoScaling.interval,
|
|
@@ -3022,6 +3038,10 @@ export const WorkerFactory = Object.freeze({
|
|
|
3022
3038
|
throw ErrorFactory.createWorkerError(`Worker "${name}" already exists`);
|
|
3023
3039
|
}
|
|
3024
3040
|
|
|
3041
|
+
if (autoStart) {
|
|
3042
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
3043
|
+
}
|
|
3044
|
+
|
|
3025
3045
|
// Resolve the correct store for this worker configuration
|
|
3026
3046
|
const store = await getStoreForWorker(config);
|
|
3027
3047
|
|
|
@@ -3439,6 +3459,8 @@ export const WorkerFactory = Object.freeze({
|
|
|
3439
3459
|
throw ErrorFactory.createConfigError(`Worker "${name}" is inactive`);
|
|
3440
3460
|
}
|
|
3441
3461
|
|
|
3462
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
3463
|
+
|
|
3442
3464
|
const version = instance.config.version ?? '1.0.0';
|
|
3443
3465
|
await WorkerRegistry.start(name, version);
|
|
3444
3466
|
|
|
@@ -3513,6 +3535,8 @@ export const WorkerFactory = Object.freeze({
|
|
|
3513
3535
|
throw ErrorFactory.createConfigError(`Worker "${name}" is inactive`);
|
|
3514
3536
|
}
|
|
3515
3537
|
|
|
3538
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
3539
|
+
|
|
3516
3540
|
let processor: WorkerFactoryConfig['processor'];
|
|
3517
3541
|
try {
|
|
3518
3542
|
processor = await resolveStartFromPersistedProcessor(name, record, discovered);
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
import { Env } from '@zintrust/core/config';
|
|
2
|
+
|
|
3
|
+
export type WorkerManifestEntry = Readonly<{
|
|
4
|
+
name?: string;
|
|
5
|
+
workerName?: string;
|
|
6
|
+
queueName?: string;
|
|
7
|
+
processorSpec?: string | null;
|
|
8
|
+
concurrency?: number;
|
|
9
|
+
autoStart?: boolean;
|
|
10
|
+
activeStatus?: boolean;
|
|
11
|
+
status?: string;
|
|
12
|
+
}>;
|
|
13
|
+
|
|
14
|
+
type WorkersManifestGlobal = typeof globalThis & {
|
|
15
|
+
workerManifestGlobalKeys?: ReadonlyArray<string>;
|
|
16
|
+
__zintrustWorkerManifestGlobalKeys?: ReadonlyArray<string>;
|
|
17
|
+
__zintrustWorkersManifest?: ReadonlyArray<WorkerManifestEntry>;
|
|
18
|
+
__zintrustWorkerManifest?: ReadonlyArray<WorkerManifestEntry>;
|
|
19
|
+
__zintrustAppWorkerManifest?: ReadonlyArray<WorkerManifestEntry>;
|
|
20
|
+
[key: string]: unknown;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
const DEFAULT_WORKER_MANIFEST_GLOBAL_KEYS = Object.freeze([
|
|
24
|
+
'__zintrustWorkersManifest',
|
|
25
|
+
'__zintrustWorkerManifest',
|
|
26
|
+
'__zintrustAppWorkerManifest',
|
|
27
|
+
] as const);
|
|
28
|
+
|
|
29
|
+
const normalizeKeyList = (values: ReadonlyArray<string>): string[] => {
|
|
30
|
+
return Array.from(
|
|
31
|
+
new Set(values.map((value) => value.trim()).filter((value) => value.length > 0))
|
|
32
|
+
);
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
const readKeyListValue = (value: unknown): string[] | undefined => {
|
|
36
|
+
if (!Array.isArray(value) || !value.every((item) => typeof item === 'string')) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return normalizeKeyList(value);
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
export const readWorkerManifestGlobalKeys = (): string[] => {
|
|
44
|
+
const manifestGlobal = globalThis as WorkersManifestGlobal;
|
|
45
|
+
|
|
46
|
+
const runtimeKeys =
|
|
47
|
+
readKeyListValue(manifestGlobal.workerManifestGlobalKeys) ??
|
|
48
|
+
readKeyListValue(manifestGlobal.__zintrustWorkerManifestGlobalKeys);
|
|
49
|
+
|
|
50
|
+
if (runtimeKeys && runtimeKeys.length > 0) {
|
|
51
|
+
return runtimeKeys;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const raw = Env.get('WORKER_MANIFEST_GLOBAL_KEYS', '').trim();
|
|
55
|
+
if (raw.length > 0) {
|
|
56
|
+
try {
|
|
57
|
+
const parsed = JSON.parse(raw) as unknown;
|
|
58
|
+
const jsonKeys = readKeyListValue(parsed);
|
|
59
|
+
if (jsonKeys && jsonKeys.length > 0) {
|
|
60
|
+
return jsonKeys;
|
|
61
|
+
}
|
|
62
|
+
} catch {
|
|
63
|
+
// fall through to CSV parsing
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
const csvKeys = normalizeKeyList(
|
|
67
|
+
raw
|
|
68
|
+
.split(',')
|
|
69
|
+
.map((value) => value.trim())
|
|
70
|
+
.filter((value) => value.length > 0)
|
|
71
|
+
);
|
|
72
|
+
if (csvKeys.length > 0) {
|
|
73
|
+
return csvKeys;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return [...DEFAULT_WORKER_MANIFEST_GLOBAL_KEYS];
|
|
78
|
+
};
|
|
79
|
+
|
|
80
|
+
export const getWorkerManifestEntries = (): WorkerManifestEntry[] => {
|
|
81
|
+
const manifestGlobal = globalThis as WorkersManifestGlobal;
|
|
82
|
+
for (const key of readWorkerManifestGlobalKeys()) {
|
|
83
|
+
const value = manifestGlobal[key];
|
|
84
|
+
if (Array.isArray(value)) {
|
|
85
|
+
return value as WorkerManifestEntry[];
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
return [];
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
export const getWorkerManifestQueueNames = (): string[] => {
|
|
93
|
+
return Array.from(
|
|
94
|
+
new Set(
|
|
95
|
+
getWorkerManifestEntries()
|
|
96
|
+
.map((entry) => entry.queueName)
|
|
97
|
+
.filter((queueName): queueName is string => typeof queueName === 'string')
|
|
98
|
+
.map((queueName) => queueName.trim())
|
|
99
|
+
.filter((queueName) => queueName.length > 0)
|
|
100
|
+
)
|
|
101
|
+
).sort((left, right) => left.localeCompare(right));
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
export const getWorkerManifestRecord = (workerName: string): WorkerManifestEntry | undefined => {
|
|
105
|
+
return getWorkerManifestEntries().find(
|
|
106
|
+
(entry) => (entry.name ?? entry.workerName) === workerName
|
|
107
|
+
);
|
|
108
|
+
};
|
|
109
|
+
|
|
110
|
+
export const getWorkerManifestStatus = (workerName: string): boolean => {
|
|
111
|
+
return getWorkerManifestRecord(workerName)?.activeStatus === false;
|
|
112
|
+
};
|