mirror of
https://github.com/Foundry376/Mailspring.git
synced 2024-11-13 21:24:58 +08:00
ed749e0f51
- Handles sync errors in a single place. For now, if error is not a socket error, will treat as a permanent error, save the error to the account object, and prevent any other syncing until the error is cleared from the account object - Adds a NylasError class that can be extended and serialized. Adds it to global namespace on all packages and replaces all uses of regular Error
76 lines
2.4 KiB
JavaScript
76 lines
2.4 KiB
JavaScript
const Hapi = require('hapi');
|
|
const HapiWebSocket = require('hapi-plugin-websocket');
|
|
const Inert = require('inert');
|
|
const {DatabaseConnector, PubsubConnector, SchedulerUtils, NylasError} = require(`nylas-core`);
|
|
const {forEachAccountList} = SchedulerUtils;
|
|
|
|
global.Promise = require('bluebird');
|
|
global.NylasError = NylasError;
|
|
|
|
const server = new Hapi.Server();
|
|
server.connection({ port: process.env.PORT / 1 + 1 || 5101 });
|
|
|
|
DatabaseConnector.forShared().then(({Account}) => {
|
|
server.register([HapiWebSocket, Inert], () => {
|
|
server.route({
|
|
method: "POST",
|
|
path: "/accounts",
|
|
config: {
|
|
plugins: {
|
|
websocket: {
|
|
only: true,
|
|
connect: (wss, ws) => {
|
|
Account.findAll().then((accts) => {
|
|
accts.forEach((acct) => {
|
|
ws.send(JSON.stringify({ cmd: "ACCOUNT", payload: acct }));
|
|
});
|
|
});
|
|
this.redis = PubsubConnector.buildClient();
|
|
this.redis.on('pmessage', (pattern, channel) => {
|
|
Account.find({where: {id: channel.replace('a-', '')}}).then((acct) => {
|
|
ws.send(JSON.stringify({ cmd: "ACCOUNT", payload: acct }));
|
|
});
|
|
});
|
|
this.redis.psubscribe(PubsubConnector.channelForAccount('*'));
|
|
this.assignmentsInterval = setInterval(() => {
|
|
const assignments = {};
|
|
forEachAccountList((identity, accountIds) => {
|
|
for (const accountId of accountIds) {
|
|
assignments[accountId] = identity;
|
|
}
|
|
}).then(() =>
|
|
ws.send(JSON.stringify({ cmd: "ASSIGNMENTS", payload: assignments}))
|
|
)
|
|
}, 1000);
|
|
},
|
|
disconnect: () => {
|
|
clearInterval(this.assignmentsInterval);
|
|
this.redis.quit();
|
|
},
|
|
},
|
|
},
|
|
},
|
|
handler: (request, reply) => {
|
|
if (request.payload.cmd === "PING") {
|
|
reply(JSON.stringify({ result: "PONG" }));
|
|
return;
|
|
}
|
|
},
|
|
});
|
|
|
|
server.route({
|
|
method: 'GET',
|
|
path: '/{param*}',
|
|
handler: {
|
|
directory: {
|
|
path: require('path').join(__dirname, 'public'),
|
|
},
|
|
},
|
|
});
|
|
|
|
server.start((startErr) => {
|
|
if (startErr) { throw startErr; }
|
|
console.log('Dashboard running at:', server.info.uri);
|
|
});
|
|
});
|
|
});
|