diff --git a/channel.js b/channel.js index 5e082463..513fc90a 100644 --- a/channel.js +++ b/channel.js @@ -17,7 +17,8 @@ var Media = require("./media.js").Media; var formatTime = require("./media.js").formatTime; var Logger = require("./logger.js"); var InfoGetter = require("./get-info.js"); -var io = require("./server.js").io; +var Server = require("./server.js"); +var io = Server.io; var Rank = require("./rank.js"); var Auth = require("./auth.js"); var ChatCommand = require("./chatcommand.js"); @@ -389,6 +390,10 @@ Channel.prototype.userLeave = function(user) { }); } this.logger.log("--- /" + user.ip + " (" + user.name + ") left"); + if(this.users.length == 0) { + this.logger.log("*** Channel empty, unloading"); + Server.unload(this); + } } Channel.prototype.kick = function(user, reason) { @@ -618,7 +623,8 @@ function mediaUpdate(chan, id) { // Bail cases - video changed, someone's leader, no video playing if(chan.media == null || id != chan.media.id || - chan.leader != null) { + chan.leader != null || + chan.users.length == 0) { return; } diff --git a/server.js b/server.js index 7fd1d33f..577bced1 100644 --- a/server.js +++ b/server.js @@ -114,8 +114,6 @@ process.on("uncaughtException", function(err) { process.on("exit", shutdown); process.on("SIGINT", shutdown); - - function shutdown() { Logger.syslog.log("Unloading channels..."); for(var name in exports.channels) { @@ -125,3 +123,10 @@ function shutdown() { Logger.syslog.log("Shutting Down"); process.exit(0); } + +exports.unload = function(chan) { + if(chan.registered) { + chan.saveDump(); + } + delete exports.channels[chan.name]; +}