Skip to content

Commit

Permalink
add summary event
Browse files Browse the repository at this point in the history
  • Loading branch information
0oM4R committed Jan 22, 2024
1 parent 1f2329c commit 7b5f868
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/monitoring/src/helpers/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,36 @@ 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);
}

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 };
6 changes: 5 additions & 1 deletion packages/monitoring/src/serviceMonitor/alivenessChecker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}

0 comments on commit 7b5f868

Please sign in to comment.