Skip to content

Commit

Permalink
Improved/Fixed - Loggin and response messaging & stop() not killing
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Saborknight committed Sep 7, 2020
1 parent dccf590 commit 4fcc2b8
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
18 changes: 12 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}"`);

////////////
Expand Down Expand Up @@ -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`
Expand Down
37 changes: 27 additions & 10 deletions modules/arma-manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
};

/**
Expand All @@ -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;
};

/**
Expand All @@ -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 {
Expand All @@ -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 => {
Expand Down Expand Up @@ -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();
});

Expand Down

0 comments on commit 4fcc2b8

Please sign in to comment.