#98, better error reporting for sync setup

This commit is contained in:
azivner 2018-07-25 22:54:37 +02:00
parent d39cdbfada
commit 02dc7b199b
7 changed files with 33 additions and 22 deletions

View File

@ -223,9 +223,14 @@ addTabHandler((function() {
}); });
$syncToServerButton.click(async () => { $syncToServerButton.click(async () => {
await server.post("setup/sync-to-server"); const resp = await server.post("setup/sync-to-server");
if (resp.success) {
infoService.showMessage("Sync has been established to the server instance. It will take some time to finish."); infoService.showMessage("Sync has been established to the server instance. It will take some time to finish.");
}
else {
infoService.showError('Sync setup failed: ' + resp.error);
}
}); });
return { return {

View File

@ -105,9 +105,11 @@ function SetupModel() {
this.step('sync-in-progress'); this.step('sync-in-progress');
setInterval(checkOutstandingSyncs, 1000); setInterval(checkOutstandingSyncs, 1000);
hideAlert();
} }
else { else {
showAlert('Sync setup failed: ', resp.error); showAlert('Sync setup failed: ' + resp.error);
} }
} }
}; };
@ -130,6 +132,10 @@ function showAlert(message) {
$("#alert").show(); $("#alert").show();
} }
function hideAlert() {
$("#alert").hide();
}
ko.applyBindings(new SetupModel(), document.getElementById('setup-dialog')); ko.applyBindings(new SetupModel(), document.getElementById('setup-dialog'));
$("#setup-dialog").show(); $("#setup-dialog").show();

View File

@ -38,7 +38,12 @@ async function setupSyncToSyncServer() {
rpOpts.proxy = syncProxy; rpOpts.proxy = syncProxy;
} }
try {
await rp(rpOpts); await rp(rpOpts);
}
catch (e) {
return { success: false, error: e.message };
}
// this is completely new sync, need to reset counters. If this would not be new sync, // this is completely new sync, need to reset counters. If this would not be new sync,
// the previous request would have failed. // the previous request would have failed.
@ -46,6 +51,8 @@ async function setupSyncToSyncServer() {
await optionService.setOption('lastSyncedPull', 0); await optionService.setOption('lastSyncedPull', 0);
syncService.sync(); syncService.sync();
return { success: true };
} }
async function saveSyncSeed(req) { async function saveSyncSeed(req) {

View File

@ -19,6 +19,5 @@ async function addNoteEvent(noteId, comment) {
} }
module.exports = { module.exports = {
addEvent, addEvent
addNoteEvent
}; };

View File

@ -96,6 +96,8 @@ async function pullSync(syncContext) {
const lastSyncedPull = await getLastSyncedPull(); const lastSyncedPull = await getLastSyncedPull();
const changesUri = '/api/sync/changed?lastSyncId=' + lastSyncedPull; const changesUri = '/api/sync/changed?lastSyncId=' + lastSyncedPull;
const startDate = new Date();
const resp = await syncRequest(syncContext, 'GET', changesUri); const resp = await syncRequest(syncContext, 'GET', changesUri);
stats.outstandingPulls = resp.maxSyncId - lastSyncedPull; stats.outstandingPulls = resp.maxSyncId - lastSyncedPull;
@ -105,21 +107,19 @@ async function pullSync(syncContext) {
break; break;
} }
log.info("Pulled " + rows.length + " changes from " + changesUri); log.info("Pulled " + rows.length + " changes from " + changesUri + " in "
+ (new Date().getTime() - startDate.getTime()) + "ms");
for (const {sync, entity} of rows) { for (const {sync, entity} of rows) {
if (sourceIdService.isLocalSourceId(sync.sourceId)) { if (!sourceIdService.isLocalSourceId(sync.sourceId)) {
// too noisy
//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); await syncUpdateService.updateEntity(sync, entity, syncContext.sourceId);
} }
stats.outstandingPulls = resp.maxSyncId - sync.id; stats.outstandingPulls = resp.maxSyncId - sync.id;
await setLastSyncedPull(sync.id);
} }
await setLastSyncedPull(rows[rows.length - 1].sync.id);
} }
log.info("Finished pull"); log.info("Finished pull");
@ -163,14 +163,15 @@ async function pushSync(syncContext) {
} }
const syncRecords = await getSyncRecords(filteredSyncs); const syncRecords = await getSyncRecords(filteredSyncs);
const startDate = new Date();
log.info(`Pushing ${syncRecords.length} syncs.`);
await syncRequest(syncContext, 'PUT', '/api/sync/update', { await syncRequest(syncContext, 'PUT', '/api/sync/update', {
sourceId: sourceIdService.getCurrentSourceId(), sourceId: sourceIdService.getCurrentSourceId(),
entities: syncRecords entities: syncRecords
}); });
log.info(`Pushing ${syncRecords.length} syncs in ` + (new Date().getTime() - startDate.getTime()) + "ms");
lastSyncedPush = syncRecords[syncRecords.length - 1].sync.id; lastSyncedPush = syncRecords[syncRecords.length - 1].sync.id;
await setLastSyncedPush(lastSyncedPush); await setLastSyncedPush(lastSyncedPush);

View File

@ -54,12 +54,6 @@ async function addEntitySync(entityName, entityId, sourceId) {
sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId() sourceId: sourceId || cls.getSourceId() || sourceIdService.getCurrentSourceId()
}); });
if (!await syncOptions.isSyncSetup()) {
// this is because the "server" instances shouldn't have outstanding pushes
// useful when you fork the DB for new "client" instance, it won't try to sync the whole DB
await sql.execute("UPDATE options SET value = (SELECT MAX(id) FROM sync) WHERE name IN('lastSyncedPush', 'lastSyncedPull')");
}
eventService.emit(eventService.ENTITY_CHANGED, { eventService.emit(eventService.ENTITY_CHANGED, {
entityName, entityName,
entityId entityId

View File

@ -57,7 +57,6 @@ async function updateNote(entity, sourceId) {
await sql.replace("notes", entity); await sql.replace("notes", entity);
await syncTableService.addNoteSync(entity.noteId, sourceId); await syncTableService.addNoteSync(entity.noteId, sourceId);
await eventLogService.addNoteEvent(entity.noteId, "Synced note <note>");
}); });
log.info("Update/sync note " + entity.noteId); log.info("Update/sync note " + entity.noteId);