mirror of
https://github.com/logos-messaging/logos-messaging-js.git
synced 2026-05-05 12:23:06 +00:00
feat: setup events API for HealthManager
This commit is contained in:
parent
88e33a90fd
commit
32b9f83bbd
@ -1,4 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
|
HealthEvent,
|
||||||
|
HealthEventType,
|
||||||
|
HealthListener,
|
||||||
HealthStatus,
|
HealthStatus,
|
||||||
type IHealthManager,
|
type IHealthManager,
|
||||||
NodeHealth,
|
NodeHealth,
|
||||||
@ -9,12 +12,14 @@ import {
|
|||||||
class HealthManager implements IHealthManager {
|
class HealthManager implements IHealthManager {
|
||||||
public static instance: HealthManager;
|
public static instance: HealthManager;
|
||||||
private readonly health: NodeHealth;
|
private readonly health: NodeHealth;
|
||||||
|
private listeners: Map<HealthEventType, Set<HealthListener>>;
|
||||||
|
|
||||||
private constructor() {
|
private constructor() {
|
||||||
this.health = {
|
this.health = {
|
||||||
overallStatus: HealthStatus.Unhealthy,
|
overallStatus: HealthStatus.Unhealthy,
|
||||||
protocolStatuses: new Map()
|
protocolStatuses: new Map()
|
||||||
};
|
};
|
||||||
|
this.listeners = new Map();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static getInstance(): HealthManager {
|
public static getInstance(): HealthManager {
|
||||||
@ -51,9 +56,37 @@ class HealthManager implements IHealthManager {
|
|||||||
lastUpdate: new Date()
|
lastUpdate: new Date()
|
||||||
});
|
});
|
||||||
|
|
||||||
|
this.emitEvent({
|
||||||
|
type: "health:protocol",
|
||||||
|
status,
|
||||||
|
protocol,
|
||||||
|
timestamp: new Date()
|
||||||
|
});
|
||||||
|
|
||||||
this.updateOverallHealth();
|
this.updateOverallHealth();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public addEventListener(
|
||||||
|
type: HealthEventType,
|
||||||
|
listener: HealthListener
|
||||||
|
): void {
|
||||||
|
if (!this.listeners.has(type)) {
|
||||||
|
this.listeners.set(type, new Set());
|
||||||
|
}
|
||||||
|
this.listeners.get(type)?.add(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public removeEventListener(
|
||||||
|
type: HealthEventType,
|
||||||
|
listener: HealthListener
|
||||||
|
): void {
|
||||||
|
this.listeners.get(type)?.delete(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
private emitEvent(event: HealthEvent): void {
|
||||||
|
this.listeners.get(event.type)?.forEach((listener) => listener(event));
|
||||||
|
}
|
||||||
|
|
||||||
private getNameFromMulticodec(multicodec: string): Protocols {
|
private getNameFromMulticodec(multicodec: string): Protocols {
|
||||||
let name: Protocols;
|
let name: Protocols;
|
||||||
if (multicodec.includes("filter")) {
|
if (multicodec.includes("filter")) {
|
||||||
@ -74,14 +107,24 @@ class HealthManager implements IHealthManager {
|
|||||||
(p) => this.getProtocolStatus(p)?.status
|
(p) => this.getProtocolStatus(p)?.status
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let newStatus: HealthStatus;
|
||||||
if (statuses.some((status) => status === HealthStatus.Unhealthy)) {
|
if (statuses.some((status) => status === HealthStatus.Unhealthy)) {
|
||||||
this.health.overallStatus = HealthStatus.Unhealthy;
|
newStatus = HealthStatus.Unhealthy;
|
||||||
} else if (
|
} else if (
|
||||||
statuses.some((status) => status === HealthStatus.MinimallyHealthy)
|
statuses.some((status) => status === HealthStatus.MinimallyHealthy)
|
||||||
) {
|
) {
|
||||||
this.health.overallStatus = HealthStatus.MinimallyHealthy;
|
newStatus = HealthStatus.MinimallyHealthy;
|
||||||
} else {
|
} else {
|
||||||
this.health.overallStatus = HealthStatus.SufficientlyHealthy;
|
newStatus = HealthStatus.SufficientlyHealthy;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.health.overallStatus !== newStatus) {
|
||||||
|
this.health.overallStatus = newStatus;
|
||||||
|
this.emitEvent({
|
||||||
|
type: "health:overall",
|
||||||
|
status: newStatus,
|
||||||
|
timestamp: new Date()
|
||||||
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,10 +6,27 @@ export enum HealthStatus {
|
|||||||
SufficientlyHealthy = "SufficientlyHealthy"
|
SufficientlyHealthy = "SufficientlyHealthy"
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type HealthEventType = "health:overall" | "health:protocol";
|
||||||
|
|
||||||
|
export interface HealthEvent {
|
||||||
|
type: HealthEventType;
|
||||||
|
status: HealthStatus;
|
||||||
|
protocol?: Protocols;
|
||||||
|
timestamp: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export type HealthListener = (event: HealthEvent) => void;
|
||||||
|
|
||||||
export interface IHealthManager {
|
export interface IHealthManager {
|
||||||
getHealthStatus: () => HealthStatus;
|
getHealthStatus: () => HealthStatus;
|
||||||
getProtocolStatus: (protocol: Protocols) => ProtocolHealth | undefined;
|
getProtocolStatus: (protocol: Protocols) => ProtocolHealth | undefined;
|
||||||
updateProtocolHealth: (multicodec: string, connectedPeers: number) => void;
|
updateProtocolHealth: (multicodec: string, connectedPeers: number) => void;
|
||||||
|
|
||||||
|
addEventListener: (type: HealthEventType, listener: HealthListener) => void;
|
||||||
|
removeEventListener: (
|
||||||
|
type: HealthEventType,
|
||||||
|
listener: HealthListener
|
||||||
|
) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
export type NodeHealth = {
|
export type NodeHealth = {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user