From b0cbe917840f653a223bbc417e892ab125d1e9c5 Mon Sep 17 00:00:00 2001 From: azivner Date: Wed, 13 Dec 2017 23:03:48 -0500 Subject: [PATCH] implementation of forcing full (re)sync --- public/javascripts/dialogs/settings.js | 12 +++++++++ routes/api/sync.js | 12 +++++++++ services/ping_job.js | 4 +-- services/sync.js | 34 ++++++++++---------------- services/sync_setup.js | 11 +++++++++ services/sync_table.js | 4 +-- services/sync_update.js | 13 ---------- views/index.ejs | 4 +++ 8 files changed, 56 insertions(+), 38 deletions(-) create mode 100644 services/sync_setup.js diff --git a/public/javascripts/dialogs/settings.js b/public/javascripts/dialogs/settings.js index b5b18fb3e..c934b4ed7 100644 --- a/public/javascripts/dialogs/settings.js +++ b/public/javascripts/dialogs/settings.js @@ -149,5 +149,17 @@ settings.addModule((async function () { buildRevisionEl.html(appInfo.build_revision); buildRevisionEl.attr('href', 'https://github.com/zadam/trilium/commit/' + appInfo.build_revision); + return {}; +})()); + +settings.addModule((async function () { + const forceFullSyncButton = $("#force-full-sync-button"); + + forceFullSyncButton.click(async () => { + await server.post('sync/force-full-sync'); + + showMessage("Full sync triggered"); + }); + return {}; })()); \ No newline at end of file diff --git a/routes/api/sync.js b/routes/api/sync.js index ed9a2e361..91c3cde75 100644 --- a/routes/api/sync.js +++ b/routes/api/sync.js @@ -20,6 +20,18 @@ router.post('/now', auth.checkApiAuth, async (req, res, next) => { res.send(await sync.sync()); }); +router.post('/force-full-sync', auth.checkApiAuth, async (req, res, next) => { + await sql.doInTransaction(async () => { + await options.setOption('last_synced_pull', 0); + await options.setOption('last_synced_push', 0); + }); + + // not awaiting for the job to finish (will probably take a long time) + sync.sync(); + + res.send({}); +}); + router.get('/changed', auth.checkApiAuth, async (req, res, next) => { const lastSyncId = parseInt(req.query.lastSyncId); diff --git a/services/ping_job.js b/services/ping_job.js index 7e8c1b3b8..a7621e269 100644 --- a/services/ping_job.js +++ b/services/ping_job.js @@ -3,7 +3,7 @@ const source_id = require('./source_id'); const utils = require('./utils'); const messaging = require('./messaging'); const options = require('./options'); -const sync = require('./sync'); +const sync_setup = require('./sync_setup'); let startTime = utils.nowDate(); let sentSyncId = []; @@ -35,7 +35,7 @@ async function sendPing() { messaging.sendMessage({ type: 'sync', data: data, - changesToPushCount: sync.isSyncSetup ? changesToPushCount : 0 + changesToPushCount: sync_setup.isSyncSetup ? changesToPushCount : 0 }); for (const syncId of syncIds) { diff --git a/services/sync.js b/services/sync.js index 73c577b96..a3eb10949 100644 --- a/services/sync.js +++ b/services/sync.js @@ -5,7 +5,6 @@ const rp = require('request-promise'); const sql = require('./sql'); const options = require('./options'); const utils = require('./utils'); -const config = require('./config'); const source_id = require('./source_id'); const notes = require('./notes'); const syncUpdate = require('./sync_update'); @@ -14,11 +13,7 @@ const event_log = require('./event_log'); const fs = require('fs'); const app_info = require('./app_info'); const messaging = require('./messaging'); - -const SYNC_SERVER = config['Sync']['syncServerHost']; -const isSyncSetup = !!SYNC_SERVER; -const SYNC_TIMEOUT = config['Sync']['syncServerTimeout'] || 5000; -const SYNC_PROXY = config['Sync']['syncProxy']; +const sync_setup = require('./sync_setup'); let syncInProgress = false; let proxyToggle = true; @@ -278,7 +273,7 @@ async function checkContentHash(syncContext) { } async function syncRequest(syncContext, method, uri, body) { - const fullUri = SYNC_SERVER + uri; + const fullUri = sync_setup.SYNC_SERVER + uri; try { const options = { @@ -287,15 +282,15 @@ async function syncRequest(syncContext, method, uri, body) { jar: syncContext.cookieJar, json: true, body: body, - timeout: SYNC_TIMEOUT + timeout: sync_setup.SYNC_TIMEOUT }; if (syncServerCertificate) { options.ca = syncServerCertificate; } - if (SYNC_PROXY && proxyToggle) { - options.proxy = SYNC_PROXY; + if (sync_setup.SYNC_PROXY && proxyToggle) { + options.proxy = sync_setup.SYNC_PROXY; } return await rp(options); @@ -306,19 +301,17 @@ async function syncRequest(syncContext, method, uri, body) { } sql.dbReady.then(() => { - if (isSyncSetup) { - log.info("Setting up sync to " + SYNC_SERVER + " with timeout " + SYNC_TIMEOUT); + if (sync_setup.isSyncSetup) { + log.info("Setting up sync to " + sync_setup.SYNC_SERVER + " with timeout " + sync_setup.SYNC_TIMEOUT); - if (SYNC_PROXY) { - log.info("Sync proxy: " + SYNC_PROXY); + if (sync_setup.SYNC_PROXY) { + log.info("Sync proxy: " + sync_setup.SYNC_PROXY); } - const syncCertPath = config['Sync']['syncServerCertificate']; + if (sync_setup.SYNC_CERT_PATH) { + log.info('Sync certificate: ' + sync_setup.SYNC_CERT_PATH); - if (syncCertPath) { - log.info('Sync certificate: ' + syncCertPath); - - syncServerCertificate = fs.readFileSync(syncCertPath); + syncServerCertificate = fs.readFileSync(sync_setup.SYNC_CERT_PATH); } setInterval(sync, 60000); @@ -332,6 +325,5 @@ sql.dbReady.then(() => { }); module.exports = { - sync, - isSyncSetup + sync }; \ No newline at end of file diff --git a/services/sync_setup.js b/services/sync_setup.js new file mode 100644 index 000000000..b51087ed1 --- /dev/null +++ b/services/sync_setup.js @@ -0,0 +1,11 @@ +"use strict"; + +const config = require('./config'); + +module.exports = { + SYNC_SERVER: config['Sync']['syncServerHost'], + isSyncSetup: !!config['Sync']['syncServerHost'], + SYNC_TIMEOUT: config['Sync']['syncServerTimeout'] || 5000, + SYNC_PROXY: config['Sync']['syncProxy'], + SYNC_CERT_PATH: config['Sync']['syncServerCertificate'] +}; \ No newline at end of file diff --git a/services/sync_table.js b/services/sync_table.js index 22b758d64..f4c038087 100644 --- a/services/sync_table.js +++ b/services/sync_table.js @@ -1,7 +1,7 @@ const sql = require('./sql'); const source_id = require('./source_id'); const utils = require('./utils'); -const sync = require('./sync'); +const sync_setup = require('./sync_setup'); async function addNoteSync(noteId, sourceId) { await addEntitySync("notes", noteId, sourceId) @@ -28,7 +28,7 @@ async function addRecentNoteSync(notePath, sourceId) { } async function addEntitySync(entityName, entityId, sourceId) { - if (sync.isSyncSetup) { + if (sync_setup.isSyncSetup) { await sql.replace("sync", { entity_name: entityName, entity_id: entityId, diff --git a/services/sync_update.js b/services/sync_update.js index f73561445..2c553d4a2 100644 --- a/services/sync_update.js +++ b/services/sync_update.js @@ -1,7 +1,6 @@ const sql = require('./sql'); const log = require('./log'); const options = require('./options'); -const utils = require('./utils'); const eventLog = require('./event_log'); const notes = require('./notes'); const sync_table = require('./sync_table'); @@ -19,9 +18,6 @@ async function updateNote(entity, sourceId) { log.info("Update/sync note " + entity.note_id); } - else { - await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note , " + utils.formatTwoDates(origNote.date_modified, entity.date_modified)); - } } async function updateNoteTree(entity, sourceId) { @@ -37,9 +33,6 @@ async function updateNoteTree(entity, sourceId) { log.info("Update/sync note tree " + entity.note_tree_id); } - else { - await eventLog.addNoteEvent(entity.note_tree_id, "Sync conflict in note tree , " + utils.formatTwoDates(orig.date_modified, entity.date_modified)); - } }); } @@ -54,9 +47,6 @@ async function updateNoteHistory(entity, sourceId) { log.info("Update/sync note history " + entity.note_history_id); } - else { - await eventLog.addNoteEvent(entity.note_id, "Sync conflict in note history for , " + utils.formatTwoDates(orig.date_modified_to, entity.date_modified_to)); - } }); } @@ -85,9 +75,6 @@ async function updateOptions(entity, sourceId) { await eventLog.addEvent("Synced option " + entity.opt_name); } - else { - await eventLog.addEvent("Sync conflict in options for " + entity.opt_name + ", " + utils.formatTwoDates(orig.date_modified, entity.date_modified)); - } }); } diff --git a/views/index.ejs b/views/index.ejs index 902ed134d..262f1be48 100644 --- a/views/index.ejs +++ b/views/index.ejs @@ -165,6 +165,7 @@
  • Change password
  • Protected session
  • History snapshots
  • +
  • Sync
  • About Trilium
  • @@ -212,6 +213,9 @@
    +
    + +