Support for graceful shutdown, filter out some spammy lines
This commit is contained in:
parent
d72405f642
commit
b971077432
48
wrapper.js
48
wrapper.js
|
|
@ -3,7 +3,7 @@
|
||||||
var startupCmd = "";
|
var startupCmd = "";
|
||||||
const fs = require("fs");
|
const fs = require("fs");
|
||||||
fs.writeFile("latest.log", "", (err) => {
|
fs.writeFile("latest.log", "", (err) => {
|
||||||
if (err) console.log("Callback error in appendFile:"+err);
|
if (err) console.log("Callback error in appendFile:" + err);
|
||||||
});
|
});
|
||||||
|
|
||||||
var args = process.argv.splice(process.execArgv.length + 2);
|
var args = process.argv.splice(process.execArgv.length + 2);
|
||||||
|
|
@ -20,18 +20,53 @@ if (startupCmd.length < 1) {
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const seenPercentage = {};
|
||||||
|
function filter(data) {
|
||||||
|
const str = data.toString();
|
||||||
|
if (str.startsWith("Loading Prefab Bundle ")) { // Rust seems to spam the same percentage, so filter out any duplicates.
|
||||||
|
const percentage = str.substr("Loading Prefab Bundle ".length);
|
||||||
|
if (seenPercentage[percentage]) return;
|
||||||
|
|
||||||
|
seenPercentage[percentage] = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(str);
|
||||||
|
}
|
||||||
|
|
||||||
var exec = require("child_process").exec;
|
var exec = require("child_process").exec;
|
||||||
console.log("Starting Rust...");
|
console.log("Starting Rust...");
|
||||||
|
|
||||||
|
var exited = false;
|
||||||
const gameProcess = exec(startupCmd);
|
const gameProcess = exec(startupCmd);
|
||||||
gameProcess.stdout.on('data', console.log);
|
gameProcess.stdout.on('data', filter);
|
||||||
gameProcess.stderr.on('data', console.log);
|
gameProcess.stderr.on('data', console.log);
|
||||||
gameProcess.on('exit', function (code, signal) {
|
gameProcess.on('exit', function (code, signal) {
|
||||||
|
exited = true;
|
||||||
|
|
||||||
|
console.log("Game process exited with code " + code + ".");
|
||||||
process.exit(code);
|
process.exit(code);
|
||||||
});
|
});
|
||||||
|
|
||||||
var waiting = true;
|
function initialListener(data) {
|
||||||
|
const command = data.toString().trim();
|
||||||
|
if (command === 'quit') {
|
||||||
|
gameProcess.kill('SIGTERM');
|
||||||
|
} else {
|
||||||
|
console.log('Unable to run "' + command + '" due to RCON not being connected yet.');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
process.stdin.resume();
|
||||||
|
process.stdin.setEncoding("utf8");
|
||||||
|
process.stdin.on('data', initialListener);
|
||||||
|
|
||||||
|
process.on('exit', function(code) {
|
||||||
|
if (exited) return;
|
||||||
|
|
||||||
|
console.log("Received request to stop the process, stopping the game...");
|
||||||
|
gameProcess.kill('SIGTERM');
|
||||||
|
});
|
||||||
|
|
||||||
|
var waiting = true;
|
||||||
var poll = function( ) {
|
var poll = function( ) {
|
||||||
function createPacket(command) {
|
function createPacket(command) {
|
||||||
var packet = {
|
var packet = {
|
||||||
|
|
@ -49,14 +84,13 @@ var poll = function( ) {
|
||||||
var ws = new WebSocket("ws://" + serverHostname + ":" + serverPort + "/" + serverPassword);
|
var ws = new WebSocket("ws://" + serverHostname + ":" + serverPort + "/" + serverPassword);
|
||||||
|
|
||||||
ws.on("open", function open() {
|
ws.on("open", function open() {
|
||||||
|
console.log("Connected to RCON.");
|
||||||
waiting = false;
|
waiting = false;
|
||||||
process.stdin.resume();
|
|
||||||
process.stdin.setEncoding("utf8");
|
|
||||||
var util = require("util");
|
|
||||||
|
|
||||||
// Hack to fix broken console output
|
// Hack to fix broken console output
|
||||||
ws.send(createPacket('status'));
|
ws.send(createPacket('status'));
|
||||||
|
|
||||||
|
process.stdin.removeListener('data', initialListener);
|
||||||
process.stdin.on('data', function (text) {
|
process.stdin.on('data', function (text) {
|
||||||
ws.send(createPacket(text));
|
ws.send(createPacket(text));
|
||||||
});
|
});
|
||||||
|
|
@ -92,6 +126,8 @@ var poll = function( ) {
|
||||||
ws.on("close", function() {
|
ws.on("close", function() {
|
||||||
if (!waiting) {
|
if (!waiting) {
|
||||||
console.log("Connection to server closed.");
|
console.log("Connection to server closed.");
|
||||||
|
|
||||||
|
exited = true;
|
||||||
process.exit();
|
process.exit();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user