From 2a1f93063235e2d5e370dbfeb4a1908f02799e4e Mon Sep 17 00:00:00 2001 From: Ivo Julca Date: Thu, 16 Jul 2015 04:43:32 -0500 Subject: [PATCH] Load tools' data for main process from plugin requiring it This allows servers which remove `info.js` plugin to cut down RAM consumption. --- app.js | 15 ++++++--------- chat-plugins/info.js | 13 ++++++++++++- commands.js | 6 ++++-- 3 files changed, 22 insertions(+), 12 deletions(-) diff --git a/app.js b/app.js index 6d3a732663..d177cdde8d 100644 --- a/app.js +++ b/app.js @@ -307,12 +307,18 @@ global.toId = function (text) { return ('' + text).toLowerCase().replace(/[^a-z0-9]+/g, ''); }; +global.Tools = require('./tools.js').includeFormats(); + global.LoginServer = require('./loginserver.js'); global.Users = require('./users.js'); global.Rooms = require('./rooms.js'); +// Generate and cache the format list. +Rooms.global.formatListText = Rooms.global.getFormatListText(); + + delete process.send; // in case we're a child process global.Verifier = require('./verifier.js'); @@ -359,15 +365,6 @@ global.Sockets = require('./sockets.js'); * Set up our last global *********************************************************/ -// This slow operation is done *after* we start listening for connections -// to the server. Anybody who connects while this require() is running will -// have to wait a couple seconds before they are able to join the server, but -// at least they probably won't receive a connection error message. -global.Tools = require('./tools.js').includeData(); - -// After loading tools, generate and cache the format list. -Rooms.global.formatListText = Rooms.global.getFormatListText(); - global.TeamValidator = require('./team-validator.js'); // load ipbans at our leisure diff --git a/chat-plugins/info.js b/chat-plugins/info.js index 8c019ebfb3..fd4de367b5 100644 --- a/chat-plugins/info.js +++ b/chat-plugins/info.js @@ -12,7 +12,7 @@ const RESULTS_MAX_LENGTH = 10; -var commands = exports.commands = { +var commands = { ip: 'whois', rooms: 'whois', @@ -2360,3 +2360,14 @@ var commands = exports.commands = { }, htmlboxhelp: ["/htmlbox [message] - Displays a message, parsing HTML code contained. Requires: ~ # with global authority"] }; + +process.nextTick(function () { + // This slow operation is done *after* we start listening for connections + // to the server. Anybody who connects while data is loading will + // have to wait a couple seconds before they are able to join the server, but + // at least they probably won't receive a connection error message. + + Tools.includeData(); + exports.commands = commands; + Object.merge(CommandParser.commands, commands); +}); diff --git a/commands.js b/commands.js index 7a58a8a06e..d0bdc89dbd 100644 --- a/commands.js +++ b/commands.js @@ -1456,10 +1456,11 @@ var commands = exports.commands = { return this.sendReply("Battles have been hotpatched. Any battles started after now will use the new code; however, in-progress battles will continue to use the old code."); } else if (target === 'formats') { try { + var toolsLoaded = !!Tools.isLoaded; // uncache the tools.js dependency tree CommandParser.uncacheTree('./tools.js'); // reload tools.js - global.Tools = require('./tools.js').includeData(); // note: this will lock up the server for a few seconds + global.Tools = require('./tools.js')[toolsLoaded ? 'includeData' : 'includeFormats'](); // note: this will lock up the server for a few seconds // rebuild the formats list Rooms.global.formatListText = Rooms.global.getFormatListText(); // respawn validator processes @@ -1475,10 +1476,11 @@ var commands = exports.commands = { } } else if (target === 'learnsets') { try { + var toolsLoaded = !!Tools.isLoaded; // uncache the tools.js dependency tree CommandParser.uncacheTree('./tools.js'); // reload tools.js - global.Tools = require('./tools.js').includeData(); // note: this will lock up the server for a few seconds + global.Tools = require('./tools.js')[toolsLoaded ? 'includeData' : 'includeFormats'](); // note: this will lock up the server for a few seconds return this.sendReply("Learnsets have been hotpatched."); } catch (e) {