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

+11 -4
View File
@@ -9,6 +9,7 @@ import fs from "node:fs";
import mysql from "mysql2";
import { EventEmitter } from "node:events";
import { MYSQL_POOL_DEFAULTS, attachMysqlPoolGuard } from "./MysqlGuard.mjs";
import { getSharedMysqlPool } from "./PoolManager.mjs";
import { logger } from "../core/Logger.mjs";
/**
@@ -180,16 +181,22 @@ export class RemoteSettingsManager extends SettingsManager {
/**
* Create a new RemoteSettingsManager backed by a MySQL pool.
* @param {object} config - mysql2 pool configuration (host, user, password, database).
* @param {object} config - mysql2 pool or configuration (host, user, password, database).
* @param {string} defaultsPath - Path to a JSON file with default values and descriptions.
* @param {string|null} [botId=null] - The bot user ID for multi-bot support.
*/
constructor(config, defaultsPath, botId = null) {
super();
this.botId = botId;
this.db = mysql.createPool({ ...MYSQL_POOL_DEFAULTS, connectionLimit: 15, ...config });
if (config && typeof config.query === "function") {
this.db = config;
this._ownsPool = false;
} else {
this.db = getSharedMysqlPool(config);
this._ownsPool = false;
}
attachMysqlPoolGuard(this.db, "DB");
this.db.on('error', (err) => {
this.db.on?.('error', (err) => {
logger.error('[DB] MySQL pool error:', err.code ?? err.message);
});
if (defaultsPath) this.loadDefaultsSync(defaultsPath);
@@ -219,7 +226,7 @@ export class RemoteSettingsManager extends SettingsManager {
}
await Promise.allSettled(pendingServers.map(server => this._doSave(server)));
try {
if (this.db?.end) {
if (this._ownsPool && this.db?.end) {
await new Promise(r => this.db.end(() => r()));
}
} catch (_) {}