From 2b101d594aba872756b4c4921eddcd3e787974a9 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Wed, 29 Sep 2021 00:21:53 +0200 Subject: [PATCH] Fix statistics_exporter sending commands before start Fix statistics_exporter potentially sending commands before an instance is started when metrics are collected. --- plugins/statistics_exporter/instance.js | 4 ++++ plugins/statistics_exporter/test/plugin.js | 7 +++++++ test/mock.js | 1 + 3 files changed, 12 insertions(+) diff --git a/plugins/statistics_exporter/instance.js b/plugins/statistics_exporter/instance.js index db40dcbd..7d98f5eb 100644 --- a/plugins/statistics_exporter/instance.js +++ b/plugins/statistics_exporter/instance.js @@ -77,6 +77,10 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin { } async onMetrics() { + if (this.instance.status !== "running") { + return; + } + // Wait configured timeout for the metrics to be collected. It may // take a long time for the command to go through if the command // stream is overloaded. Should the timeout be exceeded the diff --git a/plugins/statistics_exporter/test/plugin.js b/plugins/statistics_exporter/test/plugin.js index 50a28c88..56a16b5d 100644 --- a/plugins/statistics_exporter/test/plugin.js +++ b/plugins/statistics_exporter/test/plugin.js @@ -17,7 +17,14 @@ describe("statistics_exporter plugin", function() { }); describe(".onMetrics()", function() { + it("should not send commands before running", async function() { + instancePlugin.instance.server.rconCommands.length = 0; + instancePlugin.instance.status = "starting"; + await instancePlugin.onMetrics(); + assert(instancePlugin.instance.server.rconCommands.length === 0, "commands were run"); + }); it("should record statistics", async function() { + instancePlugin.instance.status = "running"; instancePlugin.instance.server.rconCommandResults.set( "/sc statistics_exporter.export()", JSON.stringify({ diff --git a/test/mock.js b/test/mock.js index 4eec7794..c66b3d0b 100644 --- a/test/mock.js +++ b/test/mock.js @@ -130,6 +130,7 @@ class MockInstance extends libLink.Link { this.server = new MockServer(); this.name = "test"; this.id = 7357; + this.status = "running"; this.mockConfigEntries = new Map([ ["instance.id", 7357], ["factorio.enable_save_patching", true],