From cd90200272e1a97ed56538802e3b71d4dbf02196 Mon Sep 17 00:00:00 2001 From: azivner Date: Tue, 28 Nov 2017 18:33:23 -0500 Subject: [PATCH] fixed sync --- services/messaging.js | 5 ++--- services/ping_job.js | 2 +- services/source_id.js | 2 +- services/sql.js | 13 +++++++++---- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/services/messaging.js b/services/messaging.js index 90079af7b..70fe674e4 100644 --- a/services/messaging.js +++ b/services/messaging.js @@ -1,4 +1,3 @@ -const utils = require('../services/utils'); const WebSocket = require('ws'); let webSocketServer; @@ -10,7 +9,7 @@ function init(httpServer) { }); } -async function send(message) { +async function sendMessage(message) { const jsonStr = JSON.stringify(message); webSocketServer.clients.forEach(function each(client) { @@ -22,5 +21,5 @@ async function send(message) { module.exports = { init, - send + sendMessage }; \ No newline at end of file diff --git a/services/ping_job.js b/services/ping_job.js index 74d059fef..80fc036b7 100644 --- a/services/ping_job.js +++ b/services/ping_job.js @@ -32,7 +32,7 @@ setInterval(async () => { const changesToPushCount = await sql.getSingleValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]); - messaging.send({ + messaging.sendMessage({ type: 'sync', data: data, changesToPushCount: sync.isSyncSetup ? changesToPushCount : 0 diff --git a/services/source_id.js b/services/source_id.js index 59efbd20a..f5ba89d77 100644 --- a/services/source_id.js +++ b/services/source_id.js @@ -17,7 +17,7 @@ sql.dbReady.then(async () => { }); }); - allSourceIds = await sql.getFlattenedResults("source_id", "SELECT source_id FROM source_ids"); + allSourceIds = await sql.getFlattenedResults("source_id", "SELECT source_id FROM source_ids ORDER BY date_created DESC"); } catch (e) {} }); diff --git a/services/sql.js b/services/sql.js index 163148261..cc48d3e85 100644 --- a/services/sql.js +++ b/services/sql.js @@ -122,38 +122,43 @@ async function wrap(func) { } } +let transactionActive = false; let transactionPromise = null; async function doInTransaction(func) { - while (transactionPromise !== null) { + while (transactionActive) { await transactionPromise; } const error = new Error(); // to capture correct stack trace in case of exception + transactionActive = true; transactionPromise = new Promise(async (resolve, reject) => { try { - await beginTransaction(); await func(); await commit(); + transactionActive = false; resolve(); - transactionPromise = null; } catch (e) { log.error("Error executing transaction, executing rollback. Inner exception: " + e.stack + error.stack); await rollback(); + transactionActive = false; resolve(); - transactionPromise = null; throw e; } }); + + if (transactionActive) { + await transactionPromise; + } } dbReady