From 4f1cfce39cd1bfffdb65f97e5567ec871dd9fa9f Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 28 Jan 2017 17:24:30 +0100 Subject: [PATCH 1/2] Fix crash on slow startup, resolves #40 --- client.js | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/client.js b/client.js index c8060027..3f31bd2e 100644 --- a/client.js +++ b/client.js @@ -186,14 +186,25 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ timeout: 0 }); - // wait a few seconds to let the server finish starting before connecting rcon - //TODO: catch '2.033 Info RemoteCommandProcessor.cpp:97: Starting RCON interface at port 35002' from stdout maybe? - setTimeout(() => { - client.connect(); - }, 15000); - + // check the logfile to see if the RCON interface is running as there is no way to continue without it + // we read the log every 2 seconds and stop looping when we start connecting to factorio + function checkRcon() { + fs.readFile(instancedirectory+"/factorio-current.log", function (err, data) { + if (err) throw err; + if(data.indexOf('Starting RCON interface') > 0){ + client.connect(); + } else { + setTimeout(function(){ + checkRcon(); + },2000); + } + }); + } + checkRcon(); + client.on('authenticated', function () { console.log('Authenticated!'); + instanceManagement(); // start using rcons }).on('connected', function () { console.log('Connected!'); // getID(); @@ -206,7 +217,9 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ // set some globals confirmedOrders = []; lastSignalCheck = Date.now(); +} +function instanceManagement() { // world IDs ------------------------------------------------------------------ hashMods(instance, function(modHashes){ setInterval(getID, 10000); @@ -249,7 +262,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ hash: modHashes[i].hash, } needle.post(config.masterIP + ":" + config.masterPort + '/checkMod', payload, function (err, response, body) { - if(err) throw err + if(err) throw err // Unable to contact master server! Please check your config.json. if(response && body && body == "found") { console.log("master has mod") } else if (response && body && typeof body == "string") { From f435a792de92864b561cebfc9e2f84a32148dd46 Mon Sep 17 00:00:00 2001 From: Danielv123 Date: Sat, 28 Jan 2017 17:43:02 +0100 Subject: [PATCH 2/2] Avoid crashing on every 2nd launch due to #40 fix --- client.js | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/client.js b/client.js index 3f31bd2e..25eaccb8 100644 --- a/client.js +++ b/client.js @@ -137,7 +137,7 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ } // move mods from ./sharedMods to the instances mod directory - console.log("Moving shared mods...") + console.log("Clusterio | Moving shared mods...") //ncp("./sharedMods/", instancedirectory + "/mods", function (err) { @@ -186,12 +186,24 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ timeout: 0 }); + // clean old log file to avoid crash + // file exists, delete so we don't get in trouble + try { + fs.unlinkSync(instancedirectory+'/factorio-current.log') + } catch (err){ + if(err){ + console.log(err); + } else { + console.log("Clusterio | Deleting old logs...") + } + } + // check the logfile to see if the RCON interface is running as there is no way to continue without it // we read the log every 2 seconds and stop looping when we start connecting to factorio function checkRcon() { fs.readFile(instancedirectory+"/factorio-current.log", function (err, data) { - if (err) throw err; - if(data.indexOf('Starting RCON interface') > 0){ + // if (err) console.log(err); + if(data && data.indexOf('Starting RCON interface') > 0){ client.connect(); } else { setTimeout(function(){ @@ -203,13 +215,13 @@ write-data=__PATH__executable__/../../../instances/" + instance + "\r\n\ checkRcon(); client.on('authenticated', function () { - console.log('Authenticated!'); + console.log('Clusterio | Authenticated!'); instanceManagement(); // start using rcons }).on('connected', function () { - console.log('Connected!'); + console.log('Clusterio | Connected!'); // getID(); }).on('disconnected', function () { - console.log('Disconnected!'); + console.log('Clusterio | Disconnected!'); // now reconnect client.connect(); });