Skip to content

Commit

Permalink
Fixed - ArmaManager: All functions now work
Browse files Browse the repository at this point in the history
What
=================
Fixed: All the functions (start, stop and isAlive) to work properly.

Issue ID: #1
  • Loading branch information
Saborknight committed Mar 2, 2020
1 parent d5a8090 commit 7da78ca
Show file tree
Hide file tree
Showing 2 changed files with 142 additions and 29 deletions.
42 changes: 34 additions & 8 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
const {Client, RichEmbed} = require('discord.js');
<<<<<<< Updated upstream
const {exec} = require('child_process');
const https = require('https');
=======
const {exec, spawn} = require('child_process');
>>>>>>> Stashed changes
const http = require('http');
const config = require('./config.json');
const db = require('./data.json');
const {ArmaManager} = require('./modules/index.js');

let opsServer = new ArmaManager();
const armaServers = new ArmaManager();

opsServer.start();
console.log(opsServer.isActive());

// Are we in development mode?
let devPrefix = '';
Expand Down Expand Up @@ -209,6 +202,39 @@ function helpFormat(command, roles) {
function action(message, order, service) {
let actions = config.actions;
let action = order + '_' + service;
let result = '';

if (service) {
if (service.indexOf('arma') > -1) {
if (service == 'arma') {
service = 'armaOps';
}

if (!config.servers.hasOwnProperty(service)) {
log('error',`[${action}]: Service wasn't found in the servers config.`);
return 'Function is not configured in game server configs';
}

if (order == 'start') {
let instance = armaServers.start(service);
console.log(instance);
// if (armaServers.isAlive(instance.process.pid)) {
result = 'Arma server started with PID ' + instance.process.pid;
// }
}

if (order == 'stop') {
armaServers.stop(service);
result = `Stopping server: ${server}`;
}

if (order == 'status') {
armaServers.isAlive(sevice);
}

return result;
}
}

// If the action exists
if (Object.keys(actions).indexOf(action) > -1) {
Expand Down
129 changes: 108 additions & 21 deletions modules/arma-manager.js
Original file line number Diff line number Diff line change
@@ -1,46 +1,133 @@
/**
* Arma specific server driver which handles server and headless client
* instances.
*
* Features:
* - this.start - Start an instance
* - this.stop - Stop an instance
* - this.isAlive - Check an instance is still running
* - this.cleanPointers - Clean up the `this.instances` array of dead instances
*
* @author Arend
*/
const fs = require('fs');
const logPath = 'logs/arma.log';

const out = fs.openSync(logPath, 'a');
const err = fs.openSync(logPath, 'a');

const config = require('../config.json');
const ArmaServer = require('arma-server').Server;
const ArmaHeadless = require ('arma-server').Headless;

const ArmaManager = function(server = 'armaOps') {
this.server = server;
this.serverOptions = config.servers[server].options;
const ArmaManager = function() {
};

ArmaManager.prototype.instances = [];

ArmaManager.prototype.start = function() {
let server = new ArmaServer(this.serverOptions);
ArmaManager.prototype.start = function(serverName) {
if (typeof serverName !== 'string') {
console.log('No server given when instantiating ArmaManager! Server name: ', serverName);
return false;
}

let serverConfig = config.servers[serverName];
let serverOptions = {
...config.defaultServerSettings.arma.options,
...serverConfig.options,
};

server.writeServerConfig();
let instance = server.start();
// Start the actual server
let server = null;
if (serverConfig.type == 'headless') {
server = new ArmaHeadless(serverOptions);
} else {
serverConfig.type = 'server';
server = new ArmaServer(serverOptions);
server.writeServerConfig();
}
console.log('Type: ', serverConfig.type, 'Server instance: ', server);

instance = {
name: serverName,
type: serverConfig.type,
options: serverOptions,
process: server.start()
};

// Save the server details for later use
this.instances.push(instance);

instance.stdout.on('data', function (data) {
log('info', data);
});
// instance.stdout.on('data', function (data) {
// console.log(data);
// });

instance.stderr.on('data', function (data) {
log('info', data);
});
// instance.stderr.on('data', function (data) {
// console.log(data);
// });

instance.on('close', function (code) {
log('warn', 'Close was called: ' + code);
let parent = this;
instance.process.on('close', function (code) {
console.log(`The ${instance.name} ${instance.type} with PID ${this.pid} was closed: ${code}`);
parent.cleanPointers();
});

instance.on('error', function (err) {
log('error', err);
instance.process.on('error', function (err) {
console.log(`${instance.name} ${instance.type} with PID ${this.pid} errored: ${err}`);
parent.cleanPointers();
});

return instance;
};

ArmaManager.prototype.stop = function(instance) {
message.channel.send(`Stopping server: ${server}`);
return instances[server].kill('SIGINT');
ArmaManager.prototype.stop = function(serverName) {
console.log('Killing processes');
if (serverName == 'all') {
this.instances.forEach(instance => {
console.log(process.kill(-instance.process.pid));
});
} else {
this.instances.forEach(instance => {
if (instance.name === serverName) {
console.log(process.kill(-instance.process.pid));
}
});
}

this.cleanPointers();
};

ArmaManager.prototype.isActive = function() {
ArmaManager.prototype.isAlive = function(pid) {
if (typeof pid === 'object') {
console.log('Tried to use object to check if server is alive: ', pid);
pid = (this.instances.find(instance => instance.process == pid)).process.pid;
}

try {
process.kill(pid, 0);
} catch (e) {
console.log('PID found to be dead: ', pid);
return false;
}
console.log('PID found to be alive: ', pid);
return true;
};

ArmaManager.prototype.cleanPointers = function() {
console.log('Instances to clean up: ', this.instances);
let pointersRemoved = 0;

this.instances.forEach(
(instance, index, instances) => {
if (!this.isAlive(instance.process.pid)) {
instances.splice(index, 1);
pointersRemoved += 1;
}
}
);
console.log('Instances cleaned up: ', this.instances);

return pointersRemoved;
}

module.exports = ArmaManager;

0 comments on commit 7da78ca

Please sign in to comment.