[local-sync] Add global dbs and cleanup orphan messages

This commit is contained in:
Evan Morikawa 2016-12-07 16:36:52 -08:00
parent 878735f52e
commit d161a30a34
2 changed files with 19 additions and 1 deletions

View file

@ -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

View file

@ -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()