server-esm: Fix circular dependency between sql and sql_init

This commit is contained in:
Elian Doran 2024-07-19 00:47:09 +03:00
parent 87fbd4bce8
commit 1cd6670c55
No known key found for this signature in database
2 changed files with 29 additions and 22 deletions

View File

@ -13,6 +13,7 @@ import routes from "./routes/routes.js";
import custom from "./routes/custom.js"; import custom from "./routes/custom.js";
import error_handlers from "./routes/error_handlers.js"; import error_handlers from "./routes/error_handlers.js";
import { startScheduledCleanup } from "./services/erase.js"; import { startScheduledCleanup } from "./services/erase.js";
import sql_init from "./services/sql_init.js";
await import('./services/handlers'); await import('./services/handlers');
await import('./becca/becca_loader'); await import('./becca/becca_loader');
@ -21,6 +22,9 @@ const app = express();
const scriptDir = dirname(fileURLToPath(import.meta.url)); const scriptDir = dirname(fileURLToPath(import.meta.url));
// Initialize DB
sql_init.initializeDb();
// view engine setup // view engine setup
app.set('views', path.join(scriptDir, 'views')); app.set('views', path.join(scriptDir, 'views'));
app.set('view engine', 'ejs'); app.set('view engine', 'ejs');

View File

@ -21,8 +21,6 @@ import backup from "./backup.js";
const dbReady = utils.deferred<void>(); const dbReady = utils.deferred<void>();
cls.init(initDbConnection);
function schemaExists() { function schemaExists() {
return !!sql.getValue(`SELECT name FROM sqlite_master return !!sql.getValue(`SELECT name FROM sqlite_master
WHERE type = 'table' AND name = 'options'`); WHERE type = 'table' AND name = 'options'`);
@ -160,7 +158,16 @@ function optimize() {
log.info(`Optimization finished in ${Date.now() - start}ms.`); log.info(`Optimization finished in ${Date.now() - start}ms.`);
} }
dbReady.then(() => { function getDbSize() {
return sql.getValue<number>("SELECT page_count * page_size / 1000 as size FROM pragma_page_count(), pragma_page_size()");
}
function initializeDb() {
cls.init(initDbConnection);
log.info(`DB size: ${getDbSize()} KB`);
dbReady.then(() => {
if (config.General && config.General.noBackup === true) { if (config.General && config.General.noBackup === true) {
log.info("Disabling scheduled backups."); log.info("Disabling scheduled backups.");
@ -176,14 +183,9 @@ dbReady.then(() => {
setTimeout(() => optimize(), 60 * 60 * 1000); setTimeout(() => optimize(), 60 * 60 * 1000);
setInterval(() => optimize(), 10 * 60 * 60 * 1000); setInterval(() => optimize(), 10 * 60 * 60 * 1000);
}); });
function getDbSize() {
return sql.getValue<number>("SELECT page_count * page_size / 1000 as size FROM pragma_page_count(), pragma_page_size()");
} }
log.info(`DB size: ${getDbSize()} KB`);
export default { export default {
dbReady, dbReady,
schemaExists, schemaExists,
@ -191,5 +193,6 @@ export default {
createInitialDatabase, createInitialDatabase,
createDatabaseForSync, createDatabaseForSync,
setDbAsInitialized, setDbAsInitialized,
getDbSize getDbSize,
initializeDb
}; };