Handle start and stopping servers

This commit is contained in:
Björn Dahlgren 2014-04-07 00:58:17 +02:00
parent f98e6e9d72
commit c125645823
5 changed files with 96 additions and 10 deletions

View File

@ -19,6 +19,7 @@ app.resource('api/servers', servers);
app.resource('api/settings', require('./routes/settings'));
app.get('/api/servers/:server/start', servers.start);
app.get('/api/servers/:server/stop', servers.stop);
app.get('/', function (req, res){
res.sendfile(__dirname + '/public/index.html');

View File

@ -27,19 +27,49 @@ Server.prototype.start = function() {
var mods = this.makeModsParameter();
var port = this.makePortParameter();
var server = spawn(this.armaServerPath(), [mods, port, '-config=server.cfg', '-noSound', '-world=empty']);
var process = spawn(this.armaServerPath(), [mods, port, '-config=server.cfg', '-noSound', '-world=empty']);
var self = this;
server.stdout.on('data', function (data) {
process.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
server.stderr.on('data', function (data) {
process.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
server.on('close', function (code) {
process.on('close', function (code) {
console.log('child process exited with code ' + code);
self.pid = null;
self.process = null;
});
this.pid = process.pid;
this.process = process;
return this;
}
Server.prototype.stop = function(cb) {
var handled = false;
this.process.on('close', function (code) {
if (!handled) {
handled = true;
cb();
}
});
this.process.kill();
setTimeout(function() {
if (!handled) {
handled = true;
cb();
}
}, 5000);
return this;
}
function Manager() {
@ -87,7 +117,18 @@ Manager.prototype.load = (function () {
});
Manager.prototype.save = (function () {
fs.writeFile(filePath, JSON.stringify(this.serversArr), function(err) {
var data = [];
this.serversArr.forEach(function (server) {
data.push({
id: server.id,
title: server.title,
port: server.port,
mods: server.mods,
})
});
fs.writeFile(filePath, JSON.stringify(data), function(err) {
if(err) throw err;
});
});

View File

@ -12,7 +12,8 @@ define(function (require) {
template: _.template(tpl),
events: {
"click #start": "start"
"click #start": "start",
"click #stop": "stop",
},
start: function (event) {
@ -22,7 +23,22 @@ define(function (require) {
url: "/api/servers/" + this.model.get('id') + "/start",
type: 'GET',
success: function (resp) {
console.log(resp);
self.model.set("pid", resp.pid);
self.render();
},
error: $.noop
});
},
stop: function (event) {
var self = this;
event.preventDefault();
$.ajax({
url: "/api/servers/" + this.model.get('id') + "/stop",
type: 'GET',
success: function (resp) {
self.model.set("pid", resp.pid);
self.render();
},
error: $.noop
});

View File

@ -1 +1,7 @@
<% if (typeof(pid) != "undefined" && pid) { %>
Server is running as <%= pid %>
<a class="btn btn-primary" href="#" id="stop">Stop</a>
<% } else { %>
<a class="btn btn-primary" href="#" id="start">Start</a>
<% } %>

View File

@ -5,7 +5,18 @@ var Manager = require('./../manager');
var manager = new Manager();
exports.index = function (req, res){
res.send(manager.getServers());
var servers = [];
manager.getServers().forEach(function (server) {
servers.push({
id: server.id,
title: server.title,
port: server.port,
mods: server.mods,
})
});
res.send(servers);
};
exports.create = function (req, res){
@ -29,5 +40,16 @@ exports.destroy = function(req, res){
exports.start = function (req, res){
var server = manager.getServer(req.params.server);
server.start();
res.send({status:"ok"});
res.send({status:"ok", pid: server.pid});
};
exports.stop = function (req, res){
var server = manager.getServer(req.params.server);
server.stop(function () {
if (!server.pid) {
res.send({status: true, pid: server.pid});
} else {
res.send({status: false, pid: server.pid});
}
});
};