From 4fcc2b809bd85c5765c077531f44110bd5ee8a50 Mon Sep 17 00:00:00 2001 From: Saborknight Date: Mon, 7 Sep 2020 16:31:18 +1000 Subject: [PATCH] Improved/Fixed - Loggin and response messaging & `stop()` not killing What ================= Improved: the logging and response messages on Discord for the commands that interact with the driver. Fixed: `stop()` command not finding the processes by PID before. Also, the profile specific kill commands now work properly. Issue ID: #1 --- index.js | 18 ++++++++++++------ modules/arma-manager.js | 37 +++++++++++++++++++++++++++---------- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/index.js b/index.js index db10101..efcd0f9 100644 --- a/index.js +++ b/index.js @@ -278,6 +278,7 @@ bot.on('message', message => { let arguments = { command: args[0] }; let result = []; let service; + let formattedMessage; log('info', `Command \`${db.commandPrefix}${arguments.command}\` was called: "${message.content}"`); //////////// @@ -398,20 +399,25 @@ bot.on('message', message => { } arguments.game = arguments.game.toLowerCase(); - let instances = serverManagers[arguments.game].start(arguments.serverProfile); + let driver = serverManagers[arguments.game].start(arguments.serverProfile); - if (!instances) { + if (!driver.instances || driver.instances.length <= 0) { message.channel.send(`Failed to start any ${arguments.game} instances, please contact your lord a saviour for some diving intervention.`); result = ['error', `Failed to start any ${arguments.game} instances. Profile: ${arguments.serverProfile}.`]; break; } - message.channel.send(`Spun up ${instances.length} ${arguments.game} servers. Profile: ${arguments.serverProfile}`); + formattedMessage = new RichEmbed() + .setTitle('Technical jargon') + .setColor(config.serverEnvironments[arguments.game].colour) + .setDescription(driver.message); + + message.channel.send(`Spun up ${driver.instances.length} ${arguments.game} servers. Profile: ${arguments.serverProfile}`); + message.channel.send(formattedMessage); + result = [ 'info', - `Spun up ${arguments.game} instances from profile ${arguments.serverProfile}:\n - ${instances.forEach((instance, i) => '\t' + i + ': ' + instance.pid + (instance.headless ? ', headless client.' : ', server.'))} - `]; + `Spun up ${driver.instances.length} ${arguments.game} instances from profile ${arguments.serverProfile}:\n${driver.log}`]; break; // Command: `!stop` diff --git a/modules/arma-manager.js b/modules/arma-manager.js index 015e729..66044a8 100644 --- a/modules/arma-manager.js +++ b/modules/arma-manager.js @@ -63,7 +63,19 @@ ArmaManager.prototype.start = function(serverProfile) { instances.push(this.executeServer(serverProfile, commandline)); }); - return instances; + let message = ''; + let log = ''; + + instances.forEach((instance, i) => { + message += `${i}: **${instance.profile}** type: ${instance.headless ? `headless client` : `server`}, port: ${instance.options.find(option => option.indexOf('-port') > -1).replace('-port=','')}, pid: ${instance.process.pid}.\n`; + log += `\t${i}: { profile: ${instance.profile},\nheadless: ${instance.headless},\noptions: ${instance.options},\nprocess: ${instance.process.pid} },\n`; + }); + + return { + instances: instances, + message: message, + log: log + }; }; /** @@ -79,21 +91,26 @@ ArmaManager.prototype.start = function(serverProfile) { ArmaManager.prototype.stop = function(serverProfile = 'all') { this.updatePointers(); + let killing = 0; console.log(`Killing all instances of "${serverProfile}"`); if (serverProfile == 'all') { this.instances.forEach(instance => { - console.log(serverProfile, this.instances); - console.log(process.kill(-instance.process.pid)); + if (process.kill(instance.process.pid)) { + killing += 1; + } }); } else { this.instances.forEach(instance => { - if (instance.name === serverProfile) { - console.log(process.kill(-instance.process.pid)); + if (instance.profile === serverProfile) { + if (process.kill(instance.process.pid)) { + killing += 1; + } } }); } - return this.cleanPointers(); + this.cleanPointers(); + return killing; }; /** @@ -114,7 +131,7 @@ ArmaManager.prototype.isAlive = function(childProcess) { if (typeof childProcess === 'string') { console.log('Tried to use string to check if server is alive: ', childProcess); - pid = (this.instances.find(instance => instance.name == childProcess)).process.pid; + pid = (this.instances.find(instance => instance.profile == childProcess)).process.pid; } try { @@ -137,7 +154,7 @@ ArmaManager.prototype.updatePointers = function() { (async () => { list = await psList(); - processes = list.filter(instance => instance.name == 'arma3server_x64.exe'); + processes = list.filter(process => process.name == 'arma3server_x64.exe'); let newInstances = []; processes.forEach(process => { @@ -243,12 +260,12 @@ ArmaManager.prototype.executeServer = function(serverProfile, commandline) { let parent = this; instance.process.on('close', function (code) { - console.log(`The ${instance.name} ${instance.headless} with PID ${this.pid} was closed: ${code}`); + console.log(`The ${instance.profile} ${instance.headless ? 'headless client' : 'server'} with PID ${this.pid} was closed: ${code}`); parent.updatePointers(); }); instance.process.on('error', function (err) { - console.log(`${instance.name} ${instance.headless} with PID ${this.pid} errored: ${err}`); + console.log(`${instance.profile} ${instance.headless ? 'headless client' : 'server'} with PID ${this.pid} errored: ${err}`); parent.updatePointers(); });