From 8bd686bc6e9006ca7c5171d0a242008971901280 Mon Sep 17 00:00:00 2001 From: Juan Tejada Date: Sun, 5 Mar 2017 23:28:20 -0800 Subject: [PATCH] [client-sync] Sync accounts when app comes back online Summary: Given that we backoff exponentially in the sync loop when we encounter RetryableErrors (e.g. network errors), if the app goes offline and reaches the maximum retry backoff of 5 minutes, and if then the app comes back online, in the worst case, the sync loop will idle for 5 minutes before trying to sync, which is undesireable. To mitigate this, everytime we come online we can wake all of the sync workers. This shouldn't have any adverse effects Test Plan: manual Reviewers: evan, mark, spang Reviewed By: spang Differential Revision: https://phab.nylas.com/D4097 --- .../local-sync-worker/sync-process-manager.js | 31 ++++++++++++++----- 1 file changed, 24 insertions(+), 7 deletions(-) diff --git a/packages/client-sync/src/local-sync-worker/sync-process-manager.js b/packages/client-sync/src/local-sync-worker/sync-process-manager.js index b9986780b..f934558c4 100644 --- a/packages/client-sync/src/local-sync-worker/sync-process-manager.js +++ b/packages/client-sync/src/local-sync-worker/sync-process-manager.js @@ -1,7 +1,7 @@ const _ = require('underscore') const fs = require('fs') const {remote} = require('electron') -const {Actions} = require('nylas-exports') +const {Actions, OnlineStatusStore} = require('nylas-exports') const SyncWorker = require('./sync-worker'); const LocalDatabaseConnector = require('../shared/local-database-connector') @@ -9,15 +9,24 @@ const LocalDatabaseConnector = require('../shared/local-database-connector') class SyncProcessManager { constructor() { this._workers = {}; - this._exiting = false; this._accounts = [] + this._exiting = false; + this._resettingEmailCache = false Actions.wakeLocalSyncWorkerForAccount.listen((accountId) => this.wakeWorkerForAccount(accountId, {interrupt: true}) ); - this._resettingEmailCache = false Actions.resetEmailCache.listen(this._resetEmailCache, this) - Actions.debugSync.listen(this._onDebugSync) + Actions.debugSync.listen(this._onDebugSync, this) + OnlineStatusStore.listen(this._onOnlineStatusChanged, this) + } + + _onOnlineStatusChanged() { + if (OnlineStatusStore.isOnline()) { + this._accounts.forEach(({id}) => { + this.wakeWorkerForAccount(id, {reason: 'Came back online'}) + }) + } } _onDebugSync() { @@ -63,9 +72,17 @@ class SyncProcessManager { } } - accounts() { return this._accounts } - workers() { return _.values(this._workers) } - dbs() { return this.workers().map(w => w._db) } + accounts() { + return this._accounts + } + + workers() { + return _.values(this._workers) + } + + dbs() { + return this.workers().map(w => w._db) + } wakeWorkerForAccount(accountId, {reason = 'Waking sync', interrupt} = {}) { const worker = this._workers[accountId]