From 77b1f3c50569070bfafeb63a8f0f9b455695dad8 Mon Sep 17 00:00:00 2001 From: Daniel Gasienica Date: Mon, 2 Apr 2018 19:01:30 -0400 Subject: [PATCH] Replace startup with attachment background migration Iterates through `Message::id` index. --- js/background.js | 28 +++++++++++----------------- js/modules/idle_detector.js | 12 +++++++++--- js/modules/messages_data_migrator.js | 3 +++ 3 files changed, 23 insertions(+), 20 deletions(-) diff --git a/js/background.js b/js/background.js index 00e96ddbc..ee77301f4 100644 --- a/js/background.js +++ b/js/background.js @@ -86,29 +86,23 @@ console.log('Run migrations on database with attachment data'); await Migrations0DatabaseWithAttachmentData.run({ Backbone }); - await MessageDataMigrator.dangerouslyProcessAllWithoutIndex({ - databaseName: Migrations0DatabaseWithAttachmentData.getDatabase().name, - minDatabaseVersion: Migrations0DatabaseWithAttachmentData.getDatabase().version, - upgradeMessageSchema, - }); - console.log('Storage fetch'); storage.fetch(); const idleDetector = new IdleDetector(); idleDetector.on('idle', async () => { - // const database = Migrations0DatabaseWithAttachmentData.getDatabase(); - // const batch = await MessageDataMigrator.processNextBatchWithoutIndex({ - // databaseName: database.name, - // minDatabaseVersion: database.version, - // upgradeMessageSchema, - // }); - // console.log('Upgrade message schema:', batch); - - // if (batch.done) { - // idleDetector.stop(); - // } + const database = Migrations0DatabaseWithAttachmentData.getDatabase(); + const batch = await MessageDataMigrator.processNextBatchWithoutIndex({ + databaseName: database.name, + minDatabaseVersion: database.version, + upgradeMessageSchema, + }); + console.log('Upgrade message schema:', batch); + + if (batch.done) { + idleDetector.stop(); + } }); /* eslint-disable */ diff --git a/js/modules/idle_detector.js b/js/modules/idle_detector.js index 9a2ff52b8..e72b5bfe8 100644 --- a/js/modules/idle_detector.js +++ b/js/modules/idle_detector.js @@ -3,8 +3,8 @@ const EventEmitter = require('events'); -const POLL_INTERVAL_MS = 30 * 1000; -const IDLE_THRESHOLD_MS = 25; +const POLL_INTERVAL_MS = 15 * 1000; +const IDLE_THRESHOLD_MS = 20; class IdleDetector extends EventEmitter { constructor() { @@ -14,10 +14,16 @@ class IdleDetector extends EventEmitter { } start() { + console.log('Start idle detector'); this._scheduleNextCallback(); } stop() { + console.log('Stop idle detector'); + this._clearScheduledCallbacks(); + } + + _clearScheduledCallbacks() { if (this.handle) { cancelIdleCallback(this.handle); } @@ -28,7 +34,7 @@ class IdleDetector extends EventEmitter { } _scheduleNextCallback() { - this.stop(); + this._clearScheduledCallbacks(); this.handle = window.requestIdleCallback((deadline) => { const { didTimeout } = deadline; const timeRemaining = deadline.timeRemaining(); diff --git a/js/modules/messages_data_migrator.js b/js/modules/messages_data_migrator.js index 7a210d74c..177cbb527 100644 --- a/js/modules/messages_data_migrator.js +++ b/js/modules/messages_data_migrator.js @@ -237,7 +237,10 @@ const _processBatch = async ({ connection, upgradeMessageSchema } = {}) => { await settings.deleteAttachmentMigrationLastProcessedIndex(connection); } + const batchTotalDuration = Date.now() - fetchUnprocessedMessagesStartTime; + return { + batchTotalDuration, done, fetchDuration, lastProcessedIndex,