mirror of
https://github.com/remix-bot/fluxer.git
synced 2026-11-23 21:52:45 -05:00
fix(gateway): implement gateway watchdog, socket close tracking, and self-healing reconnects (#12)
This commit is contained in:
1 parent
08634adb88
commit
26aed399fc
3 files changed
+94
-4
No files matched your search
@@ -128,10 +128,14 @@ manager.on("error", (err) => {
|
|||||||
logger.error("[Sharding] Manager error:", err?.message ?? err);
|
logger.error("[Sharding] Manager error:", err?.message ?? err);
|
||||||
});
|
});
|
||||||
|
|
||||||
// Event loop health heartbeat for Docker container healthcheck (Issue #12)
|
/* Event loop health heartbeat for Docker container healthcheck (Issue #12) */
|
||||||
const healthFile = path.join(os.tmpdir(), ".remix-health");
|
const healthFile = path.join(os.tmpdir(), ".remix-health");
|
||||||
const updateHealth = () => {
|
const updateHealth = () => {
|
||||||
try {
|
try {
|
||||||
|
if (manager.shards && manager.shards.size > 0) {
|
||||||
|
const anyAlive = [...manager.shards.values()].some(s => s.process && !s.process.killed);
|
||||||
|
if (!anyAlive) return;
|
||||||
|
}
|
||||||
fs.writeFile(healthFile, Date.now().toString(), () => {});
|
fs.writeFile(healthFile, Date.now().toString(), () => {});
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
};
|
};
|
||||||
|
|||||||
+40
-3
@@ -271,17 +271,53 @@ class Remix {
|
|||||||
this.fluxerlist = new FluxerListManager(config.fluxerlist);
|
this.fluxerlist = new FluxerListManager(config.fluxerlist);
|
||||||
this.trackOptions = new TrackOptionsManager(sharedPool);
|
this.trackOptions = new TrackOptionsManager(sharedPool);
|
||||||
|
|
||||||
// Event loop health heartbeat for Docker container healthcheck (Issue #12)
|
/* Gateway activity timestamps and status tracking */
|
||||||
|
this._lastGatewayHeartbeat = Date.now();
|
||||||
|
this._gatewayConnected = false;
|
||||||
|
|
||||||
|
/* Event loop health heartbeat for Docker container healthcheck (Issue #12) */
|
||||||
const healthFile = path.join(os.tmpdir(), ".remix-health");
|
const healthFile = path.join(os.tmpdir(), ".remix-health");
|
||||||
const updateHealth = () => {
|
const updateHealth = () => {
|
||||||
try {
|
try {
|
||||||
fs.writeFile(healthFile, Date.now().toString(), () => {});
|
/* Only touch health file if gateway is healthy or still in initial boot */
|
||||||
|
const silenceMs = this._hasBooted ? Date.now() - (this._lastGatewayHeartbeat || 0) : 0;
|
||||||
|
if (silenceMs < 90_000) {
|
||||||
|
fs.writeFile(healthFile, Date.now().toString(), () => {});
|
||||||
|
}
|
||||||
} catch (_) {}
|
} catch (_) {}
|
||||||
};
|
};
|
||||||
updateHealth();
|
updateHealth();
|
||||||
this._healthInterval = setInterval(updateHealth, 10_000);
|
this._healthInterval = setInterval(updateHealth, 10_000);
|
||||||
this._healthInterval.unref?.();
|
this._healthInterval.unref?.();
|
||||||
|
|
||||||
|
/* Active gateway self-healing watchdog */
|
||||||
|
this._gatewayWatchdogInterval = setInterval(async () => {
|
||||||
|
if (!this._hasBooted) return;
|
||||||
|
const silenceMs = Date.now() - (this._lastGatewayHeartbeat || 0);
|
||||||
|
|
||||||
|
/* After 45s of silence, actively trigger reconnect attempts on local shards */
|
||||||
|
if (silenceMs >= 45_000) {
|
||||||
|
const shards = ShardingUtils.getLocalShards(this.client);
|
||||||
|
for (const [id, shard] of shards) {
|
||||||
|
try {
|
||||||
|
if (typeof shard.connect === "function") shard.connect();
|
||||||
|
else if (typeof shard.reconnect === "function") shard.reconnect();
|
||||||
|
else if (typeof shard.ws?.connect === "function") shard.ws.connect();
|
||||||
|
} catch (_) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* After 120s of total gateway silence (dead/zombie socket), perform graceful restart to auto-recover */
|
||||||
|
if (silenceMs >= 120_000) {
|
||||||
|
logger.error(`[GatewayWatchdog] Gateway unresponsive for ${Math.round(silenceMs / 1000)}s — performing graceful restart to restore service.`);
|
||||||
|
try {
|
||||||
|
await this.gracefulShutdown();
|
||||||
|
} catch (_) {}
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
|
}, 15_000);
|
||||||
|
this._gatewayWatchdogInterval.unref?.();
|
||||||
|
|
||||||
// Direct process signal fallback handlers
|
// Direct process signal fallback handlers
|
||||||
process.once("SIGINT", () => {
|
process.once("SIGINT", () => {
|
||||||
this.gracefulShutdown().then(() => process.exit(0)).catch(() => process.exit(1));
|
this.gracefulShutdown().then(() => process.exit(0)).catch(() => process.exit(1));
|
||||||
@@ -737,8 +773,9 @@ class Remix {
|
|||||||
logger.warn("[Shutdown] MySQL pool close failed:", e?.message);
|
logger.warn("[Shutdown] MySQL pool close failed:", e?.message);
|
||||||
}
|
}
|
||||||
|
|
||||||
// 9. Clean up event-loop health heartbeat
|
/* 9. Clean up event-loop health heartbeat and gateway watchdog */
|
||||||
try {
|
try {
|
||||||
|
if (this._gatewayWatchdogInterval) clearInterval(this._gatewayWatchdogInterval);
|
||||||
if (this._healthInterval) clearInterval(this._healthInterval);
|
if (this._healthInterval) clearInterval(this._healthInterval);
|
||||||
const healthFile = path.join(os.tmpdir(), ".remix-health");
|
const healthFile = path.join(os.tmpdir(), ".remix-health");
|
||||||
fs.unlink(healthFile, () => {});
|
fs.unlink(healthFile, () => {});
|
||||||
|
|||||||
@@ -132,9 +132,11 @@ class GatewayHandler {
|
|||||||
if (typeof old.removeEventListener === "function") {
|
if (typeof old.removeEventListener === "function") {
|
||||||
old.removeEventListener("message", remix._rawGatewayMessageListener ?? remix._rawGatewayHandler);
|
old.removeEventListener("message", remix._rawGatewayMessageListener ?? remix._rawGatewayHandler);
|
||||||
old.removeEventListener("error", remix._rawGatewayErrorHandler);
|
old.removeEventListener("error", remix._rawGatewayErrorHandler);
|
||||||
|
old.removeEventListener("close", remix._rawGatewayCloseHandler);
|
||||||
} else if (typeof old.off === "function") {
|
} else if (typeof old.off === "function") {
|
||||||
old.off("message", remix._rawGatewayMessageListener ?? remix._rawGatewayHandler);
|
old.off("message", remix._rawGatewayMessageListener ?? remix._rawGatewayHandler);
|
||||||
old.off("error", remix._rawGatewayErrorHandler);
|
old.off("error", remix._rawGatewayErrorHandler);
|
||||||
|
old.off("close", remix._rawGatewayCloseHandler);
|
||||||
}
|
}
|
||||||
} catch(e) { logger.warn("[Gateway] Failed to detach old WS listener:", e?.message); }
|
} catch(e) { logger.warn("[Gateway] Failed to detach old WS listener:", e?.message); }
|
||||||
attached.delete(old);
|
attached.delete(old);
|
||||||
@@ -145,6 +147,8 @@ class GatewayHandler {
|
|||||||
if (!remix._rawGatewayHandler) {
|
if (!remix._rawGatewayHandler) {
|
||||||
remix._rawGatewayHandler = (data) => {
|
remix._rawGatewayHandler = (data) => {
|
||||||
try {
|
try {
|
||||||
|
remix._lastGatewayHeartbeat = Date.now();
|
||||||
|
remix._gatewayConnected = true;
|
||||||
const client = remix.client;
|
const client = remix.client;
|
||||||
if (typeof data === "string" && !RAW_GATEWAY_INTEREST.test(data)) return;
|
if (typeof data === "string" && !RAW_GATEWAY_INTEREST.test(data)) return;
|
||||||
const payload = typeof data === "string" ? JSON.parse(data) : data;
|
const payload = typeof data === "string" ? JSON.parse(data) : data;
|
||||||
@@ -167,6 +171,9 @@ class GatewayHandler {
|
|||||||
|
|
||||||
if (payload.t === "READY") {
|
if (payload.t === "READY") {
|
||||||
logger.player(`[Gateway] READY received (session ${payload.d?.session_id ?? "unknown"})`);
|
logger.player(`[Gateway] READY received (session ${payload.d?.session_id ?? "unknown"})`);
|
||||||
|
this._lastReadyAt = Date.now();
|
||||||
|
remix._lastGatewayHeartbeat = Date.now();
|
||||||
|
remix._gatewayConnected = true;
|
||||||
const readyGuilds = payload.d?.guilds;
|
const readyGuilds = payload.d?.guilds;
|
||||||
if (Array.isArray(readyGuilds)) {
|
if (Array.isArray(readyGuilds)) {
|
||||||
for (const g of readyGuilds) {
|
for (const g of readyGuilds) {
|
||||||
@@ -181,10 +188,41 @@ class GatewayHandler {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (remix._hasBooted) {
|
||||||
|
logger.voice247("[Gateway] Re-READY after gateway reconnection — scheduling 24/7 self-healing sweep in 3s...");
|
||||||
|
setTimeout(() => {
|
||||||
|
if (typeof this._sweep247Channels === "function") {
|
||||||
|
this._sweep247Channels().catch(err => {
|
||||||
|
logger.warn("[Watchdog247] Post-reconnect sweep failed:", err?.message ?? err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (remix.players?.resumeAllOnNodeReconnect) {
|
||||||
|
remix.players.resumeAllOnNodeReconnect();
|
||||||
|
}
|
||||||
|
}, 3_000).unref?.();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (payload.t === "RESUMED") {
|
if (payload.t === "RESUMED") {
|
||||||
logger.player("[Gateway] RESUMED received from gateway.");
|
logger.player("[Gateway] RESUMED received from gateway.");
|
||||||
|
this._lastReadyAt = Date.now();
|
||||||
|
remix._lastGatewayHeartbeat = Date.now();
|
||||||
|
remix._gatewayConnected = true;
|
||||||
|
|
||||||
|
if (remix._hasBooted) {
|
||||||
|
logger.voice247("[Gateway] RESUMED session active — scheduling 24/7 self-healing sweep in 3s...");
|
||||||
|
setTimeout(() => {
|
||||||
|
if (typeof this._sweep247Channels === "function") {
|
||||||
|
this._sweep247Channels().catch(err => {
|
||||||
|
logger.warn("[Watchdog247] Post-resume sweep failed:", err?.message ?? err);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
if (remix.players?.resumeAllOnNodeReconnect) {
|
||||||
|
remix.players.resumeAllOnNodeReconnect();
|
||||||
|
}
|
||||||
|
}, 3_000).unref?.();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (payload.t === "GUILD_CREATE") {
|
if (payload.t === "GUILD_CREATE") {
|
||||||
@@ -230,6 +268,15 @@ class GatewayHandler {
|
|||||||
_wsErrorCooldown.lastLogged = now;
|
_wsErrorCooldown.lastLogged = now;
|
||||||
logger.warn("[Gateway] Raw WS socket error (will reconnect):", err?.message ?? err);
|
logger.warn("[Gateway] Raw WS socket error (will reconnect):", err?.message ?? err);
|
||||||
};
|
};
|
||||||
|
remix._rawGatewayCloseHandler = (eventOrCode, maybeReason) => {
|
||||||
|
const code = eventOrCode?.code ?? (typeof eventOrCode === "number" ? eventOrCode : "unknown");
|
||||||
|
const reason = eventOrCode?.reason ?? maybeReason ?? "";
|
||||||
|
remix._gatewayConnected = false;
|
||||||
|
const now = Date.now();
|
||||||
|
if (now - _wsErrorCooldown.lastLogged < _wsErrorCooldown.COOLDOWN_MS) return;
|
||||||
|
_wsErrorCooldown.lastLogged = now;
|
||||||
|
logger.warn(`[Gateway] Raw WebSocket closed (code=${code}, reason=${reason || "none"}) — watchdog tracking reconnect.`);
|
||||||
|
};
|
||||||
remix._rawGatewayMessageListener = (event) => remix._rawGatewayHandler(event?.data ?? event);
|
remix._rawGatewayMessageListener = (event) => remix._rawGatewayHandler(event?.data ?? event);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -239,9 +286,11 @@ class GatewayHandler {
|
|||||||
if (typeof wsObj.addEventListener === "function") {
|
if (typeof wsObj.addEventListener === "function") {
|
||||||
wsObj.addEventListener("message", remix._rawGatewayMessageListener);
|
wsObj.addEventListener("message", remix._rawGatewayMessageListener);
|
||||||
wsObj.addEventListener("error", remix._rawGatewayErrorHandler);
|
wsObj.addEventListener("error", remix._rawGatewayErrorHandler);
|
||||||
|
wsObj.addEventListener("close", remix._rawGatewayCloseHandler);
|
||||||
} else if (typeof wsObj.on === "function") {
|
} else if (typeof wsObj.on === "function") {
|
||||||
wsObj.on("message", remix._rawGatewayMessageListener);
|
wsObj.on("message", remix._rawGatewayMessageListener);
|
||||||
wsObj.on("error", remix._rawGatewayErrorHandler);
|
wsObj.on("error", remix._rawGatewayErrorHandler);
|
||||||
|
wsObj.on("close", remix._rawGatewayCloseHandler);
|
||||||
}
|
}
|
||||||
|
|
||||||
attached.add(wsObj);
|
attached.add(wsObj);
|
||||||
|
|||||||
Reference in new issue
Block a user