Optimize MySQL connection pools & add Docker healthcheck with graceful queue flush (#21, #12)

- Unify MySQL pools in src/db/PoolManager.mjs shared across Settings, TrackOptions, LastFm, Dashboard, and PlayerStateStore
- Support global config.mysql.connectionLimit (default 15), preventing connection starvation
- Add gracefulShutdown() in Bot.mjs to flush active player queues and playback positions to MySQL on SIGTERM/SIGINT
- Update shard.mjs to forward SIGTERM and await shard worker queue flushes before exit
- Add event-loop responsiveness healthcheck in docker-compose.yml and Dockerfile via heartbeat probe
- Document connectionLimit and queue persistence in README and config templates
This commit is contained in:
NoLogicAlan committed 2026-10-22 01:37:42 +03:00
1 parent 665be29a16
commit 90a53f047f
13 files changed
+303 -84

No files matched your search

+44 -2
View File
@@ -38,6 +38,8 @@
*/
import * as fs from "node:fs";
import os from "node:os";
import path from "node:path";
import { fileURLToPath } from "node:url";
import { ShardingManager } from "@fluxerjs/sharding";
import { logger } from "./src/core/Logger.mjs";
@@ -126,18 +128,58 @@ manager.on("error", (err) => {
logger.error("[Sharding] Manager error:", err?.message ?? err);
});
/** Graceful shutdown: destroy all children, then exit.
// Event loop health heartbeat for Docker container healthcheck (Issue #12)
const healthFile = path.join(os.tmpdir(), ".remix-health");
const updateHealth = () => {
try {
fs.writeFile(healthFile, Date.now().toString(), () => {});
} catch (_) {}
};
updateHealth();
const healthInterval = setInterval(updateHealth, 10_000);
healthInterval.unref?.();
/** Graceful shutdown: signal shard children to flush queue snapshots, await exit, then destroy.
* @param {string} sig - The received signal name. */
let shuttingDown = false;
const shutdown = async (sig) => {
if (shuttingDown) return;
shuttingDown = true;
logger.recovery(`\n[Sharding] ${sig} received — destroying shard processes...`);
manager.respawn = false;
logger.recovery(`\n[Sharding] ${sig} received — waiting for shard processes to flush queue snapshots and exit...`);
// Send SIGTERM to all child processes so their saveAndExit / Bot.gracefulShutdown runs
const exitPromises = [];
if (manager.shards) {
for (const shard of manager.shards.values()) {
const cp = shard.process;
if (cp && !cp.killed) {
exitPromises.push(new Promise((resolve) => {
cp.once("exit", resolve);
setTimeout(resolve, 8000);
}));
try {
cp.kill("SIGTERM");
} catch (_) {}
}
}
}
if (exitPromises.length > 0) {
await Promise.all(exitPromises);
}
try {
await manager.destroy();
} catch (e) {
logger.warn("[Sharding] destroy error:", e?.message);
}
try {
clearInterval(healthInterval);
fs.unlink(healthFile, () => {});
} catch (_) {}
process.exit(0);
};
process.once("SIGINT", () => shutdown("SIGINT"));