@zintrust/workers 2.4.3 → 2.4.5
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/MultiQueueWorker.d.ts +1 -1
- package/dist/MultiQueueWorker.js +8 -2
- package/dist/Observability.js +10 -3
- package/dist/SLAMonitor.d.ts +8 -0
- package/dist/SLAMonitor.js +20 -0
- package/dist/WorkerFactory.d.ts +1 -1
- package/dist/WorkerFactory.js +25 -2
- package/dist/dashboard/types.d.ts +3 -0
- package/dist/dashboard/workerManifest.d.ts +15 -0
- package/dist/dashboard/workerManifest.js +67 -0
- package/dist/dashboard/workers-api.js +68 -47
- package/dist/http/WorkerController.js +203 -9
- package/dist/index.d.ts +1 -1
- package/dist/ui/router/EmbeddedAssets.d.ts +1 -1
- package/dist/ui/router/EmbeddedAssets.js +1 -1
- package/package.json +1 -1
- package/src/MultiQueueWorker.ts +10 -2
- package/src/Observability.ts +9 -3
- package/src/SLAMonitor.ts +31 -0
- package/src/WorkerFactory.ts +34 -2
- package/src/dashboard/types.ts +3 -0
- package/src/dashboard/workerManifest.ts +112 -0
- package/src/dashboard/workers-api.ts +90 -74
- package/src/http/WorkerController.ts +275 -34
- package/src/index.ts +8 -1
- package/src/ui/router/EmbeddedAssets.ts +1 -1
- package/src/ui/workers/main.js +56 -39
- package/dist/build-manifest.json +0 -550
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
|
|
7
7
|
import { Cloudflare } from '@zintrust/core/cloudflare';
|
|
8
8
|
import { Env } from '@zintrust/core/config';
|
|
9
|
+
import { ErrorFactory } from '@zintrust/core/errors';
|
|
9
10
|
import type { IRequest, IResponse } from '@zintrust/core/http';
|
|
10
11
|
import { getValidatedBody } from '@zintrust/core/http';
|
|
11
12
|
import { Logger } from '@zintrust/core/logger';
|
|
@@ -31,7 +32,7 @@ const getBody = (req: IRequest): Record<string, unknown> => {
|
|
|
31
32
|
return (
|
|
32
33
|
getValidatedBody<Record<string, unknown>>(req) ??
|
|
33
34
|
(req.getBody?.() as Record<string, unknown> | undefined) ??
|
|
34
|
-
|
|
35
|
+
req.body ??
|
|
35
36
|
{}
|
|
36
37
|
);
|
|
37
38
|
};
|
|
@@ -40,6 +41,162 @@ const getBody = (req: IRequest): Record<string, unknown> => {
|
|
|
40
41
|
|
|
41
42
|
const isCloudflareEnv = (): boolean => Cloudflare.getWorkersEnv() !== null;
|
|
42
43
|
|
|
44
|
+
const shouldUseRedisRpcWorkerLifecycle = (
|
|
45
|
+
persistenceOverride: PersistenceOverride | undefined
|
|
46
|
+
): boolean => {
|
|
47
|
+
if (!isCloudflareEnv()) return false;
|
|
48
|
+
if (persistenceOverride?.driver !== 'redis') return false;
|
|
49
|
+
return Env.getBool('USE_REDIS_PROXY', false) && Env.get('REDIS_RPC_URL', '').trim().length > 0;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const createRequestId = (): string => {
|
|
53
|
+
const cryptoApi = globalThis.crypto as { randomUUID?: () => string } | undefined;
|
|
54
|
+
if (typeof cryptoApi?.randomUUID === 'function') return cryptoApi.randomUUID();
|
|
55
|
+
return `${Date.now()}-${Math.random().toString(16).slice(2)}`;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const callRedisRpc = async <T = unknown>(
|
|
59
|
+
service: string,
|
|
60
|
+
method: string,
|
|
61
|
+
payload: Record<string, unknown> = {}
|
|
62
|
+
): Promise<T> => {
|
|
63
|
+
const baseUrl = Env.get('REDIS_RPC_URL', '').trim();
|
|
64
|
+
const endpoint = new URL('/rpc', baseUrl);
|
|
65
|
+
const secret = Env.get('REDIS_RPC_SECRET', Env.get('REDIS_PROXY_SECRET', Env.APP_KEY));
|
|
66
|
+
const response = await fetch(endpoint, {
|
|
67
|
+
method: 'POST',
|
|
68
|
+
headers: {
|
|
69
|
+
'content-type': 'application/json',
|
|
70
|
+
...(secret.trim() === '' ? {} : { 'x-redis-rpc-secret': secret }),
|
|
71
|
+
},
|
|
72
|
+
body: JSON.stringify({ requestId: createRequestId(), service, method, payload }),
|
|
73
|
+
});
|
|
74
|
+
const body = (await response.json().catch(() => ({}))) as {
|
|
75
|
+
ok?: boolean;
|
|
76
|
+
result: T;
|
|
77
|
+
error?: { message?: string };
|
|
78
|
+
};
|
|
79
|
+
if (!response.ok || body.ok !== true) {
|
|
80
|
+
throw ErrorFactory.createTryCatchError(
|
|
81
|
+
body.error?.message ?? `Redis RPC request failed with status ${response.status}`
|
|
82
|
+
);
|
|
83
|
+
}
|
|
84
|
+
return body.result;
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
type WorkerManifestEntry = Readonly<{
|
|
88
|
+
name?: string;
|
|
89
|
+
workerName?: string;
|
|
90
|
+
queueName?: string;
|
|
91
|
+
processorSpec?: string | null;
|
|
92
|
+
concurrency?: number;
|
|
93
|
+
autoStart?: boolean;
|
|
94
|
+
activeStatus?: boolean;
|
|
95
|
+
}>;
|
|
96
|
+
|
|
97
|
+
type PersistenceOverride =
|
|
98
|
+
| { driver: 'memory' }
|
|
99
|
+
| { driver: 'redis'; redis: { env: true }; keyPrefix?: string }
|
|
100
|
+
| { driver: 'database'; connection?: string; table?: string };
|
|
101
|
+
|
|
102
|
+
type WorkersManifestGlobal = typeof globalThis & {
|
|
103
|
+
__zintrustWorkerManifestGlobalKeys?: ReadonlyArray<string>;
|
|
104
|
+
[key: string]: unknown;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
const DEFAULT_WORKER_MANIFEST_GLOBAL_KEYS = Object.freeze([
|
|
108
|
+
'__zintrustWorkersManifest',
|
|
109
|
+
'__zintrustWorkerManifest',
|
|
110
|
+
'__zintrustAppWorkerManifest',
|
|
111
|
+
] as const);
|
|
112
|
+
|
|
113
|
+
const readWorkerManifestGlobalKeys = (): string[] => {
|
|
114
|
+
const manifestGlobal = globalThis as WorkersManifestGlobal;
|
|
115
|
+
const fromGlobal = manifestGlobal.__zintrustWorkerManifestGlobalKeys;
|
|
116
|
+
if (Array.isArray(fromGlobal) && fromGlobal.every((key) => typeof key === 'string')) {
|
|
117
|
+
return Array.from(new Set(fromGlobal.map((key) => key.trim()).filter(Boolean)));
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
const raw = Env.get('WORKER_MANIFEST_GLOBAL_KEYS', '').trim();
|
|
121
|
+
if (raw.length > 0) {
|
|
122
|
+
try {
|
|
123
|
+
const parsed = JSON.parse(raw) as unknown;
|
|
124
|
+
if (Array.isArray(parsed)) {
|
|
125
|
+
return Array.from(
|
|
126
|
+
new Set(parsed.map((key) => String(key).trim()).filter((key) => key.length > 0))
|
|
127
|
+
);
|
|
128
|
+
}
|
|
129
|
+
} catch {
|
|
130
|
+
// fall through
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const csv = raw
|
|
134
|
+
.split(',')
|
|
135
|
+
.map((key) => key.trim())
|
|
136
|
+
.filter(Boolean);
|
|
137
|
+
if (csv.length > 0) return Array.from(new Set(csv));
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
return [...DEFAULT_WORKER_MANIFEST_GLOBAL_KEYS];
|
|
141
|
+
};
|
|
142
|
+
|
|
143
|
+
const resolveManifestWorker = (
|
|
144
|
+
workerName: string
|
|
145
|
+
): {
|
|
146
|
+
workerName: string;
|
|
147
|
+
queueName: string;
|
|
148
|
+
processorSpec: string | null;
|
|
149
|
+
concurrency: number;
|
|
150
|
+
} | null => {
|
|
151
|
+
const manifestGlobal = globalThis as WorkersManifestGlobal;
|
|
152
|
+
const keys = readWorkerManifestGlobalKeys();
|
|
153
|
+
for (const key of keys) {
|
|
154
|
+
const value = manifestGlobal[key];
|
|
155
|
+
if (!Array.isArray(value)) continue;
|
|
156
|
+
const entries = value as WorkerManifestEntry[];
|
|
157
|
+
const match = entries.find((entry) => (entry.name ?? entry.workerName) === workerName);
|
|
158
|
+
if (!match) continue;
|
|
159
|
+
const queueName =
|
|
160
|
+
typeof match.queueName === 'string' && match.queueName.trim().length > 0
|
|
161
|
+
? match.queueName.trim()
|
|
162
|
+
: `${workerName}-queue`;
|
|
163
|
+
const processorSpec =
|
|
164
|
+
typeof match.processorSpec === 'string' && match.processorSpec.trim().length > 0
|
|
165
|
+
? match.processorSpec.trim()
|
|
166
|
+
: null;
|
|
167
|
+
const concurrency = Math.max(1, Number(match.concurrency ?? 1) || 1);
|
|
168
|
+
return { workerName, queueName, processorSpec, concurrency };
|
|
169
|
+
}
|
|
170
|
+
return null;
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
const isManifestWorkerInactive = (workerName: string): boolean => {
|
|
174
|
+
const manifestGlobal = globalThis as WorkersManifestGlobal;
|
|
175
|
+
const keys = readWorkerManifestGlobalKeys();
|
|
176
|
+
for (const key of keys) {
|
|
177
|
+
const value = manifestGlobal[key];
|
|
178
|
+
if (!Array.isArray(value)) continue;
|
|
179
|
+
const entries = value as WorkerManifestEntry[];
|
|
180
|
+
const match = entries.find((entry) => (entry.name ?? entry.workerName) === workerName);
|
|
181
|
+
if (match?.activeStatus === false) return true;
|
|
182
|
+
}
|
|
183
|
+
return false;
|
|
184
|
+
};
|
|
185
|
+
|
|
186
|
+
const assertSlaAllowsWorkerRequest = async (workerName: string): Promise<void> => {
|
|
187
|
+
const gate = await SLAMonitor.canSendWorkerRequest(workerName);
|
|
188
|
+
if (gate.allowed) return;
|
|
189
|
+
throw ErrorFactory.createConfigError(
|
|
190
|
+
`Worker "${workerName}" blocked because SLA status is not compliant`,
|
|
191
|
+
{
|
|
192
|
+
kind: 'worker_sla_blocked',
|
|
193
|
+
workerName,
|
|
194
|
+
reason: gate.reason,
|
|
195
|
+
status: gate.status?.status,
|
|
196
|
+
}
|
|
197
|
+
);
|
|
198
|
+
};
|
|
199
|
+
|
|
43
200
|
const validateCreatePayload = (body: WorkerFactoryConfig, res: IResponse): boolean => {
|
|
44
201
|
if (!body.name || !body.queueName || !body.processor || !body.version) {
|
|
45
202
|
res.setStatus(400).json({
|
|
@@ -52,6 +209,29 @@ const validateCreatePayload = (body: WorkerFactoryConfig, res: IResponse): boole
|
|
|
52
209
|
return true;
|
|
53
210
|
};
|
|
54
211
|
|
|
212
|
+
const isSlaBlockedError = (error: unknown): boolean => {
|
|
213
|
+
const details = (error as { details?: { kind?: unknown } } | undefined)?.details;
|
|
214
|
+
if (details?.kind === 'worker_sla_blocked') return true;
|
|
215
|
+
return error instanceof Error && error.message.includes('SLA status is not compliant');
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const respondWorkerOperationError = (
|
|
219
|
+
res: IResponse,
|
|
220
|
+
error: unknown,
|
|
221
|
+
fallbackStatus = 500
|
|
222
|
+
): void => {
|
|
223
|
+
if (isSlaBlockedError(error)) {
|
|
224
|
+
res.setStatus(409).json({
|
|
225
|
+
ok: false,
|
|
226
|
+
error: (error as Error).message,
|
|
227
|
+
code: 'WORKER_SLA_BLOCKED',
|
|
228
|
+
});
|
|
229
|
+
return;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
res.setStatus(fallbackStatus).json({ error: (error as Error).message });
|
|
233
|
+
};
|
|
234
|
+
|
|
55
235
|
const respondIfWorkerExists = async (
|
|
56
236
|
name: string,
|
|
57
237
|
persistenceOverride: ReturnType<typeof resolvePersistenceOverride>,
|
|
@@ -161,7 +341,7 @@ async function create(req: IRequest, res: IResponse): Promise<void> {
|
|
|
161
341
|
if (!resolvedProcessor) return;
|
|
162
342
|
|
|
163
343
|
const config = {
|
|
164
|
-
...
|
|
344
|
+
...body,
|
|
165
345
|
processor: resolvedProcessor.processor,
|
|
166
346
|
processorSpec: resolvedProcessor.processorSpec,
|
|
167
347
|
};
|
|
@@ -169,7 +349,7 @@ async function create(req: IRequest, res: IResponse): Promise<void> {
|
|
|
169
349
|
await finalizeWorkerCreate(config, res);
|
|
170
350
|
} catch (error) {
|
|
171
351
|
Logger.error('WorkerController.create failed', error);
|
|
172
|
-
res
|
|
352
|
+
respondWorkerOperationError(res, error);
|
|
173
353
|
}
|
|
174
354
|
}
|
|
175
355
|
|
|
@@ -188,6 +368,23 @@ async function start(req: IRequest, res: IResponse): Promise<void> {
|
|
|
188
368
|
const persistenceOverride = resolvePersistenceOverride(req);
|
|
189
369
|
const isActive = await ensureActiveWorker(name, persistenceOverride, res);
|
|
190
370
|
if (!isActive) return;
|
|
371
|
+
|
|
372
|
+
if (shouldUseRedisRpcWorkerLifecycle(persistenceOverride)) {
|
|
373
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
374
|
+
const manifest = resolveManifestWorker(name);
|
|
375
|
+
if (!manifest) {
|
|
376
|
+
res.setStatus(404).json({ ok: false, error: `Worker ${name} not found in manifest` });
|
|
377
|
+
return;
|
|
378
|
+
}
|
|
379
|
+
const record = await callRedisRpc('worker', 'startAppWorker', manifest);
|
|
380
|
+
res.json({
|
|
381
|
+
ok: true,
|
|
382
|
+
message: `Worker ${name} started (redis-rpc registry updated)`,
|
|
383
|
+
record,
|
|
384
|
+
});
|
|
385
|
+
return;
|
|
386
|
+
}
|
|
387
|
+
|
|
191
388
|
const registered = WorkerRegistry.list().includes(name);
|
|
192
389
|
|
|
193
390
|
if (!registered) {
|
|
@@ -199,7 +396,7 @@ async function start(req: IRequest, res: IResponse): Promise<void> {
|
|
|
199
396
|
res.json({ ok: true, message: `Worker ${name} started` });
|
|
200
397
|
} catch (error) {
|
|
201
398
|
Logger.error('WorkerController.start failed', error);
|
|
202
|
-
res
|
|
399
|
+
respondWorkerOperationError(res, error);
|
|
203
400
|
}
|
|
204
401
|
}
|
|
205
402
|
|
|
@@ -214,11 +411,34 @@ async function stop(req: IRequest, res: IResponse): Promise<void> {
|
|
|
214
411
|
const persistenceOverride = resolvePersistenceOverride(req);
|
|
215
412
|
const isActive = await ensureActiveWorker(name, persistenceOverride, res);
|
|
216
413
|
if (!isActive) return;
|
|
414
|
+
|
|
415
|
+
if (name && shouldUseRedisRpcWorkerLifecycle(persistenceOverride)) {
|
|
416
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
417
|
+
// The registry stop operation is idempotent, but may return false if the worker
|
|
418
|
+
// has not been registered yet. In that case, register then stop to ensure the
|
|
419
|
+
// "stopped" state persists after a single click.
|
|
420
|
+
let recordOrFalse = await callRedisRpc<unknown>('worker', 'stopWorker', { workerName: name });
|
|
421
|
+
if (recordOrFalse === false) {
|
|
422
|
+
const manifest = resolveManifestWorker(name);
|
|
423
|
+
if (manifest) {
|
|
424
|
+
await callRedisRpc('worker', 'startAppWorker', manifest);
|
|
425
|
+
}
|
|
426
|
+
recordOrFalse = await callRedisRpc('worker', 'stopWorker', { workerName: name });
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
res.json({
|
|
430
|
+
ok: true,
|
|
431
|
+
message: `Worker ${name} stopped (redis-rpc registry updated)`,
|
|
432
|
+
result: recordOrFalse,
|
|
433
|
+
});
|
|
434
|
+
return;
|
|
435
|
+
}
|
|
436
|
+
|
|
217
437
|
await WorkerFactory.stop(name, persistenceOverride);
|
|
218
438
|
res.json({ ok: true, message: `Worker ${name} stopped` });
|
|
219
439
|
} catch (error) {
|
|
220
440
|
Logger.error('WorkerController.stop failed', error);
|
|
221
|
-
res
|
|
441
|
+
respondWorkerOperationError(res, error);
|
|
222
442
|
}
|
|
223
443
|
}
|
|
224
444
|
|
|
@@ -233,11 +453,28 @@ async function restart(req: IRequest, res: IResponse): Promise<void> {
|
|
|
233
453
|
const persistenceOverride = resolvePersistenceOverride(req);
|
|
234
454
|
const isActive = await ensureActiveWorker(name, persistenceOverride, res);
|
|
235
455
|
if (!isActive) return;
|
|
456
|
+
|
|
457
|
+
if (name && shouldUseRedisRpcWorkerLifecycle(persistenceOverride)) {
|
|
458
|
+
await assertSlaAllowsWorkerRequest(name);
|
|
459
|
+
const manifest = resolveManifestWorker(name);
|
|
460
|
+
if (!manifest) {
|
|
461
|
+
res.setStatus(404).json({ ok: false, error: `Worker ${name} not found in manifest` });
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
const record = await callRedisRpc('worker', 'restartAppWorker', manifest);
|
|
465
|
+
res.json({
|
|
466
|
+
ok: true,
|
|
467
|
+
message: `Worker ${name} restarted (redis-rpc registry updated)`,
|
|
468
|
+
record,
|
|
469
|
+
});
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
|
|
236
473
|
await WorkerFactory.restart(name, persistenceOverride);
|
|
237
474
|
res.json({ ok: true, message: `Worker ${name} restarted` });
|
|
238
475
|
} catch (error) {
|
|
239
476
|
Logger.error('WorkerController.restart failed', error);
|
|
240
|
-
res
|
|
477
|
+
respondWorkerOperationError(res, error);
|
|
241
478
|
}
|
|
242
479
|
}
|
|
243
480
|
|
|
@@ -263,7 +500,7 @@ async function setAutoStart(req: IRequest, res: IResponse): Promise<void> {
|
|
|
263
500
|
if (typeof rawEnabled === 'boolean') {
|
|
264
501
|
enabled = rawEnabled;
|
|
265
502
|
} else {
|
|
266
|
-
const enabledStr = normalizeQueryValue(rawEnabled
|
|
503
|
+
const enabledStr = normalizeQueryValue(rawEnabled) ?? '';
|
|
267
504
|
enabled = ['true', '1', 'yes', 'on'].includes(enabledStr.toLowerCase());
|
|
268
505
|
}
|
|
269
506
|
|
|
@@ -271,6 +508,17 @@ async function setAutoStart(req: IRequest, res: IResponse): Promise<void> {
|
|
|
271
508
|
const isActive = await ensureActiveWorker(name, persistenceOverride, res);
|
|
272
509
|
if (!isActive) return;
|
|
273
510
|
|
|
511
|
+
if (shouldUseRedisRpcWorkerLifecycle(persistenceOverride)) {
|
|
512
|
+
const manifest = resolveManifestWorker(name);
|
|
513
|
+
if (!manifest) {
|
|
514
|
+
res.setStatus(404).json({ ok: false, error: `Worker ${name} not found in manifest` });
|
|
515
|
+
return;
|
|
516
|
+
}
|
|
517
|
+
await callRedisRpc('worker', 'startAppWorker', { ...manifest, autoStart: enabled });
|
|
518
|
+
res.json({ ok: true, message: `Worker ${name} autoStart set to ${enabled}` });
|
|
519
|
+
return;
|
|
520
|
+
}
|
|
521
|
+
|
|
274
522
|
await WorkerFactory.setAutoStart(name, enabled, persistenceOverride);
|
|
275
523
|
|
|
276
524
|
res.json({ ok: true, message: `Worker ${name} autoStart set to ${enabled}` });
|
|
@@ -347,13 +595,7 @@ const normalizeQueryValue = (value: string | string[] | undefined): string | und
|
|
|
347
595
|
return undefined;
|
|
348
596
|
};
|
|
349
597
|
|
|
350
|
-
const resolvePersistenceOverride = (
|
|
351
|
-
req: IRequest
|
|
352
|
-
):
|
|
353
|
-
| { driver: 'memory' }
|
|
354
|
-
| { driver: 'redis'; redis: { env: true }; keyPrefix?: string }
|
|
355
|
-
| { driver: 'database'; connection?: string; table?: string }
|
|
356
|
-
| undefined => {
|
|
598
|
+
const resolvePersistenceOverride = (req: IRequest): PersistenceOverride | undefined => {
|
|
357
599
|
// Check for 'driver' parameter first (from frontend), then fallback to 'storage'
|
|
358
600
|
const driverRaw =
|
|
359
601
|
normalizeQueryValue(req.getQueryParam?.('driver')) ||
|
|
@@ -391,11 +633,7 @@ const resolvePersistenceOverride = (
|
|
|
391
633
|
|
|
392
634
|
const ensureActiveWorker = async (
|
|
393
635
|
name: string | undefined,
|
|
394
|
-
persistenceOverride:
|
|
395
|
-
| { driver: 'memory' }
|
|
396
|
-
| { driver: 'redis'; redis: { env: true }; keyPrefix?: string }
|
|
397
|
-
| { driver: 'database'; connection?: string; table?: string }
|
|
398
|
-
| undefined,
|
|
636
|
+
persistenceOverride: PersistenceOverride | undefined,
|
|
399
637
|
res: IResponse
|
|
400
638
|
): Promise<boolean> => {
|
|
401
639
|
if (!name) return false;
|
|
@@ -406,12 +644,21 @@ const ensureActiveWorker = async (
|
|
|
406
644
|
return false;
|
|
407
645
|
}
|
|
408
646
|
|
|
409
|
-
|
|
410
|
-
if
|
|
647
|
+
// In Cloudflare Redis-RPC mode we cannot rely on direct persistence reads. Use the app manifest
|
|
648
|
+
// to determine if the worker is intentionally inactive.
|
|
649
|
+
if (shouldUseRedisRpcWorkerLifecycle(persistenceOverride) && isManifestWorkerInactive(name)) {
|
|
411
650
|
res.setStatus(410).json({ error: 'Worker is inactive', code: 'WORKER_INACTIVE' });
|
|
412
651
|
return false;
|
|
413
652
|
}
|
|
414
653
|
|
|
654
|
+
if (!shouldUseRedisRpcWorkerLifecycle(persistenceOverride)) {
|
|
655
|
+
const persisted = await WorkerFactory.getPersisted(name, persistenceOverride);
|
|
656
|
+
if (persisted?.activeStatus === false) {
|
|
657
|
+
res.setStatus(410).json({ error: 'Worker is inactive', code: 'WORKER_INACTIVE' });
|
|
658
|
+
return false;
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
|
|
415
662
|
return true;
|
|
416
663
|
};
|
|
417
664
|
|
|
@@ -518,9 +765,7 @@ async function validateProcessorSpecIfNeeded(
|
|
|
518
765
|
updateData: Record<string, unknown>
|
|
519
766
|
): Promise<boolean> {
|
|
520
767
|
if (typeof updateData['processorSpec'] === 'string') {
|
|
521
|
-
const resolved = await WorkerFactory.resolveProcessorSpec(
|
|
522
|
-
updateData['processorSpec'] as string
|
|
523
|
-
);
|
|
768
|
+
const resolved = await WorkerFactory.resolveProcessorSpec(updateData['processorSpec']);
|
|
524
769
|
return Boolean(resolved);
|
|
525
770
|
}
|
|
526
771
|
return true;
|
|
@@ -528,12 +773,12 @@ async function validateProcessorSpecIfNeeded(
|
|
|
528
773
|
|
|
529
774
|
async function persistUpdatedRecord(
|
|
530
775
|
name: string,
|
|
531
|
-
updatedRecord: WorkerRecord
|
|
776
|
+
updatedRecord: WorkerRecord,
|
|
532
777
|
persistenceOverride: ReturnType<typeof resolvePersistenceOverride> | undefined,
|
|
533
778
|
updateData: Record<string, unknown>
|
|
534
779
|
): Promise<void> {
|
|
535
780
|
try {
|
|
536
|
-
await WorkerFactory.update(name, updatedRecord
|
|
781
|
+
await WorkerFactory.update(name, updatedRecord, persistenceOverride);
|
|
537
782
|
Logger.info(`Worker ${name} persistence updated with fields:`, Object.keys(updateData));
|
|
538
783
|
} catch (persistError) {
|
|
539
784
|
Logger.warn(`Failed to persist some updates for ${name}`, persistError as Error);
|
|
@@ -549,8 +794,7 @@ async function restartIfNeeded(
|
|
|
549
794
|
persistenceOverride: ReturnType<typeof resolvePersistenceOverride> | undefined
|
|
550
795
|
): Promise<string | undefined> {
|
|
551
796
|
if (
|
|
552
|
-
|
|
553
|
-
currentInstance.status !== 'running' ||
|
|
797
|
+
currentInstance?.status !== 'running' ||
|
|
554
798
|
updatedRecord.activeStatus === false ||
|
|
555
799
|
currentRecord.activeStatus === false
|
|
556
800
|
) {
|
|
@@ -665,10 +909,7 @@ async function startMonitoring(req: IRequest, res: IResponse): Promise<void> {
|
|
|
665
909
|
try {
|
|
666
910
|
const name = getParam(req, 'name');
|
|
667
911
|
const body = getBody(req);
|
|
668
|
-
HealthMonitor.startMonitoring(
|
|
669
|
-
name,
|
|
670
|
-
body as Parameters<typeof HealthMonitor.startMonitoring>[1]
|
|
671
|
-
);
|
|
912
|
+
HealthMonitor.startMonitoring(name, body);
|
|
672
913
|
res.json({ ok: true, message: `Health monitoring started for ${name}` });
|
|
673
914
|
} catch (error) {
|
|
674
915
|
Logger.error('WorkerController.startMonitoring failed', error);
|
|
@@ -737,7 +978,7 @@ async function getSlaStatus(req: IRequest, res: IResponse): Promise<void> {
|
|
|
737
978
|
try {
|
|
738
979
|
const name = getParam(req, 'name');
|
|
739
980
|
const slaStatus = await SLAMonitor.checkCompliance(name);
|
|
740
|
-
res.json({ ok: true, status: slaStatus });
|
|
981
|
+
res.json({ ok: true, allowed: slaStatus.status === 'compliant', status: slaStatus });
|
|
741
982
|
} catch (error) {
|
|
742
983
|
Logger.error('WorkerController.getSlaStatus failed', error);
|
|
743
984
|
if ((error as Error).message.includes('SLA config not found')) {
|
|
@@ -760,7 +1001,7 @@ async function updateMonitoringConfig(req: IRequest, res: IResponse): Promise<vo
|
|
|
760
1001
|
try {
|
|
761
1002
|
const name = getParam(req, 'name');
|
|
762
1003
|
const body = getBody(req);
|
|
763
|
-
HealthMonitor.updateConfig(name, body
|
|
1004
|
+
HealthMonitor.updateConfig(name, body);
|
|
764
1005
|
res.json({ ok: true, message: `Monitoring config updated for ${name}` });
|
|
765
1006
|
} catch (error) {
|
|
766
1007
|
Logger.error('WorkerController.updateMonitoringConfig failed', error);
|
package/src/index.ts
CHANGED
|
@@ -96,7 +96,14 @@ export type {
|
|
|
96
96
|
IChaosReport,
|
|
97
97
|
IChaosStatus,
|
|
98
98
|
} from './ChaosEngineering';
|
|
99
|
-
export type {
|
|
99
|
+
export type {
|
|
100
|
+
ISLAConfig,
|
|
101
|
+
ISLAGate,
|
|
102
|
+
ISLAReport,
|
|
103
|
+
ISLAStatus,
|
|
104
|
+
ISLAViolation,
|
|
105
|
+
ITimeRange,
|
|
106
|
+
} from './SLAMonitor';
|
|
100
107
|
|
|
101
108
|
export type * from './config/workerConfig';
|
|
102
109
|
export type * from './type';
|