Change log_info to log.info by making it into an object

This commit is contained in:
Louis Lam 2022-04-13 23:33:37 +08:00
parent 94770cf865
commit e9ce1433cd
16 changed files with 289 additions and 268 deletions

View file

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