From b17be6b873c4c8ba9f3e2d054d9e5edc83f30607 Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Thu, 15 Dec 2016 20:43:52 -0800 Subject: [PATCH] [local-sync] Fix sync worker - Restore fns removed by mistake --- .../src/local-sync-worker/sync-worker.js | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/packages/local-sync/src/local-sync-worker/sync-worker.js b/packages/local-sync/src/local-sync-worker/sync-worker.js index 242f9d251..d7e669a2a 100644 --- a/packages/local-sync/src/local-sync-worker/sync-worker.js +++ b/packages/local-sync/src/local-sync-worker/sync-worker.js @@ -190,6 +190,19 @@ class SyncWorker { return await this._conn.connect(); } + cleanup() { + clearTimeout(this._syncTimer); + this._syncTimer = null; + this._destroyed = true; + this.closeConnection() + } + + closeConnection() { + if (this._conn) { + this._conn.end(); + } + } + async runNewSyncbackTasks() { const tasks = await this._getNewSyncbackTasks() if (tasks.length === 0) { return Promise.resolve() } @@ -335,12 +348,19 @@ class SyncWorker { const reason = this._interrupted ? 'Sync interrupted for restart' : 'Scheduled' const interval = shouldSyncImmediately ? 1 : intervals.active; const nextSyncTime = this._lastSyncTime + interval + const nextSyncStart = Math.max(1, nextSyncTime - Date.now()) - this._logger.info(`SyncWorker: Scheduling next sync iteration for ${nextSyncTime - Date.now()}ms`) + this._logger.info({ + moreToSync, + shouldSyncImmediately, + interrupted: this._interrupted, + nextSyncStartingIn: `${nextSyncStart}ms`, + syncAttemptsWhileInProgress: this._syncAttemptsWhileInProgress, + }, `SyncWorker: Scheduling next sync iteration`) this._syncTimer = setTimeout(() => { this.syncNow({reason}); - }, nextSyncTime - Date.now()); + }, nextSyncStart); } }