2016-07-14 02:11:32 +08:00
|
|
|
const {
|
|
|
|
DatabaseConnector,
|
|
|
|
PubsubConnector,
|
|
|
|
SchedulerUtils,
|
|
|
|
} = require(`nylas-core`);
|
|
|
|
|
|
|
|
function onWebsocketConnected(wss, ws) {
|
2016-07-14 03:35:30 +08:00
|
|
|
let toSend;
|
|
|
|
function resetToSend() {
|
|
|
|
toSend = {
|
|
|
|
updatedAccounts: [],
|
|
|
|
activeAccountIds: [],
|
|
|
|
assignments: {},
|
2016-07-15 05:51:48 +08:00
|
|
|
processLoadCounts: {},
|
2016-07-14 03:35:30 +08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
resetToSend();
|
|
|
|
|
|
|
|
function sendUpdate() {
|
|
|
|
ws.send(JSON.stringify({cmd: "UPDATE", payload: toSend}));
|
|
|
|
resetToSend();
|
|
|
|
}
|
|
|
|
|
2016-07-14 02:11:32 +08:00
|
|
|
DatabaseConnector.forShared().then(({Account}) => {
|
|
|
|
Account.findAll().then((accounts) => {
|
|
|
|
accounts.forEach((acct) => {
|
2016-07-14 03:35:30 +08:00
|
|
|
toSend.updatedAccounts.push(acct);
|
|
|
|
if (toSend.updatedAccounts.length >= 50) {
|
|
|
|
sendUpdate();
|
|
|
|
}
|
2016-07-14 02:11:32 +08:00
|
|
|
});
|
2016-07-14 03:35:30 +08:00
|
|
|
sendUpdate();
|
2016-07-14 02:11:32 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
this.observable = PubsubConnector.observeAllAccounts().subscribe((accountId) => {
|
|
|
|
Account.find({where: {id: accountId}}).then((acct) => {
|
2016-07-14 03:35:30 +08:00
|
|
|
toSend.updatedAccounts.push(acct);
|
2016-07-14 02:11:32 +08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
this.pollInterval = setInterval(() => {
|
2016-07-14 03:35:30 +08:00
|
|
|
const getActiveIds = SchedulerUtils.listActiveAccounts().then((accountIds) => {
|
|
|
|
toSend.activeAccountIds = accountIds;
|
2016-07-14 02:11:32 +08:00
|
|
|
});
|
2016-07-14 03:35:30 +08:00
|
|
|
const getAssignments = SchedulerUtils.forEachAccountList((identity, accountIds) => {
|
2016-07-15 05:51:48 +08:00
|
|
|
toSend.processLoadCounts[identity] = accountIds.length;
|
2016-07-14 02:11:32 +08:00
|
|
|
for (const accountId of accountIds) {
|
2016-07-14 03:35:30 +08:00
|
|
|
toSend.assignments[accountId] = identity;
|
2016-07-14 02:11:32 +08:00
|
|
|
}
|
2016-07-14 03:35:30 +08:00
|
|
|
})
|
|
|
|
|
|
|
|
Promise.all([getActiveIds, getAssignments]).then(() => {
|
|
|
|
sendUpdate();
|
|
|
|
})
|
2016-07-14 02:11:32 +08:00
|
|
|
}, 1000);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
function onWebsocketDisconnected() {
|
|
|
|
clearInterval(this.pollInterval);
|
|
|
|
this.observable.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
function onWebsocketConnectedFake(wss, ws) {
|
|
|
|
const accts = [];
|
2016-07-14 03:35:30 +08:00
|
|
|
for (let ii = 0; ii < 100; ii++) {
|
2016-07-14 02:11:32 +08:00
|
|
|
const acct = {
|
|
|
|
id: ii,
|
|
|
|
email_address: `halla+${ii}@nylas.com`,
|
|
|
|
object: "account",
|
|
|
|
organization_unit: "folder",
|
|
|
|
provider: "imap",
|
|
|
|
connection_settings: {
|
|
|
|
imap_host: "imap.mail.me.com",
|
|
|
|
imap_port: 993,
|
|
|
|
smtp_host: "smtp.mail.me.com",
|
|
|
|
smtp_port: 0,
|
|
|
|
ssl_required: true,
|
|
|
|
},
|
|
|
|
sync_policy: {
|
|
|
|
afterSync: "idle",
|
|
|
|
intervals: {
|
|
|
|
active: 30000,
|
|
|
|
inactive: 300000,
|
|
|
|
},
|
|
|
|
folderSyncOptions: {
|
|
|
|
deepFolderScan: 600000,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
sync_error: null,
|
|
|
|
first_sync_completion: 0,
|
|
|
|
last_sync_completions: [],
|
|
|
|
created_at: "2016-07-13T00:49:25.000Z",
|
|
|
|
};
|
2016-07-14 05:53:56 +08:00
|
|
|
ws.send(JSON.stringify({ cmd: "UPDATE", payload: {
|
|
|
|
updatedAccounts: [acct],
|
|
|
|
activeAccountIds: [],
|
|
|
|
assignments: {},
|
|
|
|
}}));
|
2016-07-14 02:11:32 +08:00
|
|
|
accts.push(acct);
|
|
|
|
}
|
|
|
|
setInterval(() => {
|
|
|
|
const acct = accts[Math.floor(Math.random() * accts.length)];
|
2016-07-14 05:53:56 +08:00
|
|
|
ws.send(JSON.stringify({ cmd: "UPDATE", payload: {
|
|
|
|
updatedAccounts: [acct],
|
|
|
|
activeAccountIds: [],
|
|
|
|
assignments: {},
|
|
|
|
}}));
|
2016-07-14 02:11:32 +08:00
|
|
|
}, 250);
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = (server) => {
|
|
|
|
server.route({
|
|
|
|
method: "POST",
|
|
|
|
path: "/websocket",
|
|
|
|
config: {
|
|
|
|
plugins: {
|
|
|
|
websocket: {
|
|
|
|
only: true,
|
|
|
|
connect: onWebsocketConnected,
|
|
|
|
disconnect: onWebsocketDisconnected,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
handler: (request, reply) => {
|
|
|
|
if (request.payload.cmd === "PING") {
|
|
|
|
reply(JSON.stringify({ result: "PONG" }));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|