small sync refactorings

This commit is contained in:
azivner 2018-04-08 10:09:33 -04:00
parent 542e82ee5d
commit 8d8ee2a87a
2 changed files with 23 additions and 34 deletions

View File

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

View File

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