From 63a656cd61d98f0ebff0a03e40086ffe864d6b8e Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sun, 8 Nov 2020 21:24:31 +0100 Subject: [PATCH 01/15] Rename default Admin role to Cluster Admin Clarify the purpose and distinquesh the Admin role a bit better from the in-game admin status by renaming the role to Cluster Admin. --- docs/how-it-works.md | 2 +- docs/managing-a-cluster.md | 2 +- packages/lib/users.js | 2 +- test/integration/clusterio.js | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/how-it-works.md b/docs/how-it-works.md index ad4084b0..e5bc2e2a 100644 --- a/docs/how-it-works.md +++ b/docs/how-it-works.md @@ -27,4 +27,4 @@ that log into instances under its control. These accounts are used to store per player data and authorize management of the cluster. By default users accounts are assigned a Player role that gives a limited read access to the cluster resources, and admins of the cluster can be -assigned an Admin role to get access to manage it. +assigned a Cluster Admin role to get access to manage it. diff --git a/docs/managing-a-cluster.md b/docs/managing-a-cluster.md index ef58dbf0..4940356f 100644 --- a/docs/managing-a-cluster.md +++ b/docs/managing-a-cluster.md @@ -107,7 +107,7 @@ Replaces the roles the user has in the cluster with a new list of roles. Calling with an empty roles argument will remove all roles from the user. -By default there's a roled named Admin which grants access to +By default there's a roled named Cluster Admin which grants access to everything and a role named Player which grants a limited read access to the cluster, see [the section on roles](#roles) for more information about setting up roles and permissions. diff --git a/packages/lib/users.js b/packages/lib/users.js index 865572d1..803fe8be 100644 --- a/packages/lib/users.js +++ b/packages/lib/users.js @@ -102,7 +102,7 @@ class Role { function ensureDefaultAdminRole(roles) { let admin = roles.get(0); if (!admin) { - admin = new Role({ id: 0, name: "Admin", description: "Cluster wide administrator." }); + admin = new Role({ id: 0, name: "Cluster Admin", description: "Cluster wide administrator." }); roles.set(0, admin); } admin.permissions.add("core.admin"); diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index e9d80333..50ec1a33 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -312,7 +312,7 @@ describe("Integration of Clusterio", function() { describe("user set-roles", function() { it("should set the roles on the user", async function() { - await execCtl("user set-roles temp Admin"); + await execCtl('user set-roles temp "Cluster Admin"'); let result = await libLink.messages.listUsers.send(getControl()); let tempUser = result.list.find(user => user.name === "temp"); assert.deepEqual(tempUser.roles, [0]); From 67654fdf8540ebfc626c1185c128fce00479429c Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 9 Nov 2020 12:38:43 +0100 Subject: [PATCH 02/15] Add stripping of instance paths in Factorio log Add option defaulting to true to strip instance directory paths printed into the Factorio log down to be relative to the instance. --- CHANGELOG.md | 1 + docs/configuration.md | 20 ++++++++++++++++ packages/lib/config/definitions.js | 6 +++++ packages/lib/factorio/server.js | 38 +++++++++++++++++++++++++++++- packages/slave/slave.js | 1 + 5 files changed, 65 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 18a79727..049f024a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -46,6 +46,7 @@ Version 2.0.0 player data. - Added synchronization of in-game adminlist, banlist and whitelist to Clusterio core. Previously this was handled by the playerManager plugin. +- Added stripping of long paths in the Factorio server log. ### Changes diff --git a/docs/configuration.md b/docs/configuration.md index 71a76b60..0ab53847 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -280,6 +280,26 @@ here overrides the properties there. Defaults to {"tags":["clusterio"],"auto_pause":false}. +### factorio.strip_paths + +Strip down absolute paths in the server log going to files inside the +instance directory such that they are relative to the instance +directory. This improves the signal to noise ratio of the log. + +Sample output when disabled: + + 0.487 Loading map /srv/clusterio/instances/test/saves/world.zip: 2615888 bytes. + 0.614 Checksum for script /srv/clusterio/instances/test/temp/currently-playing/control.lua: 2390553941 + 1.277 Script @/srv/clusterio/instances/test/temp/currently-playing/modules/example/test.lua:7: Example log line. + +Sample output when enabled: + + 0.487 Loading map saves/world.zip: 2615888 bytes. + 0.614 Checksum for script temp/currently-playing/control.lua: 2390553941 + 1.277 Script @modules/example/test.lua:7: Example log line. + +Defaults to true + ### factorio.sync_adminlist Synchronize in-game admin list with admin status of the users in diff --git a/packages/lib/config/definitions.js b/packages/lib/config/definitions.js index c95c0fb3..87709a41 100644 --- a/packages/lib/config/definitions.js +++ b/packages/lib/config/definitions.js @@ -250,6 +250,12 @@ FactorioGroup.define({ "auto_pause": false, }, }); +FactorioGroup.define({ + name: "strip_paths", + description: "Strip down instance paths in the log", + type: "boolean", + initial_value: true, +}); FactorioGroup.define({ name: "sync_adminlist", description: "Synchronize adminlist with master server", diff --git a/packages/lib/factorio/server.js b/packages/lib/factorio/server.js index 2294b4f4..78b6b226 100644 --- a/packages/lib/factorio/server.js +++ b/packages/lib/factorio/server.js @@ -12,6 +12,22 @@ const rconClient = require("rcon-client"); const libErrors = require("@clusterio/lib/errors"); +/** + * Escapes text for inclusion in a RegExp + * + * Adds \ character in front of special meta characters in the passsed in + * text so that it can be embedded into a RegExp and only match the text. + * + * See https://stackoverflow.com/a/9310752 + * + * @param {string} text - Text to escape RegExp meta chars in. + * @returns {string} escaped text. + * @inner + */ +function escapeRegExp(text) { + return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&"); +} + /** * Determines the version of Factorio the datadir is pointing to by * reading the changelog.txt in it. @@ -497,6 +513,7 @@ class FactorioServer extends events.EventEmitter { * @param {number} options.gamePort - UDP port to host game on. * @param {number} options.rconPort - TCP port to use for RCON. * @param {string} options.rconPassword - Password use for RCON. + * @param {boolean} options.stripPaths - Strip paths in the console. */ constructor(factorioDir, writeDir, options) { super(); @@ -523,6 +540,19 @@ class FactorioServer extends events.EventEmitter { this._rconReady = false; this._gameReady = false; + if (options.stripPaths) { + let chars = new Set(this.writePath("temp")); + chars.delete(":"); // Having a colon could lead to matching the line number + chars = [...chars].join(""); + + this._stripRegExp = new RegExp( + `(${escapeRegExp(this.writePath("temp", "currently-playing", path.sep))})|` + + `(${escapeRegExp(this.writePath(path.sep))})|` + + `(\\.\\.\\.[${chars}]*?currently-playing\\${path.sep})`, + "g" + ); + } + // Array of possible causes for an unexpected shutdown this._unexpected = []; @@ -606,7 +636,13 @@ class FactorioServer extends events.EventEmitter { this.emit(source, line); - let output = parseOutput(line.toString("utf-8"), source); + line = line.toString("utf-8"); + if (this._stripRegExp) { + line = line.replace(this._stripRegExp, ""); + } + + + let output = parseOutput(line, source); heuristicLoop: for (let heuristic of outputHeuristics) { for (let [name, expected] of Object.entries(heuristic.filter)) { if (!Object.hasOwnProperty.call(output, name)) { diff --git a/packages/slave/slave.js b/packages/slave/slave.js index 37c13d7b..e97666ec 100755 --- a/packages/slave/slave.js +++ b/packages/slave/slave.js @@ -135,6 +135,7 @@ class Instance extends libLink.Link{ rconPort: this.config.get("factorio.rcon_port"), rconPassword: this.config.get("factorio.rcon_password"), enableWhitelist: this.config.get("factorio.enable_whitelist"), + stripPaths: this.config.get("factorio.strip_paths"), }; this._running = false; From c8c65d2e927b406f1b9fa62bbaaf0c7eab032a60 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 9 Nov 2020 15:14:30 +0100 Subject: [PATCH 03/15] Add verbose logging option Add option to pass the --verbose flag to the Factorio server. --- docs/configuration.md | 8 ++++++++ packages/lib/config/definitions.js | 6 ++++++ packages/lib/factorio/server.js | 7 +++++++ packages/slave/slave.js | 1 + 4 files changed, 22 insertions(+) diff --git a/docs/configuration.md b/docs/configuration.md index 0ab53847..5e7f1ac1 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -280,6 +280,14 @@ here overrides the properties there. Defaults to {"tags":["clusterio"],"auto_pause":false}. +### factorio.verbose_logging + +Pass the `--verbose` flag to Factorio when starting the server. This +prints more messages to the log. + +Defaults to false + + ### factorio.strip_paths Strip down absolute paths in the server log going to files inside the diff --git a/packages/lib/config/definitions.js b/packages/lib/config/definitions.js index 87709a41..b1ed056b 100644 --- a/packages/lib/config/definitions.js +++ b/packages/lib/config/definitions.js @@ -250,6 +250,12 @@ FactorioGroup.define({ "auto_pause": false, }, }); +FactorioGroup.define({ + name: "verbose_logging", + description: "Enable verbose logging on the Factorio server", + type: "boolean", + initial_value: false, +}); FactorioGroup.define({ name: "strip_paths", description: "Strip down instance paths in the log", diff --git a/packages/lib/factorio/server.js b/packages/lib/factorio/server.js index 78b6b226..842bb1f7 100644 --- a/packages/lib/factorio/server.js +++ b/packages/lib/factorio/server.js @@ -513,6 +513,8 @@ class FactorioServer extends events.EventEmitter { * @param {number} options.gamePort - UDP port to host game on. * @param {number} options.rconPort - TCP port to use for RCON. * @param {string} options.rconPassword - Password use for RCON. + * @param {boolean} options.enableWhitelist - Turn on whitelisting. + * @param {boolean} options.verboseLogging - Enable verbose logging. * @param {boolean} options.stripPaths - Strip paths in the console. */ constructor(factorioDir, writeDir, options) { @@ -534,6 +536,8 @@ class FactorioServer extends events.EventEmitter { this.rconPassword = options.rconPassword; /** Enable player whitelist */ this.enableWhitelist = options.enableWhitelist; + /** Enable verbose logging */ + this.verboseLogging = options.verboseLogging; this._state = "new"; this._server = null; this._rconClient = null; @@ -810,6 +814,7 @@ class FactorioServer extends events.EventEmitter { [ "--config", this.writePath("config.ini"), "--create", this.writePath("saves", name), + ...(this.verboseLogging ? ["--verbose"] : []), ], { detached: true, @@ -852,6 +857,7 @@ class FactorioServer extends events.EventEmitter { "--rcon-port", this.rconPort, "--rcon-password", this.rconPassword, ...(this.enableWhitelist ? ["--use-server-whitelist"] : []), + ...(this.verboseLogging ? ["--verbose"] : []), ], { detached: true, @@ -886,6 +892,7 @@ class FactorioServer extends events.EventEmitter { "--rcon-port", this.rconPort, "--rcon-password", this.rconPassword, ...(this.enableWhitelist ? ["--use-server-whitelist"] : []), + ...(this.verboseLogging ? ["--verbose"] : []), ], { detached: true, diff --git a/packages/slave/slave.js b/packages/slave/slave.js index e97666ec..19a9ea11 100755 --- a/packages/slave/slave.js +++ b/packages/slave/slave.js @@ -135,6 +135,7 @@ class Instance extends libLink.Link{ rconPort: this.config.get("factorio.rcon_port"), rconPassword: this.config.get("factorio.rcon_password"), enableWhitelist: this.config.get("factorio.enable_whitelist"), + verboseLogging: this.config.get("factorio.verbose_logging"), stripPaths: this.config.get("factorio.strip_paths"), }; From db1f239a0f0f4e88c917eaf3b23880e99051582f Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 9 Nov 2020 15:52:50 +0100 Subject: [PATCH 04/15] Add formatting of Script and Verbose levels Detect and colour the Script and Verbose levels. The format of the source file for the Script level is slightly different and needs an update to the output parsing. --- packages/ctl/ctl.js | 6 ++- packages/lib/factorio/server.js | 6 ++- .../src/components/InstanceViewPage.jsx | 6 ++- packages/web_ui/src/index.css | 10 +++++ test/lib/factorio/lines.js | 45 +++++++++++++++++++ 5 files changed, 69 insertions(+), 4 deletions(-) diff --git a/packages/ctl/ctl.js b/packages/ctl/ctl.js index 3966abb8..9e9e27d5 100755 --- a/packages/ctl/ctl.js +++ b/packages/ctl/ctl.js @@ -43,7 +43,11 @@ function formatOutputColored(output) { let info = ""; if (output.type === "log") { let level = output.level; - if (level === "Info") { + if (level === "Script") { + level = chalk.bold.greenBright(level); + } else if (level === "Verbose") { + level = chalk.bold.gray(level); + } else if (level === "Info") { level = chalk.bold.blueBright(level); } else if (output.level === "Warning") { level = chalk.bold.yellowBright(level); diff --git a/packages/lib/factorio/server.js b/packages/lib/factorio/server.js index 842bb1f7..658e5a15 100644 --- a/packages/lib/factorio/server.js +++ b/packages/lib/factorio/server.js @@ -318,13 +318,15 @@ function parseOutput(line, source) { // A seconds output has two different kinds of formats: The first is a // log level and source location and has a format of // "Level File.cpp:123: message" - const secLogRegex = /^(\w+) (\w+\.cpp:\d+): (.*)$/; + // "Script @/path/to/file.lua:123: message" + // "Script =(command):123: message" + const secLogRegex = /^(\w+) ((\w+\.cpp:\d+)|([@=].*?:\d+)): (.*)$/; let secLogMatch = secLogRegex.exec(secContent); if (secLogMatch) { output.type = "log"; output.level = secLogMatch[1]; output.file = secLogMatch[2]; - output.message = secLogMatch[3]; + output.message = secLogMatch[5]; // The other possibility is that the content is a generic message } else { diff --git a/packages/web_ui/src/components/InstanceViewPage.jsx b/packages/web_ui/src/components/InstanceViewPage.jsx index 91c51d33..5e0057d6 100644 --- a/packages/web_ui/src/components/InstanceViewPage.jsx +++ b/packages/web_ui/src/components/InstanceViewPage.jsx @@ -83,7 +83,11 @@ function formatOutputColored(output, key) { let info = ""; if (output.type === "log") { let level = output.level; - if (level === "Info") { + if (level === "Script") { + level = {level}; + } else if (level === "Verbose") { + level = {level}; + } else if (level === "Info") { level = {level}; } else if (output.level === "Warning") { level = {level}; diff --git a/packages/web_ui/src/index.css b/packages/web_ui/src/index.css index 246613cb..f1ad3417 100644 --- a/packages/web_ui/src/index.css +++ b/packages/web_ui/src/index.css @@ -118,6 +118,16 @@ body { color: #808080; } +.instance-console .factorio-script { + font-weight: bold; + color: #49c33c; +} + +.instance-console .factorio-verbose { + font-weight: bold; + color: #888888; +} + .instance-console .factorio-info { font-weight: bold; color: #4646ea; diff --git a/test/lib/factorio/lines.js b/test/lib/factorio/lines.js index d5ced40e..aeba4b9d 100644 --- a/test/lib/factorio/lines.js +++ b/test/lib/factorio/lines.js @@ -143,6 +143,51 @@ let testLines = new Map([ message: "Saving finished", }, ], + [ + " 1.277 Script @/srv/clusterio/instances/test/temp/currently-playing/modules/example/test.lua:7: " + + "Example log line.", + { + format: "seconds", + time: "1.277", + type: "log", + level: "Script", + file: "@/srv/clusterio/instances/test/temp/currently-playing/modules/example/test.lua:7", + message: "Example log line.", + }, + ], + [ + " 1.277 Script @C:\\My Cluster\\temp\\current-save\\file.lua:7: Contrived :12: example", + { + format: "seconds", + time: "1.277", + type: "log", + level: "Script", + file: "@C:\\My Cluster\\temp\\current-save\\file.lua:7", + message: "Contrived :12: example", + }, + ], + [ + " 344.683 Script =(command):1: test", + { + format: "seconds", + time: "344.683", + type: "log", + level: "Script", + file: "=(command):1", + message: "test", + }, + ], + [ + " 0.751 Verbose ModManager.cpp:554: Time to load mods: 0.746762", + { + format: "seconds", + time: "0.751", + type: "log", + level: "Verbose", + file: "ModManager.cpp:554", + message: "Time to load mods: 0.746762", + }, + ], ]); module.exports = { From fc8edeba5f0410e154abdd22190c789bf8becbd0 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 9 Nov 2020 17:26:56 +0100 Subject: [PATCH 05/15] Reduce log spam on connection error Print only the error message in the console when a connection error is encountered on the link connector. This reduces log spam on reconnect for slaves. --- packages/lib/link/connectors.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lib/link/connectors.js b/packages/lib/link/connectors.js index a8d494c9..20766092 100644 --- a/packages/lib/link/connectors.js +++ b/packages/lib/link/connectors.js @@ -315,7 +315,7 @@ class WebSocketClientConnector extends WebSocketBaseConnector { this._socket.onerror = event => { // It's assumed that close is always called by ws - console.error("SOCKET | Error:", event); + console.error("SOCKET | Error:", event.message || "unknown error"); }; this._socket.onopen = () => { From a92ae95ab1e83a1f7f940978c6ea4c708b899a10 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 9 Nov 2020 18:36:03 +0100 Subject: [PATCH 06/15] Fix updating config breaking instance status Fix broken assign instance code in slave sending of the wrong instance status to the master server. --- packages/slave/slave.js | 31 +++++++++++++++++++++++++------ test/integration/clusterio.js | 4 ++++ 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/packages/slave/slave.js b/packages/slave/slave.js index 19a9ea11..0eb92e0d 100755 --- a/packages/slave/slave.js +++ b/packages/slave/slave.js @@ -139,6 +139,7 @@ class Instance extends libLink.Link{ stripPaths: this.config.get("factorio.strip_paths"), }; + this._status = "stopped"; this._running = false; this.server = new libFactorio.FactorioServer( factorioDir, this._dir, serverOptions @@ -181,11 +182,23 @@ class Instance extends libLink.Link{ } notifyStatus(status) { + this._status = status; libLink.messages.instanceStatusChanged.send(this, { instance_id: this.config.get("instance.id"), status, }); } + /** + * Current state of the instance + * + * One of stopped, starting, running, creating_save and exporting_data + * + * @returns {string} instance status. + */ + get status() { + return this._status; + } + notifyExit() { this._running = false; this.notifyStatus("stopped"); @@ -220,6 +233,7 @@ class Instance extends libLink.Link{ } async init(pluginInfos) { + this.notifyStatus("starting"); await this.server.init(); // load plugins @@ -1088,7 +1102,6 @@ class Slave extends libLink.Link { if (instanceInfo) { instanceInfo.config.update(serialized_config, true); console.log(`Clusterio | Updated config for ${instanceInfo.path}`); - // TODO: Notify of update if instance is running } else { let instanceConfig = new libConfig.InstanceConfig(); @@ -1104,8 +1117,17 @@ class Slave extends libLink.Link { }; this.instanceInfos.set(instance_id, instanceInfo); console.log(`Clusterio | assigned instance ${instanceConfig.get("instance.name")}`); + } + // Somewhat hacky, but in the event of a lost session the status is + // resent on assigment since the master server sends an assigment + // request for all the instances it knows should be on this slave. + let instanceConnection = this.instanceConnections.get(instance_id); + libLink.messages.instanceStatusChanged.send(this, { + instance_id, + status: instanceConnection ? instanceConnection.status : "stopped", + }); // save a copy of the instance config let warnedOutput = { @@ -1116,9 +1138,6 @@ class Slave extends libLink.Link { path.join(instanceInfo.path, "instance.json"), JSON.stringify(warnedOutput, null, 4) ); - - // Notify master the instance is no longer usassigned - libLink.messages.instanceStatusChanged.send(this, { instance_id, status: "stopped" }); } /** @@ -1142,10 +1161,10 @@ class Slave extends libLink.Link { let instance = new Instance( this, connectionClient, instanceInfo.path, this.config.get("slave.factorio_directory"), instanceInfo.config ); + + this.instanceConnections.set(instanceId, instanceConnection); await instance.init(this.pluginInfos); - // XXX: race condition on multiple simultanious calls - this.instanceConnections.set(instanceId, instanceConnection); return instanceConnection; } diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index 50ec1a33..a1528e7c 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -185,6 +185,10 @@ describe("Integration of Clusterio", function() { assert.equal(await sendRcon(44, `/config get ${configName}`), `${expectedResult}\n`); } }); + it("should not change the instance status", async function() { + slowTest(this); + await checkInstanceStatus(44, "running"); + }); }); describe("user set-admin/whitelisted/banned", function() { From 4c9b91ab573d497df07d5749f2a2ae4c7285730e Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 9 Nov 2020 20:12:59 +0100 Subject: [PATCH 07/15] Lower default heartbeat interval Use 45 seconds as the default heartbeat interval instead of 60. Using 60 causes connections to drop when proxied through Nginx as the default timeout there is also 60 seconds. --- docs/configuration.md | 2 +- packages/lib/config/definitions.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 5e7f1ac1..0a5f299c 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -84,7 +84,7 @@ heartbeat interval it will be considered stale and closed. A lower value means less time between connections going stale and them being closed. -Defaults to 60 +Defaults to 45 ### master.connector_shutdown_timeout diff --git a/packages/lib/config/definitions.js b/packages/lib/config/definitions.js index b1ed056b..cc273315 100644 --- a/packages/lib/config/definitions.js +++ b/packages/lib/config/definitions.js @@ -78,7 +78,7 @@ MasterGroup.define({ title: "Heartbeat Interval", description: "Interval heartbeats are sent out on WebSocket connections", type: "number", - initial_value: 60, + initial_value: 45, }); MasterGroup.define({ name: "connector_shutdown_timeout", From 31c0925354b2811fec3bbd363578845c3b5d21d2 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 10 Nov 2020 22:13:07 +0100 Subject: [PATCH 08/15] Fix error on dash in instance name Fix stripPaths logic breaking when the instance name contains RegExp meta characters due to a lack of escaping. --- packages/lib/factorio/server.js | 2 +- test/lib/factorio/server.js | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/packages/lib/factorio/server.js b/packages/lib/factorio/server.js index 658e5a15..49da075a 100644 --- a/packages/lib/factorio/server.js +++ b/packages/lib/factorio/server.js @@ -554,7 +554,7 @@ class FactorioServer extends events.EventEmitter { this._stripRegExp = new RegExp( `(${escapeRegExp(this.writePath("temp", "currently-playing", path.sep))})|` + `(${escapeRegExp(this.writePath(path.sep))})|` + - `(\\.\\.\\.[${chars}]*?currently-playing\\${path.sep})`, + `(\\.\\.\\.[${escapeRegExp(chars)}]*?currently-playing\\${path.sep})`, "g" ); } diff --git a/test/lib/factorio/server.js b/test/lib/factorio/server.js index 533c2d9a..424b2f9b 100644 --- a/test/lib/factorio/server.js +++ b/test/lib/factorio/server.js @@ -170,6 +170,16 @@ describe("lib/factorio/server", function() { let writePath = path.join("temp", "test", "server"); let server = new libFactorio.FactorioServer(path.join("test", "file", "factorio"), writePath, {}); + describe("constructor()", function() { + it("should handle dashes in write path with strapPaths enabled", function() { + new libFactorio.FactorioServer( + path.join("test", "file", "factorio"), + path.join("temp", "test", "server-1"), + { stripPaths: true } + ); + }); + }); + describe(".init()", function() { it("should not throw on first call", async function() { await server.init(); From 42e680f09d50902e3d9d0d5084189d33a0aabf7b Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Tue, 10 Nov 2020 22:15:25 +0100 Subject: [PATCH 09/15] Fix typos in documentation Fix typos spotted by T-A-R#8121 in the documentation. --- docs/configuration.md | 2 +- packages/lib/README.md | 2 +- plugins/statistics_exporter/README.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 0a5f299c..2128d6b6 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -129,7 +129,7 @@ Defaults to "New Slave". ### slave.id -Imutable numeric id of the slave which uniquely identifies this slave in +Immutable numeric id of the slave which uniquely identifies this slave in the cluster. Defaults to a random 31 bit number. diff --git a/packages/lib/README.md b/packages/lib/README.md index db7a33ed..0035418e 100644 --- a/packages/lib/README.md +++ b/packages/lib/README.md @@ -5,4 +5,4 @@ This package contains functionality shared between master, slave, ctl and/or plugins of Clusterio. See [the Clusterio repository](https://github.com/clusterio/factorioClusterio) -for more inforation on Clusterio. +for more information on Clusterio. diff --git a/plugins/statistics_exporter/README.md b/plugins/statistics_exporter/README.md index 42b6b81a..b22a9892 100644 --- a/plugins/statistics_exporter/README.md +++ b/plugins/statistics_exporter/README.md @@ -2,7 +2,7 @@ Clusterio Statistics Exporter Plugin ==================================== Plugin exporting item production, fluid production, kill count, entity -build and poluttion statistics to the Prometheus integration to +build and pollution statistics to the Prometheus integration to Clusterio. From 49eb96631545581cd2e9a4ae69bd6efb61d3dd62 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 12 Nov 2020 04:05:24 +0100 Subject: [PATCH 10/15] Respond to unhandled requests Give an error response back to unhandled requests, this fixes requests hanging forever when the target does not support the given request. --- packages/lib/link/link.js | 12 ++++++++++++ test/lib/link/link.js | 10 ++++++++++ 2 files changed, 22 insertions(+) diff --git a/packages/lib/link/link.js b/packages/lib/link/link.js index d0b5e814..f602f92f 100644 --- a/packages/lib/link/link.js +++ b/packages/lib/link/link.js @@ -39,6 +39,18 @@ class Link { if (err.errors) { console.error(err.errors); } + + // Send back an error response if this was a request. + if ( + typeof payload.type === "string" + && payload.type.endsWith("_request") + && Number.isInteger(payload.seq) + ) { + this.connector.send( + `${payload.type.slice(0, -8)}_response`, + { seq: payload.seq, error: err.message } + ); + } } else { this.connector.emit("error", err); } diff --git a/test/lib/link/link.js b/test/lib/link/link.js index 724ecd08..b2152861 100644 --- a/test/lib/link/link.js +++ b/test/lib/link/link.js @@ -35,6 +35,16 @@ describe("lib/link/link", function() { await assert.rejects(result, new Error("Handler error")); }); + it("should give an error response back on an unhandled request", function() { + testConnector.sentMessages = []; + testConnector.emit("message", { seq: 1, type: "unhandled_request", data: {}}); + assert.deepEqual(testConnector.sentMessages, [{ + type: "unhandled_response", + seq: testConnector._seq - 1, + data: { seq: 1, error: "No validator for unhandled_request on source-target" } }, + ]) + }); + describe(".setValidator()", function() { it("should set the validator", function() { testLink.setValidator("test", () => true); From 5b74432ee3ccbf03b8d171780cceb086996f7a64 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 12 Nov 2020 04:07:08 +0100 Subject: [PATCH 11/15] Improve web interface console performance Calculate the coloring for instance console messages once instead of every time the instance console is updated. --- packages/web_ui/src/components/InstanceViewPage.jsx | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/web_ui/src/components/InstanceViewPage.jsx b/packages/web_ui/src/components/InstanceViewPage.jsx index 5e0057d6..c9686485 100644 --- a/packages/web_ui/src/components/InstanceViewPage.jsx +++ b/packages/web_ui/src/components/InstanceViewPage.jsx @@ -106,11 +106,13 @@ function formatOutputColored(output, key) { function InstanceConsole(props) { let control = useContext(ControlContext); - let [lines, setLines] = useState([{ message: "" }]); + let [lines, setLines] = useState([{""}
]); useEffect(() => { function outputHandler(output) { - setLines(currentLines => currentLines.concat([output])); + setLines(currentLines => currentLines.concat( + [formatOutputColored(output, currentLines.length)] + )); } control.onInstanceOutput(props.id, outputHandler); @@ -121,7 +123,7 @@ function InstanceConsole(props) { return <> - {lines.map(formatOutputColored)} + {lines}
; From 65a25e25100d90944349806518757cf147c2ebd7 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 12 Nov 2020 04:17:52 +0100 Subject: [PATCH 12/15] Add more context to missing handler error Amend the error message shown when a message handler is missing from a plugin with the expected name of the handler and the name of the plugin it's missing from. --- packages/lib/link/messages.js | 33 ++++++++++++++++++++++++++++++--- packages/lib/plugin.js | 22 ++++++++++++++++------ test/lib/plugin.js | 2 +- 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/packages/lib/link/messages.js b/packages/lib/link/messages.js index f486f15e..dab4fefd 100644 --- a/packages/lib/link/messages.js +++ b/packages/lib/link/messages.js @@ -3,6 +3,25 @@ const libSchema = require("@clusterio/lib/schema"); const libErrors = require("@clusterio/lib/errors"); +class MissingLinkHandlerError extends Error { + constructor(type, source, target) { + super(); + this.code = "MISSING_LINK_HANDLER"; + this.type = type; + this.source = source; + this.target = target; + this.handler = null; + this.plugin = null; + } + + get message() { + let handler = this.handler ? `${this.handler}` : "handler"; + let plugin = this.plugin ? ` on plugin ${this.plugin}` : ""; + + return `Missing ${handler}${plugin} for ${this.type} on ${this.source}-${this.target} link`; + } +} + /** * Represents a message that can be sent over the link * @@ -168,7 +187,7 @@ class Request extends Message { } if (!handler) { - throw new Error(`Missing handler for ${this.requestType} on ${link.source}-${link.target} link`); + throw new MissingLinkHandlerError(this.requestType, link.source, link.target); } // Check permission if this is a handler for a control connection on the master @@ -766,7 +785,7 @@ class Event extends Message { } if (!handler) { - throw new Error(`Missing handler for ${this.eventType} on ${link.source}-${link.target} link`); + throw new MissingLinkHandlerError(this.eventType, link.source, link.target); } link.setHandler(this.eventType, message => { @@ -959,7 +978,15 @@ messages.playerEvent = new Event({ */ function attachAllMessages(link) { for (let [name, message] of Object.entries(messages)) { - message.attach(link, link[name + message.handlerSuffix]); + let handler = `${name}${message.handlerSuffix}`; + try { + message.attach(link, link[handler]); + } catch (err) { + if (err.code === "MISSING_LINK_HANDLER") { + err.handler = handler; + } + throw err; + } } } diff --git a/packages/lib/plugin.js b/packages/lib/plugin.js index 5444ba23..f5447f3a 100644 --- a/packages/lib/plugin.js +++ b/packages/lib/plugin.js @@ -402,13 +402,23 @@ function attachPluginMessages(link, pluginInfo, plugin) { } let handler = name + messageFormat.handlerSuffix; - if (plugin === null || !plugin[handler]) { - messageFormat.attach(link); + try { + if (plugin === null || !plugin[handler]) { + messageFormat.attach(link); - } else { - messageFormat.attach(link, async function(message, format) { - return await plugin[handler](message, format, this); - }); + } else { + messageFormat.attach(link, async function(message, format) { + return await plugin[handler](message, format, this); + }); + } + + } catch (err) { + if (err.code === "MISSING_LINK_HANDLER") { + err.plugin = pluginInfo.name; + err.handler = handler; + } + + throw err; } } } diff --git a/test/lib/plugin.js b/test/lib/plugin.js index 2ca64415..bfe7fa93 100644 --- a/test/lib/plugin.js +++ b/test/lib/plugin.js @@ -55,7 +55,7 @@ describe("lib/plugin", function() { it("should throw if missing handler for the given message", function() { assert.throws( () => libPlugin.attachPluginMessages(mockLink, { name: "test", messages: { mockEvent }}, {}), - new Error("Missing handler for test:test_event on source-target link") + new Error("Missing mockEventEventHandler on plugin test for test:test_event on source-target link") ); }); it("should throw if message starts with the wrong prefix", function() { From f89e66e3cbf0a0f83af6712b32d76dc3579a138a Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 12 Nov 2020 04:19:57 +0100 Subject: [PATCH 13/15] Rename getMetrics request type Remove statistics_exporter: prefix to the getMetrics request which was left over from when this event was a part of statistics exporter. --- packages/lib/link/messages.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lib/link/messages.js b/packages/lib/link/messages.js index dab4fefd..36454012 100644 --- a/packages/lib/link/messages.js +++ b/packages/lib/link/messages.js @@ -656,7 +656,7 @@ messages.deleteUser = new Request({ }); messages.getMetrics = new Request({ - type: "statistics_exporter:get_metrics", + type: "get_metrics", links: ["master-slave", "slave-instance"], responseProperties: { "results": { type: "array" }, From 770a6664d4c81989c42d74f47c5091077714c607 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 12 Nov 2020 04:22:13 +0100 Subject: [PATCH 14/15] Add missing instance request forwarding Add missing forwardRequestToInstance default handler for Instance connections for handling request forwarding from one instance to another. --- packages/slave/slave.js | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/slave/slave.js b/packages/slave/slave.js index 0eb92e0d..7644541c 100755 --- a/packages/slave/slave.js +++ b/packages/slave/slave.js @@ -810,6 +810,22 @@ class InstanceConnection extends libLink.Link { return await request.send(this.slave, message.data); } + async forwardRequestToInstance(message, request) { + let instanceId = message.data.instance_id; + let instanceConnection = this.slave.instanceConnections.get(instanceId); + if (!instanceConnection) { + // Instance is probably on another slave + return await request.send(this.slave, message.data); + } + + if (request.plugin && !instanceConnection.plugins.has(request.plugin)) { + throw new libErrors.RequestError(`Instance ID ${instanceId} does not have ${request.plugin} plugin loaded`); + } + + return await request.send(instanceConnection, message.data); + } + + async forwardEventToInstance(message, event) { let instanceId = message.data.instance_id; let instanceConnection = this.slave.instanceConnections.get(instanceId); From f5e28f6815760d4dd0c216e3fc15ef6751bddcd3 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 12 Nov 2020 04:35:22 +0100 Subject: [PATCH 15/15] Fix errors detected by linter --- test/lib/factorio/server.js | 1 + test/lib/link/link.js | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/test/lib/factorio/server.js b/test/lib/factorio/server.js index 424b2f9b..70291e88 100644 --- a/test/lib/factorio/server.js +++ b/test/lib/factorio/server.js @@ -172,6 +172,7 @@ describe("lib/factorio/server", function() { describe("constructor()", function() { it("should handle dashes in write path with strapPaths enabled", function() { + // eslint-disable-next-line no-new new libFactorio.FactorioServer( path.join("test", "file", "factorio"), path.join("temp", "test", "server-1"), diff --git a/test/lib/link/link.js b/test/lib/link/link.js index b2152861..11bd20c5 100644 --- a/test/lib/link/link.js +++ b/test/lib/link/link.js @@ -41,8 +41,8 @@ describe("lib/link/link", function() { assert.deepEqual(testConnector.sentMessages, [{ type: "unhandled_response", seq: testConnector._seq - 1, - data: { seq: 1, error: "No validator for unhandled_request on source-target" } }, - ]) + data: { seq: 1, error: "No validator for unhandled_request on source-target" }, + }]); }); describe(".setValidator()", function() {