introduce consistent logging

This commit is contained in:
Andreas Brett
2021-11-11 12:31:28 +01:00
parent 5df34cd137
commit bdcdf47e52
15 changed files with 290 additions and 201 deletions

View File

@@ -1,7 +1,7 @@
const fs = require("fs");
const { R } = require("redbean-node");
const { setSetting, setting } = require("./util-server");
const { debug, sleep } = require("../src/util");
const { log, sleep } = require("../src/util");
const dayjs = require("dayjs");
const knex = require("knex");
@@ -76,7 +76,7 @@ class Database {
fs.mkdirSync(Database.uploadDir, { recursive: true });
}
console.log(`Data Dir: ${Database.dataDir}`);
log("db", `Data Dir: ${Database.dataDir}`);
}
static async connect() {
@@ -117,10 +117,10 @@ class Database {
await R.exec("PRAGMA cache_size = -12000");
await R.exec("PRAGMA auto_vacuum = FULL");
console.log("SQLite config:");
console.log(await R.getAll("PRAGMA journal_mode"));
console.log(await R.getAll("PRAGMA cache_size"));
console.log("SQLite Version: " + await R.getCell("SELECT sqlite_version()"));
log("db", "SQLite config:");
log("db", await R.getAll("PRAGMA journal_mode"));
log("db", await R.getAll("PRAGMA cache_size"));
log("db","SQLite Version: " + await R.getCell("SELECT sqlite_version()"));
}
static async patch() {
@@ -130,15 +130,15 @@ class Database {
version = 0;
}
console.info("Your database version: " + version);
console.info("Latest database version: " + this.latestVersion);
log("db", "Your database version: " + version);
log("db", "Latest database version: " + this.latestVersion);
if (version === this.latestVersion) {
console.info("Database patch not needed");
log("db", "Database patch not needed");
} else if (version > this.latestVersion) {
console.info("Warning: Database version is newer than expected");
log("db", "Warning: Database version is newer than expected");
} else {
console.info("Database patch is needed");
log("db", "Database patch is needed");
this.backup(version);
@@ -146,17 +146,17 @@ class Database {
try {
for (let i = version + 1; i <= this.latestVersion; i++) {
const sqlFile = `./db/patch${i}.sql`;
console.info(`Patching ${sqlFile}`);
log("db", `Patching ${sqlFile}`);
await Database.importSQLFile(sqlFile);
console.info(`Patched ${sqlFile}`);
log("db", `Patched ${sqlFile}`);
await setSetting("database_version", i);
}
} catch (ex) {
await Database.close();
console.error(ex);
console.error("Start Uptime-Kuma failed due to issue patching the database");
console.error("Please submit a bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues");
log("db", ex, "error");
log("db", "Start Uptime-Kuma failed due to issue patching the database", "error");
log("db", "Please submit a bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues", "error");
this.restore();
process.exit(1);
@@ -171,15 +171,15 @@ class Database {
* @returns {Promise<void>}
*/
static async patch2() {
console.log("Database Patch 2.0 Process");
log("db", "Database Patch 2.0 Process");
let databasePatchedFiles = await setting("databasePatchedFiles");
if (! databasePatchedFiles) {
databasePatchedFiles = {};
}
debug("Patched files:");
debug(databasePatchedFiles);
log("db", "Patched files:", "debug");
log("db", databasePatchedFiles, "debug");
try {
for (let sqlFilename in this.patchList) {
@@ -187,15 +187,15 @@ class Database {
}
if (this.patched) {
console.log("Database Patched Successfully");
log("db", "Database Patched Successfully");
}
} catch (ex) {
await Database.close();
console.error(ex);
console.error("Start Uptime-Kuma failed due to issue patching the database");
console.error("Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues");
log("db", ex, "error");
log("db", "Start Uptime-Kuma failed due to issue patching the database", "error");
log("db", "Please submit the bug report if you still encounter the problem after restart: https://github.com/louislam/uptime-kuma/issues", "error");
this.restore();
@@ -214,16 +214,16 @@ class Database {
let value = this.patchList[sqlFilename];
if (! value) {
console.log(sqlFilename + " skip");
log("db", sqlFilename + " skip");
return;
}
// Check if patched
if (! databasePatchedFiles[sqlFilename]) {
console.log(sqlFilename + " is not patched");
log("db", sqlFilename + " is not patched");
if (value.parents) {
console.log(sqlFilename + " need parents");
log("db", sqlFilename + " need parents");
for (let parentSQLFilename of value.parents) {
await this.patch2Recursion(parentSQLFilename, databasePatchedFiles);
}
@@ -231,14 +231,14 @@ class Database {
this.backup(dayjs().format("YYYYMMDDHHmmss"));
console.log(sqlFilename + " is patching");
log("db", sqlFilename + " is patching");
this.patched = true;
await this.importSQLFile("./db/" + sqlFilename);
databasePatchedFiles[sqlFilename] = true;
console.log(sqlFilename + " was patched successfully");
log("db", sqlFilename + " was patched successfully");
} else {
debug(sqlFilename + " is already patched, skip");
log("db", sqlFilename + " is already patched, skip", "debug");
}
}
@@ -290,7 +290,7 @@ class Database {
};
process.addListener("unhandledRejection", listener);
console.log("Closing the database");
log("db", "Closing the database");
while (true) {
Database.noReject = true;
@@ -300,10 +300,10 @@ class Database {
if (Database.noReject) {
break;
} else {
console.log("Waiting to close the database");
log("db", "Waiting to close the database");
}
}
console.log("SQLite closed");
log("db", "SQLite closed");
process.removeListener("unhandledRejection", listener);
}
@@ -315,7 +315,7 @@ class Database {
*/
static backup(version) {
if (! this.backupPath) {
console.info("Backing up the database");
log("db", "Backing up the database");
this.backupPath = this.dataDir + "kuma.db.bak" + version;
fs.copyFileSync(Database.path, this.backupPath);
@@ -338,7 +338,7 @@ class Database {
*/
static restore() {
if (this.backupPath) {
console.error("Patching the database failed!!! Restoring the backup");
log("db", "Patching the database failed!!! Restoring the backup", "error");
const shmPath = Database.path + "-shm";
const walPath = Database.path + "-wal";
@@ -357,7 +357,7 @@ class Database {
fs.unlinkSync(walPath);
}
} catch (e) {
console.log("Restore failed; you may need to restore the backup manually");
log("db", "Restore failed; you may need to restore the backup manually", "error");
process.exit(1);
}
@@ -373,14 +373,14 @@ class Database {
}
} else {
console.log("Nothing to restore");
log("db", "Nothing to restore");
}
}
static getSize() {
debug("Database.getSize()");
log("db", "Database.getSize()", "debug");
let stats = fs.statSync(Database.path);
debug(stats);
log("db", stats, "debug");
return stats.size;
}