| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 1 | import { z } from "zod"; |
| 2 | |
| gio | 78a2288 | 2025-07-01 18:56:01 +0000 | [diff] [blame] | 3 | const LogItemSchema = z.object({ |
| 4 | runId: z.string(), |
| 5 | timestampMilli: z.number(), |
| 6 | commit: z.string().optional(), |
| 7 | contents: z.preprocess((val) => { |
| 8 | if (typeof val === "string") { |
| 9 | return Buffer.from(val, "base64").toString("utf-8"); |
| 10 | } |
| 11 | throw new Error("Log item contents is not a string"); |
| 12 | }, z.string()), |
| 13 | }); |
| 14 | |
| 15 | export type LogItem = z.infer<typeof LogItemSchema>; |
| 16 | |
| 17 | const LogItemsSchema = z.array(LogItemSchema); |
| 18 | |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 19 | export const WorkerSchema = z.object({ |
| 20 | id: z.string(), |
| 21 | service: z.string(), |
| 22 | address: z.string().url(), |
| 23 | status: z.optional( |
| 24 | z.object({ |
| gio | 0afbaee | 2025-05-22 04:34:33 +0000 | [diff] [blame] | 25 | commit: z.optional( |
| 26 | z.object({ |
| 27 | hash: z.string(), |
| 28 | message: z.string(), |
| 29 | }), |
| 30 | ), |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 31 | commands: z.optional( |
| 32 | z.array( |
| 33 | z.object({ |
| 34 | command: z.string(), |
| 35 | state: z.string(), |
| 36 | }), |
| 37 | ), |
| 38 | ), |
| 39 | }), |
| 40 | ), |
| gio | 78a2288 | 2025-07-01 18:56:01 +0000 | [diff] [blame] | 41 | logs: LogItemsSchema.optional(), |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 42 | }); |
| 43 | |
| 44 | export type Worker = z.infer<typeof WorkerSchema>; |
| 45 | |
| 46 | class ServiceMonitor { |
| 47 | private workers: Map<string, string> = new Map(); |
| gio | 78a2288 | 2025-07-01 18:56:01 +0000 | [diff] [blame] | 48 | private logs: Map<string, LogItem[]> = new Map(); |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 49 | private statuses: Map<string, Worker["status"]> = new Map(); |
| 50 | |
| 51 | constructor(public readonly serviceName: string) {} |
| 52 | |
| gio | 40c0c99 | 2025-07-02 13:18:05 +0000 | [diff] [blame^] | 53 | registerWorker(workerId: string, workerAddress: string, workerStatus?: Worker["status"]): void { |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 54 | this.workers.set(workerId, workerAddress); |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 55 | if (workerStatus) { |
| 56 | this.statuses.set(workerId, workerStatus); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | getWorkerAddress(workerId: string): string | undefined { |
| 61 | return this.workers.get(workerId); |
| 62 | } |
| 63 | |
| gio | 78a2288 | 2025-07-01 18:56:01 +0000 | [diff] [blame] | 64 | getWorkerLog(workerId: string): LogItem[] | undefined { |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 65 | return this.logs.get(workerId); |
| 66 | } |
| 67 | |
| 68 | getWorkerStatus(workerId: string): Worker["status"] | undefined { |
| 69 | return this.statuses.get(workerId); |
| 70 | } |
| 71 | |
| gio | 78a2288 | 2025-07-01 18:56:01 +0000 | [diff] [blame] | 72 | getAllLogs(): Map<string, LogItem[]> { |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 73 | return new Map(this.logs); |
| 74 | } |
| 75 | |
| 76 | getAllStatuses(): Map<string, Worker["status"]> { |
| 77 | return new Map(this.statuses); |
| 78 | } |
| 79 | |
| 80 | getWorkerAddresses(): string[] { |
| 81 | return Array.from(this.workers.values()); |
| 82 | } |
| 83 | |
| 84 | getWorkerIds(): string[] { |
| 85 | return Array.from(this.workers.keys()); |
| 86 | } |
| 87 | |
| 88 | hasLogs(): boolean { |
| 89 | return this.logs.size > 0; |
| 90 | } |
| gio | 918780d | 2025-05-22 08:24:41 +0000 | [diff] [blame] | 91 | |
| 92 | async reloadWorker(workerId: string): Promise<void> { |
| 93 | const workerAddress = this.workers.get(workerId); |
| 94 | if (!workerAddress) { |
| 95 | throw new Error(`Worker ${workerId} not found in service ${this.serviceName}`); |
| 96 | } |
| 97 | try { |
| 98 | const response = await fetch(`${workerAddress}/update`, { method: "POST" }); |
| 99 | if (!response.ok) { |
| 100 | throw new Error( |
| 101 | `Failed to trigger reload for worker ${workerId} at ${workerAddress}: ${response.statusText}`, |
| 102 | ); |
| 103 | } |
| 104 | console.log(`Reload triggered for worker ${workerId} in service ${this.serviceName}`); |
| 105 | } catch (error) { |
| 106 | console.error(`Error reloading worker ${workerId} in service ${this.serviceName}:`, error); |
| 107 | throw error; // Re-throw to be caught by ProjectMonitor |
| 108 | } |
| 109 | } |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | export class ProjectMonitor { |
| 113 | private serviceMonitors: Map<string, ServiceMonitor> = new Map(); |
| 114 | |
| 115 | constructor() {} |
| 116 | |
| 117 | registerWorker(workerData: Worker): void { |
| 118 | let serviceMonitor = this.serviceMonitors.get(workerData.service); |
| 119 | if (!serviceMonitor) { |
| 120 | serviceMonitor = new ServiceMonitor(workerData.service); |
| 121 | this.serviceMonitors.set(workerData.service, serviceMonitor); |
| 122 | } |
| gio | 40c0c99 | 2025-07-02 13:18:05 +0000 | [diff] [blame^] | 123 | serviceMonitor.registerWorker(workerData.id, workerData.address, workerData.status); |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | getWorkerAddresses(): string[] { |
| 127 | let allAddresses: string[] = []; |
| 128 | for (const serviceMonitor of this.serviceMonitors.values()) { |
| 129 | allAddresses = allAddresses.concat(serviceMonitor.getWorkerAddresses()); |
| 130 | } |
| 131 | return Array.from(new Set(allAddresses)); |
| 132 | } |
| 133 | |
| gio | 78a2288 | 2025-07-01 18:56:01 +0000 | [diff] [blame] | 134 | getWorkerLog(serviceName: string, workerId: string): LogItem[] | undefined { |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 135 | const serviceMonitor = this.serviceMonitors.get(serviceName); |
| 136 | if (serviceMonitor) { |
| 137 | return serviceMonitor.getWorkerLog(workerId); |
| 138 | } |
| 139 | return undefined; |
| 140 | } |
| 141 | |
| 142 | getAllServiceNames(): string[] { |
| 143 | return Array.from(this.serviceMonitors.keys()); |
| 144 | } |
| 145 | |
| 146 | hasLogs(): boolean { |
| 147 | for (const serviceMonitor of this.serviceMonitors.values()) { |
| 148 | if (serviceMonitor.hasLogs()) { |
| 149 | return true; |
| 150 | } |
| 151 | } |
| 152 | return false; |
| 153 | } |
| 154 | |
| 155 | getServiceMonitor(serviceName: string): ServiceMonitor | undefined { |
| 156 | return this.serviceMonitors.get(serviceName); |
| 157 | } |
| 158 | |
| 159 | getWorkerStatusesForService(serviceName: string): Map<string, Worker["status"]> { |
| 160 | const serviceMonitor = this.serviceMonitors.get(serviceName); |
| 161 | if (serviceMonitor) { |
| 162 | return serviceMonitor.getAllStatuses(); |
| 163 | } |
| 164 | return new Map(); |
| 165 | } |
| gio | 918780d | 2025-05-22 08:24:41 +0000 | [diff] [blame] | 166 | |
| 167 | async reloadWorker(serviceName: string, workerId: string): Promise<void> { |
| 168 | const serviceMonitor = this.serviceMonitors.get(serviceName); |
| 169 | if (!serviceMonitor) { |
| 170 | throw new Error(`Service ${serviceName} not found`); |
| 171 | } |
| 172 | await serviceMonitor.reloadWorker(workerId); |
| 173 | } |
| gio | a1efbad | 2025-05-21 07:16:45 +0000 | [diff] [blame] | 174 | } |