Skip to content

Commit

Permalink
server-esm: Fix circular dependency between sql and sql_init
Browse files Browse the repository at this point in the history
  • Loading branch information
eliandoran committed Jul 18, 2024
1 parent 87fbd4b commit 1cd6670
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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');
Expand Down
47 changes: 25 additions & 22 deletions src/services/sql_init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import backup from "./backup.js";

const dbReady = utils.deferred<void>();

cls.init(initDbConnection);

function schemaExists() {
return !!sql.getValue(`SELECT name FROM sqlite_master
WHERE type = 'table' AND name = 'options'`);
Expand Down Expand Up @@ -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<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 {
dbReady,
Expand All @@ -191,5 +193,6 @@ export default {
createInitialDatabase,
createDatabaseForSync,
setDbAsInitialized,
getDbSize
getDbSize,
initializeDb
};

0 comments on commit 1cd6670

Please sign in to comment.