mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 10:26:08 +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 app_info = require('../services/app_info'); | ||||||
| const messaging = require('../services/messaging'); | const messaging = require('../services/messaging'); | ||||||
| const utils = require('../services/utils'); | const utils = require('../services/utils'); | ||||||
|  | const sql = require('../services/sql'); | ||||||
|  |  | ||||||
| const port = normalizePort(config['Network']['port'] || '3000'); | const port = normalizePort(config['Network']['port'] || '3000'); | ||||||
| app.set('port', port); | app.set('port', port); | ||||||
| @@ -53,7 +54,7 @@ httpServer.listen(port); | |||||||
| httpServer.on('error', onError); | httpServer.on('error', onError); | ||||||
| httpServer.on('listening', onListening); | httpServer.on('listening', onListening); | ||||||
|  |  | ||||||
| messaging.init(httpServer, sessionParser); | sql.dbReady.then(() => messaging.init(httpServer, sessionParser)); | ||||||
|  |  | ||||||
| if (utils.isElectron()) { | if (utils.isElectron()) { | ||||||
|     const electronRouting = require('../routes/electron'); |     const electronRouting = require('../routes/electron'); | ||||||
|   | |||||||
| @@ -58,10 +58,12 @@ if (!fs.existsSync(dataDir.BACKUP_DIR)) { | |||||||
|     fs.mkdirSync(dataDir.BACKUP_DIR, 0o700); |     fs.mkdirSync(dataDir.BACKUP_DIR, 0o700); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | sql.dbReady.then(() => { | ||||||
|     setInterval(regularBackup, 60 * 60 * 1000); |     setInterval(regularBackup, 60 * 60 * 1000); | ||||||
|  |  | ||||||
|     // kickoff backup immediately |     // kickoff backup immediately | ||||||
|     setTimeout(regularBackup, 1000); |     setTimeout(regularBackup, 1000); | ||||||
|  | }); | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     backupNow |     backupNow | ||||||
|   | |||||||
| @@ -8,7 +8,7 @@ const sync = require('./sync'); | |||||||
| let startTime = utils.nowTimestamp(); | let startTime = utils.nowTimestamp(); | ||||||
| let sentSyncId = []; | 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]); |     const syncs = await sql.getResults("SELECT * FROM sync WHERE sync_date >= ? AND source_id != ?", [startTime, source_id.currentSourceId]); | ||||||
|     startTime = utils.nowTimestamp(); |     startTime = utils.nowTimestamp(); | ||||||
|  |  | ||||||
| @@ -41,4 +41,6 @@ setInterval(async () => { | |||||||
|     for (const syncId of syncIds) { |     for (const syncId of syncIds) { | ||||||
|         sentSyncId.push(syncId); |         sentSyncId.push(syncId); | ||||||
|     } |     } | ||||||
| }, 1000); | } | ||||||
|  |  | ||||||
|  | sql.dbReady.then(() => setInterval(sendPing, 1000)); | ||||||
| @@ -2,13 +2,30 @@ | |||||||
|  |  | ||||||
| const log = require('./log'); | const log = require('./log'); | ||||||
| const dataDir = require('./data_dir'); | const dataDir = require('./data_dir'); | ||||||
|  | const fs = require('fs'); | ||||||
| const sqlite = require('sqlite'); | const sqlite = require('sqlite'); | ||||||
|  |  | ||||||
| async function createConnection() { | async function createConnection() { | ||||||
|     return await sqlite.open(dataDir.DOCUMENT_PATH, {Promise}); |     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) { | async function insert(table_name, rec, replace = false) { | ||||||
|     const keys = Object.keys(rec); |     const keys = Object.keys(rec); | ||||||
| @@ -44,13 +61,10 @@ async function rollback() { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getSingleResult(query, params = []) { | async function getSingleResult(query, params = []) { | ||||||
|     const db = await dbReady; |  | ||||||
|  |  | ||||||
|     return await wrap(async db => db.get(query, ...params)); |     return await wrap(async db => db.get(query, ...params)); | ||||||
| } | } | ||||||
|  |  | ||||||
| async function getSingleResultOrNull(query, params = []) { | async function getSingleResultOrNull(query, params = []) { | ||||||
|     const db = await dbReady; |  | ||||||
|     const all = await wrap(async db => db.all(query, ...params)); |     const all = await wrap(async db => db.all(query, ...params)); | ||||||
|  |  | ||||||
|     return all.length > 0 ? all[0] : null; |     return all.length > 0 ? all[0] : null; | ||||||
| @@ -67,8 +81,6 @@ async function getSingleValue(query, params = []) { | |||||||
| } | } | ||||||
|  |  | ||||||
| async function getResults(query, params = []) { | async function getResults(query, params = []) { | ||||||
|     const db = await dbReady; |  | ||||||
|  |  | ||||||
|     return await wrap(async db => db.all(query, ...params)); |     return await wrap(async db => db.all(query, ...params)); | ||||||
| } | } | ||||||
|  |  | ||||||
| @@ -106,7 +118,7 @@ async function executeScript(query) { | |||||||
|  |  | ||||||
| async function wrap(func) { | async function wrap(func) { | ||||||
|     const thisError = new Error(); |     const thisError = new Error(); | ||||||
|     const db = await dbReady; |     const db = await dbConnected; | ||||||
|  |  | ||||||
|     try { |     try { | ||||||
|         return await func(db); |         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 = { | module.exports = { | ||||||
|     dbReady, |     dbReady, | ||||||
|     insert, |     insert, | ||||||
|   | |||||||
| @@ -305,6 +305,7 @@ async function syncRequest(syncContext, method, uri, body) { | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | sql.dbReady.then(() => { | ||||||
|     if (isSyncSetup) { |     if (isSyncSetup) { | ||||||
|         log.info("Setting up sync to " + SYNC_SERVER + " with timeout " + SYNC_TIMEOUT); |         log.info("Setting up sync to " + SYNC_SERVER + " with timeout " + SYNC_TIMEOUT); | ||||||
|  |  | ||||||
| @@ -328,6 +329,7 @@ if (isSyncSetup) { | |||||||
|     else { |     else { | ||||||
|         log.info("Sync server not configured, sync timer not running.") |         log.info("Sync server not configured, sync timer not running.") | ||||||
|     } |     } | ||||||
|  | }); | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|     sync, |     sync, | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user