Idle detector: Fix bug preventing it from ever turning off (#2487)

This commit is contained in:
Scott Nonnenberg 2018-07-03 15:02:58 -07:00 committed by GitHub
parent bd13939548
commit 125c3fafa8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 3 deletions

View file

@ -78,6 +78,7 @@
let isMigrationWithIndexComplete = false; let isMigrationWithIndexComplete = false;
let isMigrationWithoutIndexComplete = false; let isMigrationWithoutIndexComplete = false;
idleDetector.on('idle', async () => { idleDetector.on('idle', async () => {
console.log('Idle processing started');
const NUM_MESSAGES_PER_BATCH = 1; const NUM_MESSAGES_PER_BATCH = 1;
if (!isMigrationWithIndexComplete) { if (!isMigrationWithIndexComplete) {
@ -108,6 +109,7 @@
const areAllMigrationsComplete = const areAllMigrationsComplete =
isMigrationWithIndexComplete && isMigrationWithoutIndexComplete; isMigrationWithIndexComplete && isMigrationWithoutIndexComplete;
if (areAllMigrationsComplete) { if (areAllMigrationsComplete) {
console.log('All migrations are complete. Stopping idle detector.');
idleDetector.stop(); idleDetector.stop();
} }
}); });

View file

@ -18,6 +18,10 @@ class IdleDetector extends EventEmitter {
} }
stop() { stop() {
if (!this.handle) {
return;
}
console.log('Stop idle detector'); console.log('Stop idle detector');
this._clearScheduledCallbacks(); this._clearScheduledCallbacks();
} }
@ -25,10 +29,12 @@ class IdleDetector extends EventEmitter {
_clearScheduledCallbacks() { _clearScheduledCallbacks() {
if (this.handle) { if (this.handle) {
cancelIdleCallback(this.handle); cancelIdleCallback(this.handle);
this.handle = null;
} }
if (this.timeoutId) { if (this.timeoutId) {
clearTimeout(this.timeoutId); clearTimeout(this.timeoutId);
this.timeoutId = null;
} }
} }
@ -38,13 +44,13 @@ class IdleDetector extends EventEmitter {
const { didTimeout } = deadline; const { didTimeout } = deadline;
const timeRemaining = deadline.timeRemaining(); const timeRemaining = deadline.timeRemaining();
const isIdle = timeRemaining >= IDLE_THRESHOLD_MS; const isIdle = timeRemaining >= IDLE_THRESHOLD_MS;
if (isIdle || didTimeout) {
this.emit('idle', { timestamp: Date.now(), didTimeout, timeRemaining });
}
this.timeoutId = setTimeout( this.timeoutId = setTimeout(
() => this._scheduleNextCallback(), () => this._scheduleNextCallback(),
POLL_INTERVAL_MS POLL_INTERVAL_MS
); );
if (isIdle || didTimeout) {
this.emit('idle', { timestamp: Date.now(), didTimeout, timeRemaining });
}
}); });
} }
} }