fixed sync

This commit is contained in:
azivner 2017-11-28 18:33:23 -05:00
parent 91c2f9e7cb
commit cd90200272
4 changed files with 13 additions and 9 deletions

View File

@ -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
};

View File

@ -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

View File

@ -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) {}
});

View File

@ -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