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,29 +158,33 @@ function optimize() {
log.info(`Optimization finished in ${Date.now() - start}ms.`); 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() { function getDbSize() {
return sql.getValue<number>("SELECT page_count * page_size / 1000 as size FROM pragma_page_count(), pragma_page_size()"); 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`); 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 { export default {
dbReady, dbReady,
@ -191,5 +193,6 @@ export default {
createInitialDatabase, createInitialDatabase,
createDatabaseForSync, createDatabaseForSync,
setDbAsInitialized, setDbAsInitialized,
getDbSize getDbSize,
initializeDb
}; };