From 139585f8c6b9ba8260d575a773e241067655c15e Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 6 Apr 2020 17:04:11 +0200 Subject: [PATCH] Prevent metrics being called on createSave Gate the onMetrics plugin hook behind a boolean that's set only after the Factorio server has fully started, and reset when it stops. This prevents errors due to plugins trying to get metrics while the server is creating a new save or stopped. --- slave.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/slave.js b/slave.js index 7aee2935..1d34b0e3 100644 --- a/slave.js +++ b/slave.js @@ -54,6 +54,7 @@ class Instance extends link.Link{ rconPassword: this.config.get('factorio.rcon_password'), }; + this._running = false; this.server = new factorio.FactorioServer( factorioDir, this._dir, serverOptions ); @@ -80,6 +81,7 @@ class Instance extends link.Link{ } notifyExit() { + this._running = false; link.messages.instanceStopped.send(this); // Clear metrics this instance is exporting @@ -288,6 +290,8 @@ class Instance extends link.Link{ for (let pluginInstance of this.plugins.values()) { await pluginInstance.onStart(); } + + this._running = true; } /** @@ -303,6 +307,8 @@ class Instance extends link.Link{ * Stop the instance */ async stop() { + this._running = false; + // XXX this needs more thought to it if (this.server._state === "running") { for (let pluginInstance of this.plugins.values()) { @@ -315,11 +321,13 @@ class Instance extends link.Link{ async getMetricsRequestHandler() { let results = [] - for (let pluginInstance of this.plugins.values()) { - let pluginResults = await pluginInstance.onMetrics(); - if (pluginResults !== undefined) { - for await (let result of pluginResults) { - results.push(prometheus.serializeResult(result)) + if (this._running) { + for (let pluginInstance of this.plugins.values()) { + let pluginResults = await pluginInstance.onMetrics(); + if (pluginResults !== undefined) { + for await (let result of pluginResults) { + results.push(prometheus.serializeResult(result)) + } } } }