diff --git a/bin/www b/bin/www index ca32874bb..5110e3f5b 100755 --- a/bin/www +++ b/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'); diff --git a/services/backup.js b/services/backup.js index 513ba932e..9e355fc9e 100644 --- a/services/backup.js +++ b/services/backup.js @@ -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 diff --git a/services/ping_job.js b/services/ping_job.js index 80fc036b7..24445d0d1 100644 --- a/services/ping_job.js +++ b/services/ping_job.js @@ -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); \ No newline at end of file +} + +sql.dbReady.then(() => setInterval(sendPing, 1000)); \ No newline at end of file diff --git a/services/sql.js b/services/sql.js index 49d867999..d98261282 100644 --- a/services/sql.js +++ b/services/sql.js @@ -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, diff --git a/services/sync.js b/services/sync.js index d59a74986..631c9a235 100644 --- a/services/sync.js +++ b/services/sync.js @@ -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,