[local-sync] Use new BatteryStatusManager to compute sync loop delay

Summary:
We don't need to check folders that often while on battery. Check every
5 minutes rather than every 10 seconds.

Test Plan: Run locally, verify the timeout is longer while on battery

Reviewers: evan, spang, juan

Reviewed By: spang, juan

Differential Revision: https://phab.nylas.com/D3940
This commit is contained in:
Mark Hahnenberg 2017-02-15 18:04:26 -08:00
parent e15079874b
commit d654e80a96
2 changed files with 14 additions and 15 deletions

View file

@ -10,6 +10,7 @@ const {
NylasAPI,
N1CloudAPI,
NylasAPIRequest,
BatteryStatusManager,
Account: {SYNC_STATE_RUNNING, SYNC_STATE_AUTH_FAILED, SYNC_STATE_ERROR},
} = require('nylas-exports')
const Interruptible = require('../shared/interruptible')
@ -18,9 +19,9 @@ const SyncTaskFactory = require('./sync-task-factory');
const SyncbackTaskRunner = require('./syncback-task-runner').default;
const LocalSyncDeltaEmitter = require('./local-sync-delta-emitter').default
const SYNC_LOOP_INTERVAL_MS = 10 * 1000
const MAX_SOCKET_TIMEOUT_MS = 10 * 60 * 1000 // 10 min
const AC_SYNC_LOOP_INTERVAL_MS = 10 * 1000 // 10 sec
const BATTERY_SYNC_LOOP_INTERVAL_MS = 5 * 60 * 1000 // 5 min
const MAX_SOCKET_TIMEOUT_MS = 10 * 60 * 1000 // 10 min
class SyncWorker {
constructor(account, db, parentManager) {
@ -422,7 +423,7 @@ class SyncWorker {
// Encountered a permanent error
// In this case we could do something fancier, but for now, just retry
// with the normal sync loop interval
interval = SYNC_LOOP_INTERVAL_MS
interval = AC_SYNC_LOOP_INTERVAL_MS;
}
} else {
// Continue syncing immediately if initial sync isn't done, or if the loop was
@ -432,7 +433,13 @@ class SyncWorker {
this._interrupted ||
this._requireTokenRefresh
)
interval = shouldSyncImmediately ? 1 : SYNC_LOOP_INTERVAL_MS;
if (shouldSyncImmediately) {
interval = 1;
} else if (BatteryStatusManager.isBatteryCharging()) {
interval = AC_SYNC_LOOP_INTERVAL_MS;
} else {
interval = BATTERY_SYNC_LOOP_INTERVAL_MS;
}
}

View file

@ -8,7 +8,7 @@ const extractFiles = require('./extract-files');
const extractContacts = require('./extract-contacts');
const MessageFactory = require('../shared/message-factory')
const LocalDatabaseConnector = require('../shared/local-database-connector');
const {BatteryStatusManager} = require('nylas-exports');
const MAX_QUEUE_LENGTH = 500
const MAX_CPU_USE_ON_AC = 1.0;
@ -23,14 +23,6 @@ class MessageProcessor {
this._queueLength = 0
this._currentChunkSize = 0
this._currentChunkStart = Date.now();
this._isBatteryCharging = true;
navigator.getBattery().then((battery) => {
this._isBatteryCharging = battery.charging;
battery.addEventListener('chargingchange', () => {
console.info('charge change', battery.charging);
this._isBatteryCharging = battery.charging
});
});
}
queueLength() {
@ -42,7 +34,7 @@ class MessageProcessor {
}
_maxCPUForProcessing() {
if (this._isBatteryCharging) {
if (BatteryStatusManager.isBatteryCharging()) {
return MAX_CPU_USE_ON_AC;
}
return MAX_CPU_USE_ON_BATTERY;