diff --git a/src/app.ts b/src/app.ts index ff40a111ee..0260d30e51 100644 --- a/src/app.ts +++ b/src/app.ts @@ -13,6 +13,7 @@ import routes from "./routes/routes.js"; import custom from "./routes/custom.js"; import error_handlers from "./routes/error_handlers.js"; import { startScheduledCleanup } from "./services/erase.js"; +import sql_init from "./services/sql_init.js"; await import('./services/handlers'); await import('./becca/becca_loader'); @@ -21,6 +22,9 @@ const app = express(); const scriptDir = dirname(fileURLToPath(import.meta.url)); +// Initialize DB +sql_init.initializeDb(); + // view engine setup app.set('views', path.join(scriptDir, 'views')); app.set('view engine', 'ejs'); diff --git a/src/services/sql_init.ts b/src/services/sql_init.ts index f630336810..2a4d967152 100644 --- a/src/services/sql_init.ts +++ b/src/services/sql_init.ts @@ -21,8 +21,6 @@ import backup from "./backup.js"; const dbReady = utils.deferred(); -cls.init(initDbConnection); - function schemaExists() { return !!sql.getValue(`SELECT name FROM sqlite_master WHERE type = 'table' AND name = 'options'`); @@ -160,29 +158,33 @@ function optimize() { log.info(`Optimization finished in ${Date.now() - start}ms.`); } -dbReady.then(() => { - if (config.General && config.General.noBackup === true) { - log.info("Disabling scheduled backups."); - - return; - } - - setInterval(() => backup.regularBackup(), 4 * 60 * 60 * 1000); - - // kickoff first backup soon after start up - setTimeout(() => backup.regularBackup(), 5 * 60 * 1000); - - // optimize is usually inexpensive no-op, so running it semi-frequently is not a big deal - setTimeout(() => optimize(), 60 * 60 * 1000); - - setInterval(() => optimize(), 10 * 60 * 60 * 1000); -}); - function getDbSize() { return sql.getValue("SELECT page_count * page_size / 1000 as size FROM pragma_page_count(), pragma_page_size()"); } -log.info(`DB size: ${getDbSize()} KB`); +function initializeDb() { + cls.init(initDbConnection); + + log.info(`DB size: ${getDbSize()} KB`); + + dbReady.then(() => { + if (config.General && config.General.noBackup === true) { + log.info("Disabling scheduled backups."); + + return; + } + + setInterval(() => backup.regularBackup(), 4 * 60 * 60 * 1000); + + // kickoff first backup soon after start up + setTimeout(() => backup.regularBackup(), 5 * 60 * 1000); + + // optimize is usually inexpensive no-op, so running it semi-frequently is not a big deal + setTimeout(() => optimize(), 60 * 60 * 1000); + + setInterval(() => optimize(), 10 * 60 * 60 * 1000); + }); +} export default { dbReady, @@ -191,5 +193,6 @@ export default { createInitialDatabase, createDatabaseForSync, setDbAsInitialized, - getDbSize + getDbSize, + initializeDb };