@zintrust/workers 0.1.27
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 +861 -0
- package/dist/AnomalyDetection.d.ts +102 -0
- package/dist/AnomalyDetection.js +321 -0
- package/dist/AutoScaler.d.ts +127 -0
- package/dist/AutoScaler.js +425 -0
- package/dist/BroadcastWorker.d.ts +21 -0
- package/dist/BroadcastWorker.js +24 -0
- package/dist/CanaryController.d.ts +103 -0
- package/dist/CanaryController.js +380 -0
- package/dist/ChaosEngineering.d.ts +79 -0
- package/dist/ChaosEngineering.js +216 -0
- package/dist/CircuitBreaker.d.ts +106 -0
- package/dist/CircuitBreaker.js +374 -0
- package/dist/ClusterLock.d.ts +90 -0
- package/dist/ClusterLock.js +385 -0
- package/dist/ComplianceManager.d.ts +177 -0
- package/dist/ComplianceManager.js +556 -0
- package/dist/DatacenterOrchestrator.d.ts +133 -0
- package/dist/DatacenterOrchestrator.js +404 -0
- package/dist/DeadLetterQueue.d.ts +122 -0
- package/dist/DeadLetterQueue.js +539 -0
- package/dist/HealthMonitor.d.ts +42 -0
- package/dist/HealthMonitor.js +301 -0
- package/dist/MultiQueueWorker.d.ts +89 -0
- package/dist/MultiQueueWorker.js +277 -0
- package/dist/NotificationWorker.d.ts +21 -0
- package/dist/NotificationWorker.js +23 -0
- package/dist/Observability.d.ts +153 -0
- package/dist/Observability.js +530 -0
- package/dist/PluginManager.d.ts +123 -0
- package/dist/PluginManager.js +392 -0
- package/dist/PriorityQueue.d.ts +117 -0
- package/dist/PriorityQueue.js +244 -0
- package/dist/ResourceMonitor.d.ts +164 -0
- package/dist/ResourceMonitor.js +605 -0
- package/dist/SLAMonitor.d.ts +110 -0
- package/dist/SLAMonitor.js +274 -0
- package/dist/WorkerFactory.d.ts +193 -0
- package/dist/WorkerFactory.js +1507 -0
- package/dist/WorkerInit.d.ts +85 -0
- package/dist/WorkerInit.js +223 -0
- package/dist/WorkerMetrics.d.ts +114 -0
- package/dist/WorkerMetrics.js +509 -0
- package/dist/WorkerRegistry.d.ts +145 -0
- package/dist/WorkerRegistry.js +319 -0
- package/dist/WorkerShutdown.d.ts +61 -0
- package/dist/WorkerShutdown.js +159 -0
- package/dist/WorkerVersioning.d.ts +107 -0
- package/dist/WorkerVersioning.js +300 -0
- package/dist/build-manifest.json +462 -0
- package/dist/config/workerConfig.d.ts +3 -0
- package/dist/config/workerConfig.js +19 -0
- package/dist/createQueueWorker.d.ts +23 -0
- package/dist/createQueueWorker.js +113 -0
- package/dist/dashboard/index.d.ts +1 -0
- package/dist/dashboard/index.js +1 -0
- package/dist/dashboard/types.d.ts +117 -0
- package/dist/dashboard/types.js +1 -0
- package/dist/dashboard/workers-api.d.ts +4 -0
- package/dist/dashboard/workers-api.js +638 -0
- package/dist/dashboard/workers-dashboard-ui.d.ts +3 -0
- package/dist/dashboard/workers-dashboard-ui.js +1026 -0
- package/dist/dashboard/workers-dashboard.d.ts +4 -0
- package/dist/dashboard/workers-dashboard.js +904 -0
- package/dist/helper/index.d.ts +5 -0
- package/dist/helper/index.js +10 -0
- package/dist/http/WorkerApiController.d.ts +38 -0
- package/dist/http/WorkerApiController.js +312 -0
- package/dist/http/WorkerController.d.ts +374 -0
- package/dist/http/WorkerController.js +1351 -0
- package/dist/http/middleware/CustomValidation.d.ts +92 -0
- package/dist/http/middleware/CustomValidation.js +270 -0
- package/dist/http/middleware/DatacenterValidator.d.ts +3 -0
- package/dist/http/middleware/DatacenterValidator.js +94 -0
- package/dist/http/middleware/EditWorkerValidation.d.ts +7 -0
- package/dist/http/middleware/EditWorkerValidation.js +55 -0
- package/dist/http/middleware/FeaturesValidator.d.ts +3 -0
- package/dist/http/middleware/FeaturesValidator.js +60 -0
- package/dist/http/middleware/InfrastructureValidator.d.ts +31 -0
- package/dist/http/middleware/InfrastructureValidator.js +226 -0
- package/dist/http/middleware/OptionsValidator.d.ts +3 -0
- package/dist/http/middleware/OptionsValidator.js +112 -0
- package/dist/http/middleware/PayloadSanitizer.d.ts +7 -0
- package/dist/http/middleware/PayloadSanitizer.js +42 -0
- package/dist/http/middleware/ProcessorPathSanitizer.d.ts +3 -0
- package/dist/http/middleware/ProcessorPathSanitizer.js +74 -0
- package/dist/http/middleware/QueueNameSanitizer.d.ts +3 -0
- package/dist/http/middleware/QueueNameSanitizer.js +45 -0
- package/dist/http/middleware/ValidateDriver.d.ts +7 -0
- package/dist/http/middleware/ValidateDriver.js +20 -0
- package/dist/http/middleware/VersionSanitizer.d.ts +3 -0
- package/dist/http/middleware/VersionSanitizer.js +25 -0
- package/dist/http/middleware/WorkerNameSanitizer.d.ts +3 -0
- package/dist/http/middleware/WorkerNameSanitizer.js +46 -0
- package/dist/http/middleware/WorkerValidationChain.d.ts +27 -0
- package/dist/http/middleware/WorkerValidationChain.js +185 -0
- package/dist/index.d.ts +46 -0
- package/dist/index.js +48 -0
- package/dist/routes/workers.d.ts +12 -0
- package/dist/routes/workers.js +81 -0
- package/dist/storage/WorkerStore.d.ts +45 -0
- package/dist/storage/WorkerStore.js +195 -0
- package/dist/type.d.ts +76 -0
- package/dist/type.js +1 -0
- package/dist/ui/router/ui.d.ts +3 -0
- package/dist/ui/router/ui.js +83 -0
- package/dist/ui/types/worker-ui.d.ts +229 -0
- package/dist/ui/types/worker-ui.js +5 -0
- package/package.json +53 -0
- package/src/AnomalyDetection.ts +434 -0
- package/src/AutoScaler.ts +654 -0
- package/src/BroadcastWorker.ts +34 -0
- package/src/CanaryController.ts +531 -0
- package/src/ChaosEngineering.ts +301 -0
- package/src/CircuitBreaker.ts +495 -0
- package/src/ClusterLock.ts +499 -0
- package/src/ComplianceManager.ts +815 -0
- package/src/DatacenterOrchestrator.ts +561 -0
- package/src/DeadLetterQueue.ts +733 -0
- package/src/HealthMonitor.ts +390 -0
- package/src/MultiQueueWorker.ts +431 -0
- package/src/NotificationWorker.ts +33 -0
- package/src/Observability.ts +696 -0
- package/src/PluginManager.ts +551 -0
- package/src/PriorityQueue.ts +351 -0
- package/src/ResourceMonitor.ts +769 -0
- package/src/SLAMonitor.ts +408 -0
- package/src/WorkerFactory.ts +2108 -0
- package/src/WorkerInit.ts +313 -0
- package/src/WorkerMetrics.ts +709 -0
- package/src/WorkerRegistry.ts +443 -0
- package/src/WorkerShutdown.ts +210 -0
- package/src/WorkerVersioning.ts +422 -0
- package/src/config/workerConfig.ts +25 -0
- package/src/createQueueWorker.ts +174 -0
- package/src/dashboard/index.ts +6 -0
- package/src/dashboard/types.ts +141 -0
- package/src/dashboard/workers-api.ts +785 -0
- package/src/dashboard/zintrust.svg +30 -0
- package/src/helper/index.ts +11 -0
- package/src/http/WorkerApiController.ts +369 -0
- package/src/http/WorkerController.ts +1512 -0
- package/src/http/middleware/CustomValidation.ts +360 -0
- package/src/http/middleware/DatacenterValidator.ts +124 -0
- package/src/http/middleware/EditWorkerValidation.ts +74 -0
- package/src/http/middleware/FeaturesValidator.ts +82 -0
- package/src/http/middleware/InfrastructureValidator.ts +295 -0
- package/src/http/middleware/OptionsValidator.ts +144 -0
- package/src/http/middleware/PayloadSanitizer.ts +52 -0
- package/src/http/middleware/ProcessorPathSanitizer.ts +86 -0
- package/src/http/middleware/QueueNameSanitizer.ts +55 -0
- package/src/http/middleware/ValidateDriver.ts +29 -0
- package/src/http/middleware/VersionSanitizer.ts +30 -0
- package/src/http/middleware/WorkerNameSanitizer.ts +56 -0
- package/src/http/middleware/WorkerValidationChain.ts +230 -0
- package/src/index.ts +98 -0
- package/src/routes/workers.ts +154 -0
- package/src/storage/WorkerStore.ts +240 -0
- package/src/type.ts +89 -0
- package/src/types/queue-monitor.d.ts +38 -0
- package/src/types/queue-redis.d.ts +38 -0
- package/src/ui/README.md +13 -0
- package/src/ui/components/JsonEditor.js +670 -0
- package/src/ui/components/JsonViewer.js +387 -0
- package/src/ui/components/WorkerCard.js +178 -0
- package/src/ui/components/WorkerExpandPanel.js +257 -0
- package/src/ui/components/fetcher.js +42 -0
- package/src/ui/components/sla-scorecard.js +32 -0
- package/src/ui/components/styles.css +30 -0
- package/src/ui/components/table-expander.js +34 -0
- package/src/ui/integration/worker-ui-integration.js +565 -0
- package/src/ui/router/ui.ts +99 -0
- package/src/ui/services/workerApi.js +240 -0
- package/src/ui/types/worker-ui.ts +283 -0
- package/src/ui/utils/jsonValidator.js +444 -0
- package/src/ui/workers/index.html +202 -0
- package/src/ui/workers/main.js +1781 -0
- package/src/ui/workers/styles.css +1350 -0
|
@@ -0,0 +1,390 @@
|
|
|
1
|
+
import { ErrorFactory, Logger } from '@zintrust/core';
|
|
2
|
+
import type { Worker } from 'bullmq';
|
|
3
|
+
import { WorkerCreationStatus, WorkerFactory } from './WorkerFactory';
|
|
4
|
+
|
|
5
|
+
export type HealthCheckResult = {
|
|
6
|
+
timestamp: Date;
|
|
7
|
+
status: 'healthy' | 'degraded' | 'critical';
|
|
8
|
+
latency: number;
|
|
9
|
+
message?: string;
|
|
10
|
+
meta?: Record<string, unknown>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
type HealthMonitorConfig = {
|
|
14
|
+
enabled: boolean;
|
|
15
|
+
tickIntervalMs: number;
|
|
16
|
+
concurrencyLimit: number;
|
|
17
|
+
checkTimeoutMs: number;
|
|
18
|
+
intervalHealthyMs: number;
|
|
19
|
+
intervalSuspectMs: number;
|
|
20
|
+
failureThreshold: number;
|
|
21
|
+
historyLimit: number;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
type WorkerMonitorConfig = {
|
|
25
|
+
degradedCallback?: (name: string, result: HealthCheckResult) => void;
|
|
26
|
+
criticalCallback?: (name: string, result: HealthCheckResult) => void;
|
|
27
|
+
[key: string]: unknown;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
type WorkerHealthState = {
|
|
31
|
+
name: string;
|
|
32
|
+
worker?: Worker; // Optional because startMonitoring might be called before register
|
|
33
|
+
queueName?: string;
|
|
34
|
+
status: WorkerCreationStatus;
|
|
35
|
+
lastCheck: Date;
|
|
36
|
+
nextCheck: Date;
|
|
37
|
+
consecutiveFailures: number;
|
|
38
|
+
inProgress: boolean;
|
|
39
|
+
config?: WorkerMonitorConfig;
|
|
40
|
+
history: HealthCheckResult[];
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
const DEFAULT_CONFIG: HealthMonitorConfig = {
|
|
44
|
+
enabled: true,
|
|
45
|
+
tickIntervalMs: 1000,
|
|
46
|
+
concurrencyLimit: 50,
|
|
47
|
+
checkTimeoutMs: 5000,
|
|
48
|
+
intervalHealthyMs: 30000,
|
|
49
|
+
intervalSuspectMs: 5000,
|
|
50
|
+
failureThreshold: 2,
|
|
51
|
+
historyLimit: 50,
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
// Module-level state (Singleton by nature of ESM)
|
|
55
|
+
const registry = new Map<string, WorkerHealthState>();
|
|
56
|
+
let config: HealthMonitorConfig = { ...DEFAULT_CONFIG };
|
|
57
|
+
let timer: NodeJS.Timeout | null = null;
|
|
58
|
+
let runningChecks = 0;
|
|
59
|
+
|
|
60
|
+
// Internal Helpers
|
|
61
|
+
const persistStatusChange = async (
|
|
62
|
+
name: string,
|
|
63
|
+
status: WorkerCreationStatus,
|
|
64
|
+
lastError?: string
|
|
65
|
+
): Promise<void> => {
|
|
66
|
+
try {
|
|
67
|
+
await WorkerFactory.updateStatus(name, status, lastError);
|
|
68
|
+
} catch (err) {
|
|
69
|
+
Logger.error(`Failed to persist status change for ${name}`, err);
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
|
|
73
|
+
const verifyWorkerHealth = async (
|
|
74
|
+
worker: Worker,
|
|
75
|
+
_name: string,
|
|
76
|
+
_queueName: string
|
|
77
|
+
): Promise<boolean> => {
|
|
78
|
+
// Check if isClosing exists (isClosing check safe for mocks)
|
|
79
|
+
const workerAny = worker as unknown as Record<string, unknown>;
|
|
80
|
+
const isClosingFn = workerAny['isClosing'];
|
|
81
|
+
|
|
82
|
+
if (
|
|
83
|
+
worker.isPaused() ||
|
|
84
|
+
(typeof isClosingFn === 'function' && (isClosingFn as () => boolean)())
|
|
85
|
+
) {
|
|
86
|
+
return false;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
const isRunning = await worker.isRunning();
|
|
90
|
+
if (!isRunning) return false;
|
|
91
|
+
|
|
92
|
+
const client = await worker.client;
|
|
93
|
+
const pingResult = await client.ping();
|
|
94
|
+
if (pingResult !== 'PONG') {
|
|
95
|
+
throw ErrorFactory.createWorkerError(`Redis ping failed: ${pingResult}`);
|
|
96
|
+
}
|
|
97
|
+
Logger.debug(`Worker health verification passed for ${_name} ${_queueName}`);
|
|
98
|
+
return true;
|
|
99
|
+
};
|
|
100
|
+
|
|
101
|
+
const updateState = (
|
|
102
|
+
state: WorkerHealthState,
|
|
103
|
+
isHealthy: boolean,
|
|
104
|
+
errorMsg: string | undefined,
|
|
105
|
+
latency: number
|
|
106
|
+
): void => {
|
|
107
|
+
const now = new Date();
|
|
108
|
+
state.lastCheck = now;
|
|
109
|
+
|
|
110
|
+
// Determine status (healthy > degraded > critical)
|
|
111
|
+
let status: 'healthy' | 'degraded' | 'critical';
|
|
112
|
+
if (isHealthy) {
|
|
113
|
+
status = 'healthy';
|
|
114
|
+
} else if (state.consecutiveFailures < config.failureThreshold) {
|
|
115
|
+
status = 'degraded';
|
|
116
|
+
} else {
|
|
117
|
+
status = 'critical';
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// Create Check Result
|
|
121
|
+
const result: HealthCheckResult = {
|
|
122
|
+
timestamp: now,
|
|
123
|
+
status,
|
|
124
|
+
latency,
|
|
125
|
+
message: errorMsg,
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
// Add to history
|
|
129
|
+
state.history.push(result);
|
|
130
|
+
if (state.history.length > config.historyLimit) {
|
|
131
|
+
state.history.shift();
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Callbacks
|
|
135
|
+
if (!isHealthy && state.config?.degradedCallback) {
|
|
136
|
+
state.config.degradedCallback(state.name, result);
|
|
137
|
+
}
|
|
138
|
+
if (result.status === 'critical' && state.config?.criticalCallback) {
|
|
139
|
+
state.config.criticalCallback(state.name, result);
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
if (isHealthy) {
|
|
143
|
+
state.consecutiveFailures = 0;
|
|
144
|
+
if (state.status !== WorkerCreationStatus.RUNNING) {
|
|
145
|
+
persistStatusChange(state.name, WorkerCreationStatus.RUNNING);
|
|
146
|
+
state.status = WorkerCreationStatus.RUNNING;
|
|
147
|
+
Logger.info(`Worker ${state.name} recovered to RUNNING`);
|
|
148
|
+
}
|
|
149
|
+
const jitter = Math.floor(Math.random() * 500); //NOSONAR
|
|
150
|
+
state.nextCheck = new Date(now.getTime() + config.intervalHealthyMs + jitter);
|
|
151
|
+
} else {
|
|
152
|
+
state.consecutiveFailures++;
|
|
153
|
+
|
|
154
|
+
if (
|
|
155
|
+
state.consecutiveFailures >= config.failureThreshold &&
|
|
156
|
+
state.status !== WorkerCreationStatus.FAILED
|
|
157
|
+
) {
|
|
158
|
+
persistStatusChange(state.name, WorkerCreationStatus.FAILED, errorMsg);
|
|
159
|
+
state.status = WorkerCreationStatus.FAILED;
|
|
160
|
+
Logger.warn(`Worker ${state.name} marked FAILED after ${state.consecutiveFailures} checks`, {
|
|
161
|
+
error: errorMsg,
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
const jitter = Math.floor(Math.random() * 500); //NOSONAR
|
|
166
|
+
state.nextCheck = new Date(now.getTime() + config.intervalSuspectMs + jitter);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
const performCheck = async (state: WorkerHealthState): Promise<void> => {
|
|
171
|
+
const startTime = Date.now();
|
|
172
|
+
let isHealthy = false;
|
|
173
|
+
let errorMsg: string | undefined;
|
|
174
|
+
|
|
175
|
+
try {
|
|
176
|
+
if (!state.worker) {
|
|
177
|
+
throw ErrorFactory.createWorkerError('Worker instance not available');
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
isHealthy = await Promise.race([
|
|
181
|
+
verifyWorkerHealth(state.worker, state.name, state.queueName || 'unknown'),
|
|
182
|
+
new Promise<boolean>((_, reject) => {
|
|
183
|
+
// eslint-disable-next-line
|
|
184
|
+
const id = setTimeout(() => {
|
|
185
|
+
reject(ErrorFactory.createWorkerError('Health check timeout'));
|
|
186
|
+
}, config.checkTimeoutMs);
|
|
187
|
+
// Unref to prevent holding event loop if everything else finishes
|
|
188
|
+
id.unref();
|
|
189
|
+
}),
|
|
190
|
+
]);
|
|
191
|
+
} catch (err) {
|
|
192
|
+
isHealthy = false;
|
|
193
|
+
errorMsg = (err as Error).message;
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const duration = Date.now() - startTime;
|
|
197
|
+
updateState(state, isHealthy, errorMsg, duration);
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
const scheduleCheck = async (state: WorkerHealthState): Promise<void> => {
|
|
201
|
+
state.inProgress = true;
|
|
202
|
+
runningChecks++;
|
|
203
|
+
|
|
204
|
+
performCheck(state).finally(() => {
|
|
205
|
+
state.inProgress = false;
|
|
206
|
+
runningChecks--;
|
|
207
|
+
});
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const tick = async (): Promise<void> => {
|
|
211
|
+
const now = new Date();
|
|
212
|
+
const candidates: WorkerHealthState[] = [];
|
|
213
|
+
|
|
214
|
+
for (const state of registry.values()) {
|
|
215
|
+
if (runningChecks >= config.concurrencyLimit) break;
|
|
216
|
+
// Skip if checks are paused or if worker instance is missing (wait for register)
|
|
217
|
+
if (!state.worker && !state.queueName) continue;
|
|
218
|
+
|
|
219
|
+
if (!state.inProgress && state.nextCheck <= now) {
|
|
220
|
+
candidates.push(state);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
for (const candidate of candidates) {
|
|
225
|
+
if (runningChecks >= config.concurrencyLimit) break;
|
|
226
|
+
scheduleCheck(candidate);
|
|
227
|
+
}
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const start = (): void => {
|
|
231
|
+
if (timer) return;
|
|
232
|
+
timer = setInterval(() => tick(), config.tickIntervalMs);
|
|
233
|
+
Logger.debug('HealthMonitor started');
|
|
234
|
+
};
|
|
235
|
+
|
|
236
|
+
const stop = (): void => {
|
|
237
|
+
if (timer) {
|
|
238
|
+
clearInterval(timer);
|
|
239
|
+
timer = null;
|
|
240
|
+
}
|
|
241
|
+
Logger.debug('HealthMonitor stopped');
|
|
242
|
+
};
|
|
243
|
+
|
|
244
|
+
// Exported Public Methods
|
|
245
|
+
|
|
246
|
+
const configure = (newConfig: Partial<HealthMonitorConfig>): void => {
|
|
247
|
+
config = { ...config, ...newConfig };
|
|
248
|
+
if (timer) {
|
|
249
|
+
stop();
|
|
250
|
+
start();
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
const register = (name: string, worker: Worker, queueName: string): void => {
|
|
255
|
+
let state = registry.get(name);
|
|
256
|
+
|
|
257
|
+
if (state) {
|
|
258
|
+
// update existing entry (maybe created by startMonitoring)
|
|
259
|
+
state.worker = worker;
|
|
260
|
+
state.queueName = queueName;
|
|
261
|
+
} else {
|
|
262
|
+
// Add jitter
|
|
263
|
+
const initialDelay = Math.floor(Math.random() * 5000); //NOSONAR
|
|
264
|
+
state = {
|
|
265
|
+
name,
|
|
266
|
+
worker,
|
|
267
|
+
queueName,
|
|
268
|
+
status: WorkerCreationStatus.STARTING,
|
|
269
|
+
lastCheck: new Date(),
|
|
270
|
+
nextCheck: new Date(Date.now() + initialDelay),
|
|
271
|
+
consecutiveFailures: 0,
|
|
272
|
+
inProgress: false,
|
|
273
|
+
history: [],
|
|
274
|
+
};
|
|
275
|
+
registry.set(name, state);
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
if (!timer) {
|
|
279
|
+
start();
|
|
280
|
+
}
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
const startMonitoring = (name: string, monitorConfig?: WorkerMonitorConfig): void => {
|
|
284
|
+
const state = registry.get(name);
|
|
285
|
+
|
|
286
|
+
if (state) {
|
|
287
|
+
if (monitorConfig) state.config = { ...state.config, ...monitorConfig };
|
|
288
|
+
} else {
|
|
289
|
+
// Worker instance not yet registered, create placeholder
|
|
290
|
+
const initialDelay = Math.floor(Math.random() * 5000); //NOSONAR
|
|
291
|
+
registry.set(name, {
|
|
292
|
+
name,
|
|
293
|
+
status: WorkerCreationStatus.STARTING,
|
|
294
|
+
lastCheck: new Date(),
|
|
295
|
+
nextCheck: new Date(Date.now() + initialDelay),
|
|
296
|
+
consecutiveFailures: 0,
|
|
297
|
+
inProgress: false,
|
|
298
|
+
history: [],
|
|
299
|
+
config: monitorConfig,
|
|
300
|
+
});
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
if (!timer) start();
|
|
304
|
+
};
|
|
305
|
+
|
|
306
|
+
const unregister = (name: string): void => {
|
|
307
|
+
registry.delete(name);
|
|
308
|
+
if (registry.size === 0) {
|
|
309
|
+
stop();
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
const stopMonitoring = (name: string): void => {
|
|
314
|
+
unregister(name);
|
|
315
|
+
};
|
|
316
|
+
|
|
317
|
+
const updateConfig = (name: string, monitorConfig: WorkerMonitorConfig): void => {
|
|
318
|
+
startMonitoring(name, monitorConfig);
|
|
319
|
+
};
|
|
320
|
+
|
|
321
|
+
const getCurrentHealth = (name: string): HealthCheckResult | null => {
|
|
322
|
+
const state = registry.get(name);
|
|
323
|
+
if (!state || state.history.length === 0) return null;
|
|
324
|
+
return state.history[state.history.length - 1];
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const getHealthHistory = (name: string, limit?: number): HealthCheckResult[] => {
|
|
328
|
+
const state = registry.get(name);
|
|
329
|
+
if (!state) return [];
|
|
330
|
+
const history = state.history;
|
|
331
|
+
return limit ? history.slice(-limit) : history;
|
|
332
|
+
};
|
|
333
|
+
|
|
334
|
+
const getHealthTrend = (name: string): { uptime: number; samples: number } => {
|
|
335
|
+
const history = getHealthHistory(name, 10);
|
|
336
|
+
const uptime = history.filter((h) => h.status === 'healthy').length / (history.length || 1);
|
|
337
|
+
return { uptime, samples: history.length };
|
|
338
|
+
};
|
|
339
|
+
|
|
340
|
+
const getSummary = async (): Promise<unknown> => {
|
|
341
|
+
interface SummaryDetail {
|
|
342
|
+
name: string;
|
|
343
|
+
status: string;
|
|
344
|
+
lastCheck: Date;
|
|
345
|
+
}
|
|
346
|
+
|
|
347
|
+
const summary = {
|
|
348
|
+
total: registry.size,
|
|
349
|
+
healthy: 0,
|
|
350
|
+
degraded: 0,
|
|
351
|
+
critical: 0,
|
|
352
|
+
details: [] as SummaryDetail[],
|
|
353
|
+
};
|
|
354
|
+
|
|
355
|
+
for (const [name, state] of registry) {
|
|
356
|
+
const lastResult = state.history[state.history.length - 1];
|
|
357
|
+
const status = lastResult?.status || 'unknown';
|
|
358
|
+
if (status === 'healthy') summary.healthy++;
|
|
359
|
+
else if (status === 'degraded') summary.degraded++;
|
|
360
|
+
else if (status === 'critical') summary.critical++;
|
|
361
|
+
|
|
362
|
+
summary.details.push({
|
|
363
|
+
name,
|
|
364
|
+
status,
|
|
365
|
+
lastCheck: state.lastCheck,
|
|
366
|
+
});
|
|
367
|
+
}
|
|
368
|
+
return summary;
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
const shutdown = (): void => {
|
|
372
|
+
stop();
|
|
373
|
+
registry.clear();
|
|
374
|
+
};
|
|
375
|
+
|
|
376
|
+
export const HealthMonitor = Object.freeze({
|
|
377
|
+
configure,
|
|
378
|
+
register,
|
|
379
|
+
unregister,
|
|
380
|
+
start,
|
|
381
|
+
stop,
|
|
382
|
+
startMonitoring,
|
|
383
|
+
stopMonitoring,
|
|
384
|
+
updateConfig,
|
|
385
|
+
getCurrentHealth,
|
|
386
|
+
getHealthHistory,
|
|
387
|
+
getHealthTrend,
|
|
388
|
+
getSummary,
|
|
389
|
+
shutdown,
|
|
390
|
+
});
|