|
| 1 | +import type { ILoggerComponent, IConfigComponent, IBaseComponent } from '@well-known-components/interfaces' |
| 2 | +import type { IFetchComponent } from '@well-known-components/http-server' |
| 3 | +import crypto from 'crypto' |
| 4 | + |
| 5 | +export interface IMonitoringReporter extends IBaseComponent { |
| 6 | + reportHeartbeat(data: HeartbeatData): void |
| 7 | + reportJobComplete(data: JobCompleteData): void |
| 8 | + getConsumerId(): string |
| 9 | +} |
| 10 | + |
| 11 | +export interface HeartbeatData { |
| 12 | + status: 'idle' | 'processing' |
| 13 | + currentSceneId?: string |
| 14 | + currentStep?: string |
| 15 | + progressPercent?: number |
| 16 | + startedAt?: string |
| 17 | +} |
| 18 | + |
| 19 | +export interface JobCompleteData { |
| 20 | + sceneId: string |
| 21 | + status: 'success' | 'failed' |
| 22 | + startedAt: string |
| 23 | + completedAt: string |
| 24 | + durationMs: number |
| 25 | + errorMessage?: string |
| 26 | +} |
| 27 | + |
| 28 | +interface MonitoringReporterComponents { |
| 29 | + logs: ILoggerComponent |
| 30 | + config: IConfigComponent |
| 31 | + fetch: IFetchComponent |
| 32 | +} |
| 33 | + |
| 34 | +export function createMonitoringReporter( |
| 35 | + components: MonitoringReporterComponents, |
| 36 | + processMethod: string |
| 37 | +): IMonitoringReporter { |
| 38 | + const { logs, config, fetch } = components |
| 39 | + const logger = logs.getLogger('monitoring-reporter') |
| 40 | + |
| 41 | + const consumerId = crypto.randomUUID() |
| 42 | + let monitoringUrl: string | undefined |
| 43 | + let monitoringSecret: string | undefined |
| 44 | + let heartbeatInterval: NodeJS.Timeout | undefined |
| 45 | + let currentHeartbeatData: HeartbeatData = { status: 'idle' } |
| 46 | + let isRunning = false |
| 47 | + |
| 48 | + async function initConfig() { |
| 49 | + monitoringUrl = await config.getString('MONITORING_URL') |
| 50 | + monitoringSecret = await config.getString('MONITORING_SECRET') |
| 51 | + |
| 52 | + if (!monitoringUrl || !monitoringSecret) { |
| 53 | + logger.info('Monitoring not configured (MONITORING_URL or MONITORING_SECRET missing)') |
| 54 | + } else { |
| 55 | + logger.info('Monitoring configured', { consumerId, monitoringUrl }) |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + async function report(endpoint: string, data: object): Promise<void> { |
| 60 | + if (!monitoringUrl || !monitoringSecret) { |
| 61 | + return |
| 62 | + } |
| 63 | + |
| 64 | + try { |
| 65 | + const url = `${monitoringUrl}${endpoint}` |
| 66 | + const controller = new AbortController() |
| 67 | + const timeoutId = setTimeout(() => controller.abort(), 5000) |
| 68 | + |
| 69 | + await fetch.fetch(url, { |
| 70 | + method: 'POST', |
| 71 | + headers: { 'Content-Type': 'application/json' }, |
| 72 | + body: JSON.stringify({ ...data, secret: monitoringSecret }), |
| 73 | + signal: controller.signal |
| 74 | + }) |
| 75 | + |
| 76 | + clearTimeout(timeoutId) |
| 77 | + } catch (error) { |
| 78 | + // Silently ignore - monitoring should never block pipeline |
| 79 | + logger.debug('Monitoring report failed (non-blocking)', { |
| 80 | + error: error instanceof Error ? error.message : 'Unknown error' |
| 81 | + }) |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + function sendHeartbeat() { |
| 86 | + report('/api/monitoring/heartbeat', { |
| 87 | + consumerId, |
| 88 | + processMethod, |
| 89 | + ...currentHeartbeatData |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + function startHeartbeat() { |
| 94 | + if (heartbeatInterval) { |
| 95 | + return |
| 96 | + } |
| 97 | + |
| 98 | + // Send initial heartbeat |
| 99 | + sendHeartbeat() |
| 100 | + |
| 101 | + // Set up interval (every 10 seconds) |
| 102 | + heartbeatInterval = setInterval(sendHeartbeat, 10000) |
| 103 | + } |
| 104 | + |
| 105 | + function stopHeartbeat() { |
| 106 | + if (heartbeatInterval) { |
| 107 | + clearInterval(heartbeatInterval) |
| 108 | + heartbeatInterval = undefined |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + return { |
| 113 | + async start() { |
| 114 | + await initConfig() |
| 115 | + isRunning = true |
| 116 | + startHeartbeat() |
| 117 | + }, |
| 118 | + |
| 119 | + async stop() { |
| 120 | + isRunning = false |
| 121 | + stopHeartbeat() |
| 122 | + }, |
| 123 | + |
| 124 | + getConsumerId() { |
| 125 | + return consumerId |
| 126 | + }, |
| 127 | + |
| 128 | + reportHeartbeat(data: HeartbeatData) { |
| 129 | + currentHeartbeatData = data |
| 130 | + // Heartbeat will be sent on next interval, but also send immediately for status changes |
| 131 | + if (isRunning) { |
| 132 | + sendHeartbeat() |
| 133 | + } |
| 134 | + }, |
| 135 | + |
| 136 | + reportJobComplete(data: JobCompleteData) { |
| 137 | + report('/api/monitoring/job-complete', { |
| 138 | + consumerId, |
| 139 | + processMethod, |
| 140 | + ...data |
| 141 | + }) |
| 142 | + |
| 143 | + // Reset heartbeat data to idle |
| 144 | + currentHeartbeatData = { status: 'idle' } |
| 145 | + } |
| 146 | + } |
| 147 | +} |
0 commit comments