diff --git a/package.json b/package.json index 9c819e6c..2824452b 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "author": "Calvin Montgomery", "name": "CyTube", "description": "Online media synchronizer and chat", - "version": "3.58.1", + "version": "3.58.2", "repository": { "url": "http://github.com/calzoneman/sync" }, diff --git a/src/bgtask.js b/src/bgtask.js index ab27429c..0d25acf0 100644 --- a/src/bgtask.js +++ b/src/bgtask.js @@ -51,13 +51,19 @@ function initChannelDumper(Server) { var wait = CHANNEL_SAVE_INTERVAL / Server.channels.length; LOGGER.info(`Saving channels with delay ${wait}`); Promise.reduce(Server.channels, (_, chan) => { - return Promise.delay(wait).then(() => { + return Promise.delay(wait).then(async () => { if (!chan.dead && chan.users && chan.users.length > 0) { - return chan.saveState().tap(() => { + try { + await chan.saveState(); LOGGER.info(`Saved /${chanPath}/${chan.name}`); - }).catch(err => { - LOGGER.error(`Failed to save /${chanPath}/${chan.name}: ${err.stack}`); - }); + } catch (error) { + LOGGER.error( + 'Failed to save /%s/%s: %s', + chanPath, + chan ? chan.name : '', + error.stack + ); + } } }).catch(error => { LOGGER.error(`Failed to save channel: ${error.stack}`); diff --git a/src/channel/channel.js b/src/channel/channel.js index e0f4071d..34d20554 100644 --- a/src/channel/channel.js +++ b/src/channel/channel.js @@ -9,8 +9,6 @@ import { ChannelStateSizeError } from '../errors'; import { EventEmitter } from 'events'; import { throttle } from '../util/throttle'; import Logger from '../logger'; -// Not directly used, but needs to be in scope for async functions -import Promise from 'bluebird'; const LOGGER = require('@calzoneman/jsli')('channel'); diff --git a/src/server.js b/src/server.js index 7e2486ab..33d5227e 100644 --- a/src/server.js +++ b/src/server.js @@ -377,15 +377,17 @@ Server.prototype.setAnnouncement = function (data) { }; Server.prototype.forceSave = function () { - Promise.map(this.channels, channel => { + Promise.map(this.channels, async channel => { try { - return channel.saveState().tap(() => { - LOGGER.info(`Saved /${this.chanPath}/${channel.name}`); - }).catch(err => { - LOGGER.error(`Failed to save /${this.chanPath}/${channel.name}: ${err.stack}`); - }); + await channel.saveState(); + LOGGER.info(`Saved /${this.chanPath}/${channel.name}`); } catch (error) { - LOGGER.error(`Failed to save channel: ${error.stack}`); + LOGGER.error( + 'Failed to save /%s/%s: %s', + this.chanPath, + channel ? channel.name : '', + error.stack + ); } }, { concurrency: 5 }).then(() => { LOGGER.info('Finished save'); @@ -394,15 +396,17 @@ Server.prototype.forceSave = function () { Server.prototype.shutdown = function () { LOGGER.info("Unloading channels"); - Promise.map(this.channels, channel => { + Promise.map(this.channels, async channel => { try { - return channel.saveState().tap(() => { - LOGGER.info(`Saved /${this.chanPath}/${channel.name}`); - }).catch(err => { - LOGGER.error(`Failed to save /${this.chanPath}/${channel.name}: ${err.stack}`); - }); + await channel.saveState(); + LOGGER.info(`Saved /${this.chanPath}/${channel.name}`); } catch (error) { - LOGGER.error(`Failed to save channel: ${error.stack}`); + LOGGER.error( + 'Failed to save /%s/%s: %s', + this.chanPath, + channel ? channel.name : '', + error.stack + ); } }, { concurrency: 5 }).then(() => { LOGGER.info("Goodbye"); @@ -415,16 +419,23 @@ Server.prototype.shutdown = function () { Server.prototype.handlePartitionMapChange = function () { const channels = Array.prototype.slice.call(this.channels); - Promise.map(channels, channel => { + Promise.map(channels, async channel => { if (channel.dead) { return; } if (!this.partitionDecider.isChannelOnThisPartition(channel.uniqueName)) { LOGGER.info("Partition changed for " + channel.uniqueName); - return channel.saveState().then(() => { - channel.broadcastAll("partitionChange", - this.partitionDecider.getPartitionForChannel(channel.uniqueName)); + try { + await channel.saveState(); + + channel.broadcastAll( + "partitionChange", + this.partitionDecider.getPartitionForChannel( + channel.uniqueName + ) + ); + const users = Array.prototype.slice.call(channel.users); users.forEach(u => { try { @@ -433,11 +444,16 @@ Server.prototype.handlePartitionMapChange = function () { // Ignore } }); + this.unloadChannel(channel, { skipSave: true }); - }).catch(error => { - LOGGER.error(`Failed to unload /${this.chanPath}/${channel.name} for ` + - `partition map flip: ${error.stack}`); - }); + } catch (error) { + LOGGER.error( + 'Failed to unload /%s/%s for partition map flip: %s', + this.chanPath, + channel ? channel.name : '', + error.stack + ); + } } }, { concurrency: 5 }).then(() => { LOGGER.info("Partition reload complete");