From d161a30a34c237825c4b784d16fe2f2acfa09627 Mon Sep 17 00:00:00 2001 From: Evan Morikawa Date: Wed, 7 Dec 2016 16:36:52 -0800 Subject: [PATCH] [local-sync] Add global dbs and cleanup orphan messages --- .../src/local-sync-worker/sync-process-manager.js | 14 +++++++++++++- .../src/local-sync-worker/sync-worker.js | 6 ++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/packages/local-sync/src/local-sync-worker/sync-process-manager.js b/packages/local-sync/src/local-sync-worker/sync-process-manager.js index 00f9bc892..a28a09f44 100644 --- a/packages/local-sync/src/local-sync-worker/sync-process-manager.js +++ b/packages/local-sync/src/local-sync-worker/sync-process-manager.js @@ -1,3 +1,4 @@ +const _ = require('underscore') const SyncWorker = require('./sync-worker'); const LocalDatabaseConnector = require('../shared/local-database-connector') @@ -28,9 +29,13 @@ class SyncProcessManager { this._workers = {}; this._listenForSyncsClient = null; this._exiting = false; + this._accounts = [] this._logger = global.Logger.child(); } + /** + * Useful for debugging. + */ async start() { this._logger.info(`ProcessManager: Starting with ID`) @@ -41,6 +46,10 @@ class SyncProcessManager { } } + accounts() { return this._accounts } + workers() { return _.values(this._workers) } + dbs() { return this.workers().map(w => w._db) } + wakeWorkerForAccount(account) { this._workers[account.id].syncNow(); } @@ -53,6 +62,7 @@ class SyncProcessManager { if (this._workers[account.id]) { throw new Error("Local worker already exists"); } + this._accounts.push(account) this._workers[account.id] = new SyncWorker(account, db, this); this._logger.info({account_id: account.id}, `ProcessManager: Claiming Account Succeeded`) } catch (err) { @@ -68,4 +78,6 @@ class SyncProcessManager { } } -module.exports = new SyncProcessManager() +window.syncProcessManager = new SyncProcessManager(); +window.dbs = window.syncProcessManager.dbs.bind(window.syncProcessManager) +module.exports = window.syncProcessManager 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 d36900023..c86b9697c 100644 --- a/packages/local-sync/src/local-sync-worker/sync-worker.js +++ b/packages/local-sync/src/local-sync-worker/sync-worker.js @@ -236,6 +236,7 @@ class SyncWorker { await this.runNewSyncbackRequests(); await this._conn.runOperation(new FetchFolderList(this._account.provider, this._logger)); await this.syncMessagesInAllFolders(); + await this.cleanupOrpahnMessages(); await this.onSyncDidComplete(); } catch (error) { this.onSyncError(error); @@ -245,6 +246,11 @@ class SyncWorker { } } + async cleanupOrpahnMessages() { + const orphans = await this._db.Message.findAll({where: {folderId: null}}) + return Promise.map(orphans, (msg) => msg.destroy()); + } + onSyncError(error) { this.closeConnection()