Skip to content

Build your personal knowledge base with Trilium Notes

#98, better error reporting for sync setup

azivner02dc7b199be6
diff --git a/src/public/javascripts/dialogs/options.js b/src/public/javascripts/dialogs/options.js
index 29abfd2b63..5c150dcb11 100644
--- a/src/public/javascripts/dialogs/options.js
+++ b/src/public/javascripts/dialogs/options.js
@@ -223,9 +223,14 @@ addTabHandler((function() {
});
$syncToServerButton.click(async () => {
- await server.post("setup/sync-to-server");
+ const resp = await server.post("setup/sync-to-server");
- infoService.showMessage("Sync has been established to the server instance. It will take some time to finish.");
+ if (resp.success) {
+ 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 {
diff --git a/src/public/javascripts/setup.js b/src/public/javascripts/setup.js
index 06cc3dd0dc..09d102b728 100644
--- a/src/public/javascripts/setup.js
+++ b/src/public/javascripts/setup.js
@@ -105,9 +105,11 @@ function SetupModel() {
this.step('sync-in-progress');
setInterval(checkOutstandingSyncs, 1000);
+
+ hideAlert();
}
else {
- showAlert('Sync setup failed: ', resp.error);
+ showAlert('Sync setup failed: ' + resp.error);
}
}
};
@@ -130,6 +132,10 @@ function showAlert(message) {
$("#alert").show();
}
+function hideAlert() {
+ $("#alert").hide();
+}
+
ko.applyBindings(new SetupModel(), document.getElementById('setup-dialog'));
$("#setup-dialog").show();
\ No newline at end of file
diff --git a/src/routes/api/setup.js b/src/routes/api/setup.js
index e62a1afa85..9fc0b277d7 100644
--- a/src/routes/api/setup.js
+++ b/src/routes/api/setup.js
@@ -38,7 +38,12 @@ async function setupSyncToSyncServer() {
rpOpts.proxy = syncProxy;
}
- await rp(rpOpts);
+ try {
+ 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,
// the previous request would have failed.
@@ -46,6 +51,8 @@ async function setupSyncToSyncServer() {
await optionService.setOption('lastSyncedPull', 0);
syncService.sync();
+
+ return { success: true };
}
async function saveSyncSeed(req) {
diff --git a/src/services/event_log.js b/src/services/event_log.js
index 7a5f77b98e..65cefed3b4 100644
--- a/src/services/event_log.js
+++ b/src/services/event_log.js
@@ -19,6 +19,5 @@ async function addNoteEvent(noteId, comment) {
}
module.exports = {
- addEvent,
- addNoteEvent
+ addEvent
};
\ No newline at end of file
diff --git a/src/services/sync.js b/src/services/sync.js
index a36794f6ec..25799cd89e 100644
--- a/src/services/sync.js
+++ b/src/services/sync.js
@@ -96,6 +96,8 @@ async function pullSync(syncContext) {
const lastSyncedPull = await getLastSyncedPull();
const changesUri = '/api/sync/changed?lastSyncId=' + lastSyncedPull;
+ const startDate = new Date();
+
const resp = await syncRequest(syncContext, 'GET', changesUri);
stats.outstandingPulls = resp.maxSyncId - lastSyncedPull;
@@ -105,21 +107,19 @@ async function pullSync(syncContext) {
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) {
- 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 {
+ if (!sourceIdService.isLocalSourceId(sync.sourceId)) {
await syncUpdateService.updateEntity(sync, entity, syncContext.sourceId);
}
stats.outstandingPulls = resp.maxSyncId - sync.id;
- await setLastSyncedPull(sync.id);
}
+
+ await setLastSyncedPull(rows[rows.length - 1].sync.id);
}
log.info("Finished pull");
@@ -163,14 +163,15 @@ async function pushSync(syncContext) {
}
const syncRecords = await getSyncRecords(filteredSyncs);
-
- log.info(`Pushing ${syncRecords.length} syncs.`);
+ const startDate = new Date();
await syncRequest(syncContext, 'PUT', '/api/sync/update', {
sourceId: sourceIdService.getCurrentSourceId(),
entities: syncRecords
});
+ log.info(`Pushing ${syncRecords.length} syncs in ` + (new Date().getTime() - startDate.getTime()) + "ms");
+
lastSyncedPush = syncRecords[syncRecords.length - 1].sync.id;
await setLastSyncedPush(lastSyncedPush);
diff --git a/src/services/sync_table.js b/src/services/sync_table.js
index e4c2a6a13c..9b4e6ec16f 100644
--- a/src/services/sync_table.js
+++ b/src/services/sync_table.js
@@ -54,12 +54,6 @@ async function addEntitySync(entityName, entityId, sourceId) {
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, {
entityName,
entityId
diff --git a/src/services/sync_update.js b/src/services/sync_update.js
index 536a99052d..f966cc4589 100644
--- a/src/services/sync_update.js
+++ b/src/services/sync_update.js
@@ -57,7 +57,6 @@ async function updateNote(entity, sourceId) {
await sql.replace("notes", entity);
await syncTableService.addNoteSync(entity.noteId, sourceId);
- await eventLogService.addNoteEvent(entity.noteId, "Synced note <note>");
});
log.info("Update/sync note " + entity.noteId);