diff --git a/package.json b/package.json index 48e61003..31276c43 100644 --- a/package.json +++ b/package.json @@ -60,7 +60,7 @@ "cy:run:unit": "npx cypress run --browser chrome --headless --config-file ./config/cypress.frontend.config.js", "cypress-open": "concurrently -k -r \"node test/prepare-test-server.js && node server/server.js --port=3002 --data-dir=./data/test/\" \"cypress open --config-file ./config/cypress.config.js\"", "build-healthcheck-armv7": "cross-env GOOS=linux GOARCH=arm GOARM=7 go build -x -o ./extra/healthcheck-armv7 ./extra/healthcheck.go", - "quick-run-nightly": "docker run --rm -p 3001:3001 louislam/uptime-kuma:nightly2", + "quick-run-nightly": "docker run --rm --env NODE_ENV=development -p 3001:3001 louislam/uptime-kuma:nightly2", "start-dev-container": "cd docker && docker-compose -f docker-compose-dev.yml up" }, "dependencies": { diff --git a/server/embedded-mariadb.js b/server/embedded-mariadb.js index 50fbfb94..8f82e64d 100644 --- a/server/embedded-mariadb.js +++ b/server/embedded-mariadb.js @@ -1,6 +1,7 @@ const { log } = require("../src/util"); const childProcess = require("child_process"); const fs = require("fs"); +const mysql = require("mysql2"); /** * It is only used inside the docker container @@ -46,32 +47,7 @@ class EmbeddedMariaDB { return; } - 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, - }); - } + this.initDB(); this.running = true; log.info("mariadb", "Starting Embedded MariaDB"); @@ -105,8 +81,7 @@ class EmbeddedMariaDB { let handler = (data) => { log.debug("mariadb", data.toString("utf-8")); if (data.toString("utf-8").includes("ready for connections")) { - log.info("mariadb", "Embedded MariaDB is ready for connections"); - this.started = true; + this.initDBAfterStarted(); } }; @@ -132,6 +107,49 @@ class EmbeddedMariaDB { } } + 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, + }); + } + + } + + 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; + } + } module.exports = { diff --git a/server/server.js b/server/server.js index 1073f3be..3870b09d 100644 --- a/server/server.js +++ b/server/server.js @@ -142,6 +142,7 @@ const { generalSocketHandler } = require("./socket-handlers/general-socket-handl const { Settings } = require("./settings"); const { CacheableDnsHttpAgent } = require("./cacheable-dns-http-agent"); const { pluginsHandler } = require("./socket-handlers/plugins-handler"); +const { EmbeddedMariaDB } = require("./embedded-mariadb"); app.use(express.json()); @@ -1770,6 +1771,10 @@ async function shutdownFunction(signal) { await sleep(2000); await Database.close(); + if (EmbeddedMariaDB.hasInstance()) { + EmbeddedMariaDB.getInstance().stop(); + } + stopBackgroundJobs(); await cloudflaredStop(); Settings.stopCacheCleaner();