Add a few safeguards around channel saving

This commit is contained in:
Calvin Montgomery 2016-12-20 00:09:24 -08:00
parent d21943ecc7
commit f6c201f3ba
4 changed files with 26 additions and 8 deletions

View file

@ -63,15 +63,24 @@ function initChannelDumper(Server) {
* 60000;
setInterval(function () {
var wait = CHANNEL_SAVE_INTERVAL / Server.channels.length;
Logger.syslog.log(`Saving channels with delay ${wait}`);
Promise.reduce(Server.channels, (_, chan) => {
return Promise.delay(wait).then(() => {
if (chan.name === 'test')
throw new TypeError('Whoops fucked up there');
if (!chan.dead && chan.users && chan.users.length > 0) {
return chan.saveState().catch(err => {
return chan.saveState().tap(() => {
Logger.syslog.log(`Saved /r/${chan.name}`);
}).catch(err => {
Logger.errlog.log(`Failed to save /r/${chan.name}: ${err.stack}`);
});
}
}).catch(error => {
Logger.errlog.log(`Failed to save channel: ${error.stack}`);
});
}, 0);
}, 0).catch(error => {
Logger.errlog.log(`Failed to save channels: ${error.stack}`);
});
}, CHANNEL_SAVE_INTERVAL);
}

View file

@ -319,11 +319,15 @@ Server.prototype.setAnnouncement = function (data) {
Server.prototype.shutdown = function () {
Logger.syslog.log("Unloading channels");
Promise.map(this.channels, channel => {
return channel.saveState().tap(() => {
Logger.syslog.log(`Saved /r/${channel.name}`);
}).catch(err => {
Logger.errlog.log(`Failed to save /r/${channel.name}: ${err.stack}`);
});
try {
return channel.saveState().tap(() => {
Logger.syslog.log(`Saved /r/${channel.name}`);
}).catch(err => {
Logger.errlog.log(`Failed to save /r/${channel.name}: ${err.stack}`);
});
} catch (error) {
Logger.errlog.log(`Failed to save channel: ${error.stack}`);
}
}, { concurrency: 5 }).then(() => {
Logger.syslog.log("Goodbye");
process.exit(0);