2026-01-24 19:49:42 -06:00
|
|
|
type EventListener = (...args: any[]) => void;
|
|
|
|
|
|
|
|
|
|
class DatabaseHealthMonitor {
|
|
|
|
|
private static instance: DatabaseHealthMonitor;
|
|
|
|
|
private dbHealthy: boolean = true;
|
|
|
|
|
private lastCheckTime: number = 0;
|
|
|
|
|
private checkInProgress: boolean = false;
|
|
|
|
|
private listeners: Map<string, EventListener[]> = new Map();
|
2026-02-12 22:28:13 -06:00
|
|
|
private consecutiveErrorTimer: ReturnType<typeof setTimeout> | null = null;
|
|
|
|
|
private confirmedUnhealthy: boolean = false;
|
2026-01-24 19:49:42 -06:00
|
|
|
|
|
|
|
|
private constructor() {}
|
|
|
|
|
|
|
|
|
|
static getInstance(): DatabaseHealthMonitor {
|
|
|
|
|
if (!DatabaseHealthMonitor.instance) {
|
|
|
|
|
DatabaseHealthMonitor.instance = new DatabaseHealthMonitor();
|
|
|
|
|
}
|
|
|
|
|
return DatabaseHealthMonitor.instance;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
on(event: string, listener: EventListener): void {
|
|
|
|
|
if (!this.listeners.has(event)) {
|
|
|
|
|
this.listeners.set(event, []);
|
|
|
|
|
}
|
|
|
|
|
this.listeners.get(event)!.push(listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
off(event: string, listener: EventListener): void {
|
|
|
|
|
const eventListeners = this.listeners.get(event);
|
|
|
|
|
if (eventListeners) {
|
|
|
|
|
const index = eventListeners.indexOf(listener);
|
|
|
|
|
if (index !== -1) {
|
|
|
|
|
eventListeners.splice(index, 1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private emit(event: string, ...args: any[]): void {
|
|
|
|
|
const eventListeners = this.listeners.get(event);
|
|
|
|
|
if (eventListeners) {
|
|
|
|
|
eventListeners.forEach((listener) => listener(...args));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-02-12 22:28:13 -06:00
|
|
|
reportSessionExpired() {
|
|
|
|
|
this.emit("session-expired", { timestamp: Date.now() });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reportDatabaseError(error: any, _wasAuthenticated: boolean = false) {
|
2026-01-24 19:49:42 -06:00
|
|
|
const errorMessage = error?.response?.data?.error || error?.message || "";
|
|
|
|
|
const errorCode = error?.response?.data?.code || error?.code;
|
|
|
|
|
|
|
|
|
|
const isDatabaseError =
|
|
|
|
|
errorMessage.toLowerCase().includes("database") ||
|
|
|
|
|
errorMessage.toLowerCase().includes("sqlite") ||
|
|
|
|
|
errorMessage.toLowerCase().includes("drizzle") ||
|
|
|
|
|
errorCode === "DATABASE_ERROR" ||
|
|
|
|
|
errorCode === "DB_CONNECTION_FAILED";
|
|
|
|
|
|
|
|
|
|
const isBackendUnreachable =
|
|
|
|
|
errorCode === "ERR_NETWORK" ||
|
|
|
|
|
errorCode === "ECONNREFUSED" ||
|
|
|
|
|
(errorMessage.toLowerCase().includes("network error") &&
|
|
|
|
|
error?.response === undefined);
|
|
|
|
|
|
2026-02-12 22:28:13 -06:00
|
|
|
if (!(isDatabaseError || isBackendUnreachable)) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2026-01-24 19:49:42 -06:00
|
|
|
|
2026-02-12 22:28:13 -06:00
|
|
|
if (this.dbHealthy && !this.consecutiveErrorTimer) {
|
|
|
|
|
this.consecutiveErrorTimer = setTimeout(() => {
|
|
|
|
|
this.consecutiveErrorTimer = null;
|
|
|
|
|
if (this.dbHealthy) {
|
|
|
|
|
this.dbHealthy = false;
|
|
|
|
|
this.confirmedUnhealthy = true;
|
|
|
|
|
this.emit("database-connection-lost", {
|
|
|
|
|
error: errorMessage || "Backend server unreachable",
|
|
|
|
|
code: errorCode,
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}, 10000);
|
2026-01-24 19:49:42 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reportDatabaseSuccess() {
|
2026-02-12 22:28:13 -06:00
|
|
|
this.clearErrorTimer();
|
|
|
|
|
|
|
|
|
|
if (this.confirmedUnhealthy) {
|
2026-01-24 19:49:42 -06:00
|
|
|
this.dbHealthy = true;
|
2026-02-12 22:28:13 -06:00
|
|
|
this.confirmedUnhealthy = false;
|
2026-01-24 19:49:42 -06:00
|
|
|
this.emit("database-connection-restored", {
|
|
|
|
|
timestamp: Date.now(),
|
|
|
|
|
});
|
2026-02-12 22:28:13 -06:00
|
|
|
} else if (!this.dbHealthy) {
|
|
|
|
|
this.dbHealthy = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private clearErrorTimer(): void {
|
|
|
|
|
if (this.consecutiveErrorTimer !== null) {
|
|
|
|
|
clearTimeout(this.consecutiveErrorTimer);
|
|
|
|
|
this.consecutiveErrorTimer = null;
|
2026-01-24 19:49:42 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
isDatabaseHealthy(): boolean {
|
|
|
|
|
return this.dbHealthy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
reset() {
|
|
|
|
|
this.dbHealthy = true;
|
2026-02-12 22:28:13 -06:00
|
|
|
this.confirmedUnhealthy = false;
|
2026-01-24 19:49:42 -06:00
|
|
|
this.lastCheckTime = 0;
|
|
|
|
|
this.checkInProgress = false;
|
2026-02-12 22:28:13 -06:00
|
|
|
this.clearErrorTimer();
|
2026-01-24 19:49:42 -06:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const dbHealthMonitor = DatabaseHealthMonitor.getInstance();
|