Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
refactor(server): don't set up other timers on module init
Elian Dorand301e562162a
diff --git a/apps/server/src/app.ts b/apps/server/src/app.tsindex 4c6e97dfae..3c5f4bdb72 100644--- a/apps/server/src/app.ts+++ b/apps/server/src/app.ts@@ -99,16 +99,16 @@ export default async function buildApp() {custom.register(app);error_handlers.register(app);- // triggers sync timer- await import("./services/sync.js");+ const { startSyncTimer } = await import("./services/sync.js");+ startSyncTimer();- // triggers backup timerawait import("./services/backup.js");- // trigger consistency checks timer- await import("./services/consistency_checks.js");+ const { startConsistencyChecks } = await import("./services/consistency_checks.js");+ startConsistencyChecks();- await import("./services/scheduler.js");+ const { startScheduler } = await import("./services/scheduler.js");+ startScheduler();startScheduledCleanup();diff --git a/apps/server/src/services/consistency_checks.ts b/apps/server/src/services/consistency_checks.tsindex 7b4ba72adf..a92daf5b7e 100644--- a/apps/server/src/services/consistency_checks.ts+++ b/apps/server/src/services/consistency_checks.ts@@ -953,12 +953,14 @@ function runEntityChangesChecks() {consistencyChecks.findEntityChangeIssues();}-sqlInit.dbReady.then(() => {- setInterval(cls.wrap(runPeriodicChecks), 60 * 60 * 1000);+export function startConsistencyChecks() {+ sqlInit.dbReady.then(() => {+ setInterval(cls.wrap(runPeriodicChecks), 60 * 60 * 1000);- // kickoff checks soon after startup (to not block the initial load)- setTimeout(cls.wrap(runPeriodicChecks), 4 * 1000);-});+ // kickoff checks soon after startup (to not block the initial load)+ setTimeout(cls.wrap(runPeriodicChecks), 4 * 1000);+ });+}export default {runOnDemandChecks,diff --git a/apps/server/src/services/scheduler.ts b/apps/server/src/services/scheduler.tsindex f44e3aa430..ef39e76009 100644--- a/apps/server/src/services/scheduler.ts+++ b/apps/server/src/services/scheduler.ts@@ -35,39 +35,41 @@ function runNotesWithLabel(runAttrValue: string) {}}-// If the database is already initialized, we need to check the hidden subtree. Otherwise, hidden subtree-// is also checked before importing the demo.zip, so no need to do it again.-if (sqlInit.isDbInitialized()) {- console.log("Checking hidden subtree.");- sqlInit.dbReady.then(() => cls.init(() => hiddenSubtreeService.checkHiddenSubtree()));-}+export function startScheduler() {+ // If the database is already initialized, we need to check the hidden subtree. Otherwise, hidden subtree+ // is also checked before importing the demo.zip, so no need to do it again.+ if (sqlInit.isDbInitialized()) {+ console.log("Checking hidden subtree.");+ sqlInit.dbReady.then(() => cls.init(() => hiddenSubtreeService.checkHiddenSubtree()));+ }-// Periodic checks.-sqlInit.dbReady.then(() => {- if (!process.env.TRILIUM_SAFE_MODE) {- setTimeout(- cls.wrap(() => runNotesWithLabel("backendStartup")),- 10 * 1000- );+ // Periodic checks.+ sqlInit.dbReady.then(() => {+ if (!process.env.TRILIUM_SAFE_MODE) {+ setTimeout(+ cls.wrap(() => runNotesWithLabel("backendStartup")),+ 10 * 1000+ );- setInterval(- cls.wrap(() => runNotesWithLabel("hourly")),- 3600 * 1000- );+ setInterval(+ cls.wrap(() => runNotesWithLabel("hourly")),+ 3600 * 1000+ );- setInterval(- cls.wrap(() => runNotesWithLabel("daily")),- 24 * 3600 * 1000- );+ setInterval(+ cls.wrap(() => runNotesWithLabel("daily")),+ 24 * 3600 * 1000+ );- setInterval(- cls.wrap(() => hiddenSubtreeService.checkHiddenSubtree()),- 7 * 3600 * 1000- );- }+ setInterval(+ cls.wrap(() => hiddenSubtreeService.checkHiddenSubtree()),+ 7 * 3600 * 1000+ );+ }- setInterval(() => checkProtectedSessionExpiration(), 30000);-});+ setInterval(() => checkProtectedSessionExpiration(), 30000);+ });+}function checkProtectedSessionExpiration() {const protectedSessionTimeout = options.getOptionInt("protectedSessionTimeout");diff --git a/apps/server/src/services/sync.ts b/apps/server/src/services/sync.tsindex ef3bd6cba9..9b01a360bc 100644--- a/apps/server/src/services/sync.ts+++ b/apps/server/src/services/sync.ts@@ -446,15 +446,17 @@ function getOutstandingPullCount() {return outstandingPullCount;}-becca_loader.beccaLoaded.then(() => {- setInterval(cls.wrap(sync), 60000);+export function startSyncTimer() {+ becca_loader.beccaLoaded.then(() => {+ setInterval(cls.wrap(sync), 60000);- // kickoff initial sync immediately, but should happen after initial consistency checks- setTimeout(cls.wrap(sync), 5000);+ // kickoff initial sync immediately, but should happen after initial consistency checks+ setTimeout(cls.wrap(sync), 5000);- // called just so ws.setLastSyncedPush() is called- getLastSyncedPush();-});+ // called just so ws.setLastSyncedPush() is called+ getLastSyncedPush();+ });+}export default {sync,