mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 02:16:05 +01:00 
			
		
		
		
	distinguishing between when DB is just connected and when it's ready for queries (validated)
This commit is contained in:
		
							
								
								
									
										3
									
								
								bin/www
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								bin/www
									
									
									
									
									
								
							| @@ -18,6 +18,7 @@ const log = require('../services/log'); | ||||
| const app_info = require('../services/app_info'); | ||||
| const messaging = require('../services/messaging'); | ||||
| const utils = require('../services/utils'); | ||||
| const sql = require('../services/sql'); | ||||
|  | ||||
| const port = normalizePort(config['Network']['port'] || '3000'); | ||||
| app.set('port', port); | ||||
| @@ -53,7 +54,7 @@ httpServer.listen(port); | ||||
| httpServer.on('error', onError); | ||||
| httpServer.on('listening', onListening); | ||||
|  | ||||
| messaging.init(httpServer, sessionParser); | ||||
| sql.dbReady.then(() => messaging.init(httpServer, sessionParser)); | ||||
|  | ||||
| if (utils.isElectron()) { | ||||
|     const electronRouting = require('../routes/electron'); | ||||
|   | ||||
| @@ -58,10 +58,12 @@ if (!fs.existsSync(dataDir.BACKUP_DIR)) { | ||||
|     fs.mkdirSync(dataDir.BACKUP_DIR, 0o700); | ||||
| } | ||||
|  | ||||
| setInterval(regularBackup, 60 * 60 * 1000); | ||||
| sql.dbReady.then(() => { | ||||
|     setInterval(regularBackup, 60 * 60 * 1000); | ||||
|  | ||||
| // kickoff backup immediately | ||||
| setTimeout(regularBackup, 1000); | ||||
|     // kickoff backup immediately | ||||
|     setTimeout(regularBackup, 1000); | ||||
| }); | ||||
|  | ||||
| module.exports = { | ||||
|     backupNow | ||||
|   | ||||
| @@ -8,7 +8,7 @@ const sync = require('./sync'); | ||||
| let startTime = utils.nowTimestamp(); | ||||
| let sentSyncId = []; | ||||
|  | ||||
| setInterval(async () => { | ||||
| async function sendPing() { | ||||
|     const syncs = await sql.getResults("SELECT * FROM sync WHERE sync_date >= ? AND source_id != ?", [startTime, source_id.currentSourceId]); | ||||
|     startTime = utils.nowTimestamp(); | ||||
|  | ||||
| @@ -41,4 +41,6 @@ setInterval(async () => { | ||||
|     for (const syncId of syncIds) { | ||||
|         sentSyncId.push(syncId); | ||||
|     } | ||||
| }, 1000); | ||||
| } | ||||
|  | ||||
| sql.dbReady.then(() => setInterval(sendPing, 1000)); | ||||
| @@ -2,13 +2,30 @@ | ||||
|  | ||||
| const log = require('./log'); | ||||
| const dataDir = require('./data_dir'); | ||||
| const fs = require('fs'); | ||||
| const sqlite = require('sqlite'); | ||||
|  | ||||
| async function createConnection() { | ||||
|     return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise}); | ||||
| } | ||||
|  | ||||
| const dbReady = createConnection(); | ||||
| const dbConnected = createConnection(); | ||||
|  | ||||
| const dbReady = new Promise((resolve, reject) => { | ||||
|     dbConnected.then(async db => { | ||||
|         const tableResults = await getResults("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'"); | ||||
|         if (tableResults.length !== 1) { | ||||
|             console.log("No connection to initialized DB."); | ||||
|             process.exit(1); | ||||
|         } | ||||
|  | ||||
|         resolve(db); | ||||
|     }) | ||||
|     .catch(e => { | ||||
|         console.log("Error connecting to DB.", e); | ||||
|         process.exit(1); | ||||
|     }); | ||||
| }); | ||||
|  | ||||
| async function insert(table_name, rec, replace = false) { | ||||
|     const keys = Object.keys(rec); | ||||
| @@ -44,13 +61,10 @@ async function rollback() { | ||||
| } | ||||
|  | ||||
| async function getSingleResult(query, params = []) { | ||||
|     const db = await dbReady; | ||||
|  | ||||
|     return await wrap(async db => db.get(query, ...params)); | ||||
| } | ||||
|  | ||||
| async function getSingleResultOrNull(query, params = []) { | ||||
|     const db = await dbReady; | ||||
|     const all = await wrap(async db => db.all(query, ...params)); | ||||
|  | ||||
|     return all.length > 0 ? all[0] : null; | ||||
| @@ -67,8 +81,6 @@ async function getSingleValue(query, params = []) { | ||||
| } | ||||
|  | ||||
| async function getResults(query, params = []) { | ||||
|     const db = await dbReady; | ||||
|  | ||||
|     return await wrap(async db => db.all(query, ...params)); | ||||
| } | ||||
|  | ||||
| @@ -106,7 +118,7 @@ async function executeScript(query) { | ||||
|  | ||||
| async function wrap(func) { | ||||
|     const thisError = new Error(); | ||||
|     const db = await dbReady; | ||||
|     const db = await dbConnected; | ||||
|  | ||||
|     try { | ||||
|         return await func(db); | ||||
| @@ -157,20 +169,6 @@ async function doInTransaction(func) { | ||||
|     } | ||||
| } | ||||
|  | ||||
| dbReady | ||||
|     .then(async () => { | ||||
|         const tableResults = await getResults("SELECT name FROM sqlite_master WHERE type='table' AND name='notes'"); | ||||
|  | ||||
|         if (tableResults.length !== 1) { | ||||
|             console.log("No connection to initialized DB."); | ||||
|             process.exit(1); | ||||
|         } | ||||
|     }) | ||||
|     .catch(e => { | ||||
|         console.log("Error connecting to DB.", e); | ||||
|         process.exit(1); | ||||
|     }); | ||||
|  | ||||
| module.exports = { | ||||
|     dbReady, | ||||
|     insert, | ||||
|   | ||||
| @@ -305,29 +305,31 @@ async function syncRequest(syncContext, method, uri, body) { | ||||
|     } | ||||
| } | ||||
|  | ||||
| if (isSyncSetup) { | ||||
|     log.info("Setting up sync to " + SYNC_SERVER + " with timeout " + SYNC_TIMEOUT); | ||||
| sql.dbReady.then(() => { | ||||
|     if (isSyncSetup) { | ||||
|         log.info("Setting up sync to " + SYNC_SERVER + " with timeout " + SYNC_TIMEOUT); | ||||
|  | ||||
|     if (SYNC_PROXY) { | ||||
|         log.info("Sync proxy: " + SYNC_PROXY); | ||||
|         if (SYNC_PROXY) { | ||||
|             log.info("Sync proxy: " + SYNC_PROXY); | ||||
|         } | ||||
|  | ||||
|         const syncCertPath = config['Sync']['syncServerCertificate']; | ||||
|  | ||||
|         if (syncCertPath) { | ||||
|             log.info('Sync certificate: ' + syncCertPath); | ||||
|  | ||||
|             syncServerCertificate = fs.readFileSync(syncCertPath); | ||||
|         } | ||||
|  | ||||
|         setInterval(sync, 60000); | ||||
|  | ||||
|         // kickoff initial sync immediately | ||||
|         setTimeout(sync, 1000); | ||||
|     } | ||||
|  | ||||
|     const syncCertPath = config['Sync']['syncServerCertificate']; | ||||
|  | ||||
|     if (syncCertPath) { | ||||
|         log.info('Sync certificate: ' + syncCertPath); | ||||
|  | ||||
|         syncServerCertificate = fs.readFileSync(syncCertPath); | ||||
|     else { | ||||
|         log.info("Sync server not configured, sync timer not running.") | ||||
|     } | ||||
|  | ||||
|     setInterval(sync, 60000); | ||||
|  | ||||
|     // kickoff initial sync immediately | ||||
|     setTimeout(sync, 1000); | ||||
| } | ||||
| else { | ||||
|     log.info("Sync server not configured, sync timer not running.") | ||||
| } | ||||
| }); | ||||
|  | ||||
| module.exports = { | ||||
|     sync, | ||||
|   | ||||
		Reference in New Issue
	
	Block a user