Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | /** * V3 Health Monitor * Decomposed from orchestrator.ts - Agent health checks * ~150 lines (target achieved) */ import type { IHealthMonitor, IHealthStatus, IComponentHealth, } from '../interfaces/coordinator.interface.js'; import type { IEventBus } from '../interfaces/event.interface.js'; import { SystemEventTypes } from '../interfaces/event.interface.js'; /** * Health check function type */ export type HealthCheckFn = () => Promise<{ healthy: boolean; error?: string; metrics?: Record<string, number>; }>; /** * Health monitor configuration */ export interface HealthMonitorConfig { checkInterval: number; historyLimit: number; degradedThreshold: number; unhealthyThreshold: number; } /** * Health monitor implementation */ export class HealthMonitor implements IHealthMonitor { private checks = new Map<string, HealthCheckFn>(); private history: IHealthStatus[] = []; private interval?: ReturnType<typeof setInterval>; private listeners: Array<(status: IHealthStatus) => void> = []; private running = false; constructor( private eventBus: IEventBus, private config: HealthMonitorConfig = { checkInterval: 30000, historyLimit: 100, degradedThreshold: 1, unhealthyThreshold: 2, }, ) {} start(): void { if (this.running) { return; } this.running = true; this.interval = setInterval(async () => { const status = await this.getStatus(); this.addToHistory(status); this.notifyListeners(status); this.eventBus.emit(SystemEventTypes.SYSTEM_HEALTHCHECK, { status }); }, this.config.checkInterval); } stop(): void { if (this.interval) { clearInterval(this.interval); this.interval = undefined; } this.running = false; } async getStatus(): Promise<IHealthStatus> { const components: Record<string, IComponentHealth> = {}; let unhealthyCount = 0; let degradedCount = 0; const checkPromises = Array.from(this.checks.entries()).map( async ([name, check]) => { try { const result = await Promise.race([ check(), this.timeout(5000, 'Health check timeout'), ]); const health: IComponentHealth = { name, status: result.healthy ? 'healthy' : 'unhealthy', lastCheck: new Date(), error: result.error, metrics: result.metrics, }; return { name, health }; } catch (error) { return { name, health: { name, status: 'unhealthy' as const, lastCheck: new Date(), error: error instanceof Error ? error.message : 'Unknown error', }, }; } }, ); const results = await Promise.allSettled(checkPromises); for (const result of results) { if (result.status === 'fulfilled') { const { name, health } = result.value; components[name] = health; if (health.status === 'unhealthy') { unhealthyCount++; } else if (health.status === 'degraded') { degradedCount++; } } } let overallStatus: IHealthStatus['status'] = 'healthy'; if (unhealthyCount >= this.config.unhealthyThreshold) { overallStatus = 'unhealthy'; } else if ( unhealthyCount > 0 || degradedCount >= this.config.degradedThreshold ) { overallStatus = 'degraded'; } return { status: overallStatus, components, timestamp: new Date(), }; } registerCheck(name: string, check: HealthCheckFn): void { this.checks.set(name, check); } unregisterCheck(name: string): void { this.checks.delete(name); } getHistory(limit?: number): IHealthStatus[] { const count = limit ?? this.config.historyLimit; return this.history.slice(-count); } onHealthChange(callback: (status: IHealthStatus) => void): () => void { this.listeners.push(callback); return () => { const index = this.listeners.indexOf(callback); if (index !== -1) { this.listeners.splice(index, 1); } }; } private addToHistory(status: IHealthStatus): void { this.history.push(status); // Trim history to limit if (this.history.length > this.config.historyLimit) { this.history = this.history.slice(-this.config.historyLimit); } } private notifyListeners(status: IHealthStatus): void { for (const listener of this.listeners) { try { listener(status); } catch { // Ignore listener errors } } } private timeout(ms: number, message: string): Promise<never> { return new Promise((_, reject) => { setTimeout(() => reject(new Error(message)), ms); }); } /** * Get component health by name */ async getComponentHealth(name: string): Promise<IComponentHealth | undefined> { const status = await this.getStatus(); return status.components[name]; } /** * Check if system is healthy */ async isHealthy(): Promise<boolean> { const status = await this.getStatus(); return status.status === 'healthy'; } /** * Get registered check names */ getRegisteredChecks(): string[] { return Array.from(this.checks.keys()); } } |