#98, sync setup now doesn't copy the whole DB file, but sets up minimal database and starts off sync

This commit is contained in:
azivner
2018-07-23 21:15:32 +02:00
parent a06618d851
commit 1fe7c62f5a
12 changed files with 185 additions and 100 deletions

View File

@@ -18,6 +18,11 @@ const cls = require('./cls');
let proxyToggle = true;
const stats = {
outstandingPushes: 0,
outstandingPulls: 0
};
async function sync() {
try {
await syncMutexService.doExclusively(async () => {
@@ -82,21 +87,33 @@ async function login() {
}
async function pullSync(syncContext) {
const changesUri = '/api/sync/changed?lastSyncId=' + await getLastSyncedPull();
while (true) {
const lastSyncedPull = await getLastSyncedPull();
const changesUri = '/api/sync/changed?lastSyncId=' + lastSyncedPull;
const rows = await syncRequest(syncContext, 'GET', changesUri);
const resp = await syncRequest(syncContext, 'GET', changesUri);
stats.outstandingPulls = resp.maxSyncId - lastSyncedPull;
log.info("Pulled " + rows.length + " changes from " + changesUri);
const rows = resp.syncs;
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.`);
}
else {
await syncUpdateService.updateEntity(sync, entity, syncContext.sourceId);
if (rows.length === 0) {
break;
}
await setLastSyncedPull(sync.id);
log.info("Pulled " + rows.length + " changes from " + changesUri);
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.`);
}
else {
await syncUpdateService.updateEntity(sync, entity, syncContext.sourceId);
}
stats.outstandingPulls = resp.maxSyncId - sync.id;
await setLastSyncedPull(sync.id);
}
}
log.info("Finished pull");
@@ -127,6 +144,8 @@ async function pushSync(syncContext) {
if (filteredSyncs.length === 0) {
log.info("Nothing to push");
stats.outstandingPushes = 0;
await setLastSyncedPush(lastSyncedPush);
break;
@@ -144,6 +163,8 @@ async function pushSync(syncContext) {
lastSyncedPush = syncRecords[syncRecords.length - 1].sync.id;
await setLastSyncedPush(lastSyncedPush);
stats.outstandingPushes = await sql.getValue(`SELECT MAX(id) FROM sync`) - lastSyncedPush;
}
}
@@ -290,5 +311,6 @@ sqlInit.dbReady.then(async () => {
module.exports = {
sync,
login,
getSyncRecords
getSyncRecords,
stats
};