You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
uptime-kuma/server/embedded-mariadb.js

177 lines
4.9 KiB

2 years ago
const { log } = require("../src/util");
const childProcess = require("child_process");
2 years ago
const fs = require("fs");
2 years ago
const mysql = require("mysql2");
2 years ago
2 years ago
/**
* It is only used inside the docker container
*/
2 years ago
class EmbeddedMariaDB {
2 years ago
static instance = null;
2 years ago
2 years ago
exec = "mariadbd";
2 years ago
2 years ago
mariadbDataDir = "/app/data/mariadb";
runDir = "/app/data/run/mariadb";
socketPath = this.runDir + "/mysqld.sock";
/**
* @type {ChildProcessWithoutNullStreams}
* @private
*/
2 years ago
childProcess = null;
running = false;
started = false;
/**
* @returns {EmbeddedMariaDB} The singleton instance
2 years ago
*/
static getInstance() {
if (!EmbeddedMariaDB.instance) {
EmbeddedMariaDB.instance = new EmbeddedMariaDB();
}
return EmbeddedMariaDB.instance;
2 years ago
}
/**
* @returns {boolean} If the singleton instance is created
*/
2 years ago
static hasInstance() {
return !!EmbeddedMariaDB.instance;
}
/**
* Start the embedded MariaDB
* @returns {Promise<void>|void} A promise that resolves when the MariaDB is started or void if it is already started
2 years ago
*/
start() {
2 years ago
if (this.childProcess) {
2 years ago
log.info("mariadb", "Already started");
2 years ago
return;
}
2 years ago
this.initDB();
2 years ago
2 years ago
this.running = true;
2 years ago
log.info("mariadb", "Starting Embedded MariaDB");
this.childProcess = childProcess.spawn(this.exec, [
"--user=node",
"--datadir=" + this.mariadbDataDir,
`--socket=${this.socketPath}`,
`--pid-file=${this.runDir}/mysqld.pid`,
]);
2 years ago
this.childProcess.on("close", (code) => {
this.running = false;
this.childProcess = null;
2 years ago
this.started = false;
log.info("mariadb", "Stopped Embedded MariaDB: " + code);
if (code !== 0) {
log.info("mariadb", "Try to restart Embedded MariaDB as it is not stopped by user");
this.start();
}
2 years ago
});
this.childProcess.on("error", (err) => {
if (err.code === "ENOENT") {
2 years ago
log.error("mariadb", `Embedded MariaDB: ${this.exec} is not found`);
2 years ago
} else {
2 years ago
log.error("mariadb", err);
2 years ago
}
});
2 years ago
let handler = (data) => {
log.debug("mariadb", data.toString("utf-8"));
if (data.toString("utf-8").includes("ready for connections")) {
2 years ago
this.initDBAfterStarted();
2 years ago
}
};
this.childProcess.stdout.on("data", handler);
this.childProcess.stderr.on("data", handler);
return new Promise((resolve) => {
let interval = setInterval(() => {
if (this.started) {
clearInterval(interval);
resolve();
} else {
log.info("mariadb", "Waiting for Embedded MariaDB to start...");
}
}, 1000);
2 years ago
});
}
/**
* Stop all the child processes
* @returns {void}
*/
2 years ago
stop() {
2 years ago
if (this.childProcess) {
this.childProcess.kill("SIGINT");
this.childProcess = null;
}
}
/**
* Install MariaDB if it is not installed and make sure the `runDir` directory exists
* @returns {void}
*/
2 years ago
initDB() {
if (!fs.existsSync(this.mariadbDataDir)) {
log.info("mariadb", `Embedded MariaDB: ${this.mariadbDataDir} is not found, create one now.`);
fs.mkdirSync(this.mariadbDataDir, {
recursive: true,
});
let result = childProcess.spawnSync("mysql_install_db", [
"--user=node",
"--ldata=" + this.mariadbDataDir,
]);
if (result.status !== 0) {
let error = result.stderr.toString("utf-8");
log.error("mariadb", error);
return;
} else {
log.info("mariadb", "Embedded MariaDB: mysql_install_db done:" + result.stdout.toString("utf-8"));
}
}
if (!fs.existsSync(this.runDir)) {
log.info("mariadb", `Embedded MariaDB: ${this.runDir} is not found, create one now.`);
fs.mkdirSync(this.runDir, {
recursive: true,
});
}
}
/**
* Initialise the "kuma" database in mariadb if it does not exist
* @returns {Promise<void>}
*/
2 years ago
async initDBAfterStarted() {
const connection = mysql.createConnection({
socketPath: this.socketPath,
user: "node",
});
let result = await connection.execute("CREATE DATABASE IF NOT EXISTS `kuma`");
log.debug("mariadb", "CREATE DATABASE: " + JSON.stringify(result));
log.info("mariadb", "Embedded MariaDB is ready for connections");
this.started = true;
}
2 years ago
}
2 years ago
module.exports = {
EmbeddedMariaDB,
};