From 8d8ee2a87a74954cf0230b039ef748fb628ee952 Mon Sep 17 00:00:00 2001 From: azivner Date: Sun, 8 Apr 2018 10:09:33 -0400 Subject: [PATCH] small sync refactorings --- src/routes/api/sync.js | 4 ++-- src/services/sync.js | 53 +++++++++++++++++------------------------- 2 files changed, 23 insertions(+), 34 deletions(-) diff --git a/src/routes/api/sync.js b/src/routes/api/sync.js index 725626b9f..b90266048 100644 --- a/src/routes/api/sync.js +++ b/src/routes/api/sync.js @@ -10,8 +10,8 @@ const log = require('../../services/log'); async function checkSync() { return { - 'hashes': await contentHashService.getHashes(), - 'maxSyncId': await sql.getValue('SELECT MAX(id) FROM sync') + hashes: await contentHashService.getHashes(), + maxSyncId: await sql.getValue('SELECT MAX(id) FROM sync') }; } diff --git a/src/services/sync.js b/src/services/sync.js index 92e4bacc3..12669caeb 100644 --- a/src/services/sync.js +++ b/src/services/sync.js @@ -89,18 +89,8 @@ async function login() { return syncContext; } -async function getLastSyncedPull() { - return parseInt(await optionService.getOption('lastSyncedPull')); -} - -async function setLastSyncedPull(syncId) { - await optionService.setOption('lastSyncedPull', syncId); -} - async function pullSync(syncContext) { - const lastSyncedPull = await getLastSyncedPull(); - - const changesUri = '/api/sync/changed?lastSyncId=' + lastSyncedPull; + const changesUri = '/api/sync/changed?lastSyncId=' + await getLastSyncedPull(); const rows = await syncRequest(syncContext, 'GET', changesUri); @@ -109,14 +99,6 @@ async function pullSync(syncContext) { for (const {sync, entity} of rows) { if (sourceIdService.isLocalSourceId(sync.sourceId)) { log.info(`Skipping pull #${sync.id} ${sync.entityName} ${sync.entityId} because ${sync.sourceId} is a local source id.`); - - await setLastSyncedPull(sync.id); - - continue; - } - - if (!entity) { - log.error(`Empty response to pull for sync #${sync.id} ${sync.entityName}, id=${sync.entityId}`); } else { await syncUpdateService.updateEntity(sync.entityName, entity, syncContext.sourceId); @@ -128,14 +110,6 @@ async function pullSync(syncContext) { log.info("Finished pull"); } -async function getLastSyncedPush() { - return parseInt(await optionService.getOption('lastSyncedPush')); -} - -async function setLastSyncedPush(lastSyncedPush) { - await optionService.setOption('lastSyncedPush', lastSyncedPush); -} - async function pushSync(syncContext) { let lastSyncedPush = await getLastSyncedPush(); @@ -159,8 +133,6 @@ async function pushSync(syncContext) { }); if (filteredSyncs.length === 0) { - // nothing to sync - log.info("Nothing to push"); await setLastSyncedPush(lastSyncedPush); @@ -170,6 +142,8 @@ async function pushSync(syncContext) { const syncRecords = await getSyncRecords(filteredSyncs); + log.info(`Pushing ${syncRecords.length} syncs.`); + await syncRequest(syncContext, 'PUT', '/api/sync/update', { sourceId: sourceIdService.getCurrentSourceId(), entities: syncRecords @@ -190,11 +164,10 @@ async function checkContentHash(syncContext) { return; } - const lastSyncedPush = await getLastSyncedPush(); - const notPushedSyncs = await sql.getValue("SELECT COUNT(*) FROM sync WHERE id > ?", [lastSyncedPush]); + const notPushedSyncs = await sql.getValue("SELECT COUNT(*) FROM sync WHERE id > ?", [await getLastSyncedPush()]); if (notPushedSyncs > 0) { - log.info("There's " + notPushedSyncs + " outstanding pushes, skipping content check."); + log.info(`There's ${notPushedSyncs} outstanding pushes, skipping content check.`); return; } @@ -288,6 +261,22 @@ async function getSyncRecords(syncs) { return records; } +async function getLastSyncedPull() { + return parseInt(await optionService.getOption('lastSyncedPull')); +} + +async function setLastSyncedPull(syncId) { + await optionService.setOption('lastSyncedPull', syncId); +} + +async function getLastSyncedPush() { + return parseInt(await optionService.getOption('lastSyncedPush')); +} + +async function setLastSyncedPush(lastSyncedPush) { + await optionService.setOption('lastSyncedPush', lastSyncedPush); +} + sqlInit.dbReady.then(() => { if (syncSetup.isSyncSetup) { log.info("Setting up sync to " + syncSetup.SYNC_SERVER + " with timeout " + syncSetup.SYNC_TIMEOUT);