From 7b5f868ef3225d729c99744ac6823b51af825f96 Mon Sep 17 00:00:00 2001 From: kassem Date: Mon, 22 Jan 2024 16:14:02 +0200 Subject: [PATCH] add summary event --- packages/monitoring/src/helpers/events.ts | 21 +++++++++++++++++++ .../src/serviceMonitor/alivenessChecker.ts | 6 +++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/packages/monitoring/src/helpers/events.ts b/packages/monitoring/src/helpers/events.ts index f82bde913b..c4f199965f 100644 --- a/packages/monitoring/src/helpers/events.ts +++ b/packages/monitoring/src/helpers/events.ts @@ -3,7 +3,11 @@ import { EventEmitter } from "events"; import { TextColor } from "../types"; import { colorizeText } from "./utils"; const events = new EventEmitter(); +type ServiceStatus = { [key: string]: boolean }; +const serviceStatusSummary: ServiceStatus = {}; +const ALIVE = colorizeText("Alive", TextColor.Green); +const DOWN = colorizeText("Down", TextColor.Red); function logsHandler(msg) { console.log(msg); } @@ -11,7 +15,24 @@ function logsHandler(msg) { function serviceDownHandler(serviceName: string, error: Error) { console.log(`${colorizeText(serviceName + " is Down", TextColor.Red)}`); console.log(colorizeText("* Error: " + error.message, TextColor.Gray)); + serviceStatusSummary[serviceName] = false; +} + +function addToServiceSummary(serviceName: string, serviceIsAlive: boolean) { + serviceStatusSummary[serviceName] = serviceIsAlive; +} + +function printStatusSummary() { + const serviceNames = Object.keys(serviceStatusSummary); + const maxServiceNameLength = Math.max(...serviceNames.map(entry => entry.length)); + console.log(colorizeText("Aliveness check summary:", TextColor.Blue)); + for (const service in serviceStatusSummary) { + const padding = " ".repeat(maxServiceNameLength - service.length); + console.log(`\t${service}${padding}: ${serviceStatusSummary[service] ? ALIVE : DOWN}`); + } } events.addListener("logs", logsHandler); events.addListener("serviceIsDown", serviceDownHandler); +events.addListener("storeServiceStatus", addToServiceSummary); +events.addListener("summarize", printStatusSummary); export { events }; diff --git a/packages/monitoring/src/serviceMonitor/alivenessChecker.ts b/packages/monitoring/src/serviceMonitor/alivenessChecker.ts index 1572d38645..84f83df75e 100644 --- a/packages/monitoring/src/serviceMonitor/alivenessChecker.ts +++ b/packages/monitoring/src/serviceMonitor/alivenessChecker.ts @@ -29,11 +29,15 @@ export async function checkServiceAliveness(services: IServiceAliveness[], retri for (const service of services) { for (let retryCount = 1; retryCount <= retries; retryCount++) { const { alive, error } = await service.isAlive(); - if (alive) break; + if (alive) { + events.emit("storeServiceStatus", service.ServiceName, alive); + break; + } if (retryCount < retries) { events.emit("logs", `${service.ServiceName} seems to be down; Retrying (${retryCount}/${retries})...`); await new Promise(resolve => setTimeout(resolve, retryInterval * 60)); } else events.emit("serviceIsDown", service.ServiceName, error); } } + events.emit("summarize"); }