From 4f06269b136821e0a3b9c91ff95d64525a57b356 Mon Sep 17 00:00:00 2001 From: Darren Date: Fri, 26 Aug 2011 14:05:27 +0100 Subject: [PATCH] Kiwi modules reloading --- node/app.js | 18 +++++++--- node/kiwi.js | 6 ++-- node/kiwi_modules/statistics.js | 10 +++++- node/lib/kiwi_mod.js | 62 ++++++++++++++++++++++++++++----- 4 files changed, 78 insertions(+), 18 deletions(-) diff --git a/node/app.js b/node/app.js index 2cb3513..cc2180f 100644 --- a/node/app.js +++ b/node/app.js @@ -590,7 +590,9 @@ this.websocketConnection = function (websocket) { con.sockets.push(websocket); websocket.sendClientEvent = function (event_name, data) { - kiwi.kiwi_mod.run(event_name, data, {websocket: this}); + var ev = kiwi.kiwi_mod.run(event_name, data, {websocket: this}); + if(ev === null) return; + data.event = event_name; websocket.emit('message', data); }; @@ -798,9 +800,8 @@ this.rehash = function () { this.startControll = function () { process.stdin.resume(); process.stdin.on('data', function (chunk) { - var command; - command = chunk.toString().trim(); - switch (command) { + var parts = chunk.toString().trim().split(' '); + switch (parts[0]) { case 'rehash': console.log('Rehashing...'); console.log(kiwi.rehash() ? 'Rehash complete' : 'Rehash failed'); @@ -811,8 +812,15 @@ this.startControll = function () { console.log(kiwi.recode() ? 'Recode complete' : 'Recode failed'); break; + case 'mod': + if (parts[1] === 'reload') { + console.log('Reloading module (' + parts[2] + ')..'); + kiwi.kiwi_mod.reloadModule(parts[2]); + } + break; + default: - console.log('Unknown command \'' + command + '\''); + console.log('Unknown command \'' + parts[0] + '\''); } }); }; \ No newline at end of file diff --git a/node/kiwi.js b/node/kiwi.js index 032b560..189301f 100644 --- a/node/kiwi.js +++ b/node/kiwi.js @@ -29,7 +29,7 @@ this.kiwi_root = __dirname; * Configuration and rehashing routines */ var config_filename = 'config.json', - config_dirs = ['/etc/kiwiirc/', __dirname + '/']; + config_dirs = ['/etc/kiwiirc/', this.kiwi_root + '/']; this.config = {}; this.loadConfig = function () { @@ -83,8 +83,8 @@ this.rehash = function () { // Reloads app.js during runtime for any recoding this.recode = function () { - if (typeof require.cache[__dirname + '/app.js'] !== 'undefined'){ - delete require.cache[__dirname + '/app.js']; + if (typeof require.cache[this.kiwi_root + '/app.js'] !== 'undefined'){ + delete require.cache[this.kiwi_root + '/app.js']; } app = null; diff --git a/node/kiwi_modules/statistics.js b/node/kiwi_modules/statistics.js index 9f6911a..09b4546 100644 --- a/node/kiwi_modules/statistics.js +++ b/node/kiwi_modules/statistics.js @@ -3,13 +3,21 @@ * This is by no means is a production ready module. */ +var kiwi = require('../kiwi.js'); var stats = {msgs: 0, topic_changes: 0}; exports.onmsgsend = function (msg, opts) { stats.msgs++; + var connections_cnt = 0; + for (var i in kiwi.connections) { + connections_cnt = connections_cnt + parseInt(kiwi.connections[i].count, 10); + } + if (msg.msg === '!kiwistats') { - msg.msg = 'Messages sent: ' + stats.msgs.toString() + '. '; + msg.msg = ''; + msg.msg += 'Connections: ' + connections_cnt.toString() + '. '; + msg.msg += 'Messages sent: ' + stats.msgs.toString() + '. '; msg.msg += 'Topics set: ' + stats.topic_changes.toString() + '. '; opts.websocket.sendClientEvent('msg', {nick: msg.target, ident: '', hostname: '', channel: msg.target, msg: msg.msg}); diff --git a/node/lib/kiwi_mod.js b/node/lib/kiwi_mod.js index bb29783..2e23c1b 100644 --- a/node/lib/kiwi_mod.js +++ b/node/lib/kiwi_mod.js @@ -10,38 +10,82 @@ * and null is passed back to the caller to take action. * For example, if null is returned for onmsg, kiwi stops sending * the message to any clients. -*/ + */ var kiwi = require('../kiwi.js'); +var fs = require('fs'); this.loaded_modules = {}; +/* + * Load any unloaded modules as set in config + */ exports.loadModules = function (kiwi_root, config) { var i, mod_name; // Warn each module it is about to be unloaded - this.run('unload'); - this.loaded_modules = {}; + //this.run('unload'); + //this.loaded_modules = {}; // Load each module and run the onload event for (i in kiwi.config.modules) { mod_name = kiwi.config.modules[i]; - this.loaded_modules[mod_name] = require(kiwi_root + '/' + kiwi.config.module_dir + mod_name); + if (typeof this.loaded_modules[mod_name] !== 'undefined') continue; + + this.loaded_modules[mod_name] = require(kiwi.kiwi_root + '/' + kiwi.config.module_dir + mod_name); } this.run('load'); }; + +/* + * Unload and reload a specific module + */ +exports.reloadModule = function (mod_name) { + fs.realpath(kiwi.kiwi_root + '/' + kiwi.config.module_dir + mod_name + '.js', function(err, resolvedPath){ + try { + var mod_path = resolvedPath; + + if (typeof kiwi.kiwi_mod.loaded_modules[mod_name] !== 'undefined') { + delete kiwi.kiwi_mod.loaded_modules[mod_name]; + } + if (typeof require.cache[mod_path] !== 'undefined') { + delete require.cache[mod_path]; + } + + kiwi.kiwi_mod.loaded_modules[mod_name] = null; + kiwi.kiwi_mod.loaded_modules[mod_name] = require(mod_path); + + console.log('Module ' + mod_name + ' reloaded.'); + } catch (e) { + console.log('reloadModule error!'); + console.log(e); + return false; + } + }); + + //return this.loaded_modules[mod_name] ? true : false; +}; + + +/* + * Run an event against all loaded modules + */ exports.run = function (event_name, event_data, opts) { var ret = event_data, - mod_name; + ret_tmp, mod_name; event_data = (typeof event_data === 'undefined') ? {} : event_data; opts = (typeof opts === 'undefined') ? {} : opts; for (mod_name in this.loaded_modules) { if (typeof this.loaded_modules[mod_name]['on' + event_name] === 'function') { - ret = this.loaded_modules[mod_name]['on' + event_name](ret, opts); - if (ret === null) { - return null; + try { + ret_tmp = this.loaded_modules[mod_name]['on' + event_name](ret, opts); + if (ret_tmp === null) { + return null; + } + ret = ret_tmp; + } catch (e) { } } } @@ -51,7 +95,7 @@ exports.run = function (event_name, event_data, opts) { exports.printMods = function () { var mod_name; - console.log('Loaded Kiwi modules'); + console.log('Loaded Kiwi modules:'); for (mod_name in this.loaded_modules) { console.log(' - ' + mod_name); } -- 2.25.1