syncbackMessageActions refactor

This commit is contained in:
Evan Morikawa 2016-06-28 17:12:45 -07:00
parent b867bcb31e
commit cea7783a5f
5 changed files with 24 additions and 24 deletions

View file

@ -1,7 +1,11 @@
module.exports = (sequelize, Sequelize) => { module.exports = (sequelize, Sequelize) => {
const SyncbackRequest = sequelize.define('SyncbackRequest', { const SyncbackRequest = sequelize.define('SyncbackRequest', {
type: Sequelize.STRING, type: Sequelize.STRING,
status: Sequelize.STRING, status: {
type: Sequelize.ENUM("NEW", "SUCCEEDED", "FAILED"),
defaultValue: "NEW",
allowNull: false,
},
error: { error: {
type: Sequelize.STRING, type: Sequelize.STRING,
get: function get() { get: function get() {

View file

@ -55,7 +55,7 @@ class PubsubConnector {
} }
notify({accountId, type, data}) { notify({accountId, type, data}) {
this.broadcastClient().publish(`channel-${accountId}`, {type, data}); this.broadcastClient().publish(`channel-${accountId}`, JSON.stringify({type, data}));
} }
observe(accountId) { observe(accountId) {

View file

@ -40,14 +40,15 @@ class SyncWorker {
this._conn = null this._conn = null
} }
_onMessage(msg = {}) { _onMessage(msg) {
switch(msg.type) { const {type, data} = JSON.parse(msg)
switch(type) {
case MessageTypes.ACCOUNT_UPDATED: case MessageTypes.ACCOUNT_UPDATED:
this._onAccountUpdated(); break; this._onAccountUpdated(); break;
case MessageTypes.SYNCBACK_REQUESTED: case MessageTypes.SYNCBACK_REQUESTED:
this.syncNow(); break; this.syncNow(); break;
default: default:
throw new Error(`Invalid message: ${JSON.stringify(msg)}`) throw new Error(`Invalid message: ${msg}`)
} }
} }
@ -121,20 +122,11 @@ class SyncWorker {
return this._conn.connect(); return this._conn.connect();
} }
fetchCategoryList() {
return this._conn.runOperation(new FetchCategoryList())
}
syncbackMessageActions() { syncbackMessageActions() {
return this._getAccount((account) => const where = {where: {status: "NEW"}, limit: 100};
Promise.each(this._db.SyncbackRequest.findAll().then((reqs = []) => return this._db.SyncbackRequest.findAll(where)
reqs.map((request) => { .map((req) => SyncbackTaskFactory.create(this._account, req))
const task = SyncbackTaskFactory.create(account, request); .each(this._conn.runOperation)
if (!task) return Promise.reject("No Task")
return this._conn.runOperation(task)
})
))
)
} }
syncAllCategories() { syncAllCategories() {
@ -163,7 +155,7 @@ class SyncWorker {
} }
performSync() { performSync() {
return this.fetchCategoryList() return this._conn.runOperation(new FetchCategoryList())
.then(() => this.syncbackMessageActions()) .then(() => this.syncbackMessageActions())
.then(() => this.syncAllCategories()) .then(() => this.syncAllCategories())
} }

View file

@ -4,12 +4,14 @@
*/ */
class SyncbackTaskFactory { class SyncbackTaskFactory {
static create(account, syncbackRequest) { static create(account, syncbackRequest) {
if (syncbackRequest.type === "MoveToFolder") { let Task = null;
// TODO: Know it's IMAP from the account object. switch(syncbackRequest.type) {
const Task = require('./syncback_tasks/move-to-folder.imap'); case "MoveToFolder":
return new Task(account, syncbackRequest) Task = require('./syncback_tasks/move-to-folder.imap'); break;
default:
throw new Error(`Invalid Task Type: ${syncbackRequest.type}`)
} }
return null return new Task(account, syncbackRequest)
} }
} }

View file

@ -9,6 +9,8 @@ class MoveToFolderIMAP {
} }
run(db, imap) { run(db, imap) {
console.log("RUNNING MOVE TO FOLDER IMAP");
return Promise.resolve();
} }
} }
module.exports = MoveToFolderIMAP module.exports = MoveToFolderIMAP