From 6d7a2d46d66b6be091e73d5cf2c465646e92fc47 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sun, 24 Jul 2022 11:52:26 +0200 Subject: [PATCH 01/23] Fix sorting of last seen column --- packages/web_ui/src/components/UsersPage.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web_ui/src/components/UsersPage.jsx b/packages/web_ui/src/components/UsersPage.jsx index cc5a1390..3bed4ec5 100644 --- a/packages/web_ui/src/components/UsersPage.jsx +++ b/packages/web_ui/src/components/UsersPage.jsx @@ -111,7 +111,7 @@ export default function UsersPage() { title: "Last seen", key: "lastSeen", render: user => formatLastSeen(user), - sorter: sortLastSeen, + sorter: (a, b) => sortLastSeen(a, b), responsive: ["lg"], }, ]} From 033de0e20ad14ee04645581fce0ad58202f8ec95 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sun, 5 Feb 2023 23:25:10 +0100 Subject: [PATCH 02/23] Validate plugin entrypoint Check that Master/Instance/Control plugins export the correct class and that this class is a subclass of the correct base class. This gives a better user experience when plugins are incorrectly set up and loads a duplicated clusterio/lib, and when accidentally using the wrong name when developing plugins. --- packages/lib/plugin_loader.js | 26 +++++++++++++++++----- test/lib/plugin_loader.js | 41 +++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/packages/lib/plugin_loader.js b/packages/lib/plugin_loader.js index f06e332d..b5d38eeb 100644 --- a/packages/lib/plugin_loader.js +++ b/packages/lib/plugin_loader.js @@ -6,6 +6,7 @@ "use strict"; const libErrors = require("./errors"); +const libPlugin = require("./plugin"); const path = require("path"); @@ -48,6 +49,22 @@ async function loadPluginInfos(pluginList) { return plugins; } +function loadPluginClass(entrypointName, className, pluginClass, pluginInfo) { + let resolvedPath = path.posix.join(pluginInfo.requirePath, pluginInfo[entrypointName]); + let entrypoint = require(resolvedPath); + if (!entrypoint[className]) { + throw new libErrors.PluginError(pluginInfo.name, + new Error(`Expected ${resolvedPath} to export a class named ${className}`) + ); + } + if (!(entrypoint[className].prototype instanceof pluginClass)) { + throw new libErrors.PluginError(pluginInfo.name, + new Error(`Expected ${className} exported from ${resolvedPath} to be a subclass of ${pluginClass.name}`) + ); + } + return entrypoint[className]; +} + /** * Load master plugin class of a plugin * @@ -58,8 +75,7 @@ async function loadPluginInfos(pluginList) { * @static */ async function loadMasterPluginClass(pluginInfo) { - let entrypoint = require(path.posix.join(pluginInfo.requirePath, pluginInfo.masterEntrypoint)); - return entrypoint.MasterPlugin; + return loadPluginClass("masterEntrypoint", "MasterPlugin", libPlugin.BaseMasterPlugin, pluginInfo); } /** @@ -72,8 +88,7 @@ async function loadMasterPluginClass(pluginInfo) { * @static */ async function loadInstancePluginClass(pluginInfo) { - let entrypoint = require(path.posix.join(pluginInfo.requirePath, pluginInfo.instanceEntrypoint)); - return entrypoint.InstancePlugin; + return loadPluginClass("instanceEntrypoint", "InstancePlugin", libPlugin.BaseInstancePlugin, pluginInfo); } /** @@ -86,8 +101,7 @@ async function loadInstancePluginClass(pluginInfo) { * @static */ async function loadControlPluginClass(pluginInfo) { - let entrypoint = require(path.posix.join(pluginInfo.requirePath, pluginInfo.controlEntrypoint)); - return entrypoint.ControlPlugin; + return loadPluginClass("controlEntrypoint", "ControlPlugin", libPlugin.BaseControlPlugin, pluginInfo); } module.exports = { diff --git a/test/lib/plugin_loader.js b/test/lib/plugin_loader.js index d1153c1b..d8ec95e9 100644 --- a/test/lib/plugin_loader.js +++ b/test/lib/plugin_loader.js @@ -64,4 +64,45 @@ describe("lib/plugin_loader", function() { ); }); }); + describe("loadMasterPluginClass()", function() { + let baseDir = path.join("temp", "test", "plugin"); + let missingClass = path.join(baseDir, "missing_class_plugin"); + let wrongParentClass = path.join(baseDir, "wrong_parent_class_plugin"); + before(async function() { + async function writeEntrypoint(pluginPath, content) { + await fs.outputFile(path.join(pluginPath, "master.js"), content); + } + + await writeEntrypoint(missingClass, ""); + await writeEntrypoint(wrongParentClass, "class MasterPlugin {}\n module.exports = { MasterPlugin };\n"); + }); + it("should throw if class is missing from entrypoint", async function() { + await assert.rejects( + libPluginLoader.loadMasterPluginClass({ + name: "test", + masterEntrypoint: "master", + requirePath: path.resolve(missingClass), + }), + { + message: + `PluginError: Expected ${path.resolve(missingClass, "master")} ` + + "to export a class named MasterPlugin", + } + ); + }); + it("should throw if class is not a subclass of BaseMasterPlugin", async function() { + await assert.rejects( + libPluginLoader.loadMasterPluginClass({ + name: "test", + masterEntrypoint: "master", + requirePath: path.resolve(wrongParentClass), + }), + { + message: + "PluginError: Expected MasterPlugin exported from " + + `${path.resolve(wrongParentClass, "master")} to be a subclass of BaseMasterPlugin`, + } + ); + }); + }); }); From 58e5d96e1d9b9e6ab8c82a187addda8c8586586f Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sun, 5 Feb 2023 23:27:56 +0100 Subject: [PATCH 03/23] Annotate error when loading module.json fails If a plugin has an invalid module/module.json file this should also give a readable error. --- packages/slave/src/Instance.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/slave/src/Instance.js b/packages/slave/src/Instance.js index 2d33aad9..c698f955 100644 --- a/packages/slave/src/Instance.js +++ b/packages/slave/src/Instance.js @@ -757,7 +757,12 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " "); throw new Error(`Module for plugin ${pluginName} is missing module.json`); } - let module = JSON.parse(await fs.readFile(moduleJsonPath)); + let module; + try { + module = JSON.parse(await fs.readFile(moduleJsonPath)); + } catch (err) { + throw new Error(`Loading module/module.json in plugin ${pluginName} failed: ${err.message}`); + } if (module.name !== pluginName) { throw new Error(`Expected name of module for plugin ${pluginName} to match the plugin name`); } From d29715e9ad426154b42238ec10ccab13602b0f9e Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Sun, 5 Feb 2023 23:30:08 +0100 Subject: [PATCH 04/23] Allow players to list users by default The rationale for not allowing players to list users in the cluster by default was that this would be an information disclosure of banned users in the cluster, but given that a player is a user that can connect to a server, and anyone that can connect to a server get the full banlist from the server during the connection phase this does not make much sense. Grant permission to list users by default for players. --- packages/lib/users.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/lib/users.js b/packages/lib/users.js index 4f3fa0e8..00b90de9 100644 --- a/packages/lib/users.js +++ b/packages/lib/users.js @@ -618,16 +618,19 @@ definePermission({ name: "core.user.get", title: "Get user", description: "Get the details of a user in the cluster.", + grantByDefault: true, }); definePermission({ name: "core.user.list", title: "List users", description: "Get the full list of users in the cluster.", + grantByDefault: true, }); definePermission({ name: "core.user.subscribe", title: "Subscribe to user updates", description: "Subscribe to be notified on updates on the details and status of users.", + grantByDefault: true, }); definePermission({ name: "core.user.create", From 98dc0174d665aa8e067c192f0d9259d73012da86 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 6 Feb 2023 01:41:56 +0100 Subject: [PATCH 05/23] Implement basic Summary metric Provide the basic version of Summary that does not report quantiles in the prometheus library. Useful for geting averages of observed values. --- packages/lib/prometheus.js | 236 ++++++++++++++++++++++++++++++++++++- test/lib/prometheus.js | 108 +++++++++++++++++ 2 files changed, 339 insertions(+), 5 deletions(-) diff --git a/packages/lib/prometheus.js b/packages/lib/prometheus.js index cc167625..aa96efa9 100644 --- a/packages/lib/prometheus.js +++ b/packages/lib/prometheus.js @@ -9,9 +9,8 @@ * The ordinary use case of instrumenting a code base should be covered by * {@link module:lib/prometheus.Counter}, {@link * module:lib/prometheus.Gauge}, {@link module:lib/prometheus.Histogram} and - * {@link module:lib/prometheus.exposition}. Note that the summary - * collector is not implemented. See documentation for each of the listed - * interfaces for more information. + * {@link module:lib/prometheus.exposition}. See documentation for each of + * the listed interfaces for more information. * * @example * const { Counter, exposition } = require("@clusterio/lib/prometheus"); @@ -802,7 +801,7 @@ class GaugeChild extends ValueCollectorChild { * to avoid having the time of many collectors add up to too much. * * Like with the Counter the Gauge also supports labels. When using labels - * the methods for changing the value of the counter is not longer usable + * the methods for changing the value of the counter is no longer usable * directly on the counter itself, instead a child counter with label values * set has to be retrieved with the `.labels()` method. For example: * @@ -919,6 +918,232 @@ class Gauge extends ValueCollector { } } +/** + * Child Summary holding the sum and count for a single label set. + */ +class SummaryChild { + constructor(collector, key) { + this._sumValues = collector._sumValues; + this._countValues = collector._countValues; + this._key = key; + + this._sumValues.set(key, 0); + this._countValues.set(key, 0); + } + + /** + * Sum of all observations for label set + * @type {number} + */ + get sum() { + return this._sumValues.get(this._key); + } + + /** + * Count of observations for label set + * @type {number} + */ + get count() { + return this._countValues.get(this._key); + } + + /** + * Observe a given value and increment matching buckets for label set + * + * @param {number} value - number to count into summary. + */ + observe(value) { + this._sumValues.set(this._key, this._sumValues.get(this._key) + value); + this._countValues.set(this._key, this._countValues.get(this._key) + 1); + } + + /** + * Start a timer for observing a duration for label set + * + * @returns {function()} + * function that when called will store the duration in seconds from + * when the timer was started into the metric. + */ + startTimer() { + let start = process.hrtime.bigint(); + return () => { + let end = process.hrtime.bigint(); + this.observe(Number(end - start) / 1e9); + }; + } +} + +/** + * Summary metric + * + * Sample observations into a count and sum. This is useful when you have + * an operation that reports a metric that you want insight into the rate + * and average size of values for. A common case for this is request + * duration, for example: + * + * ```js + * const requestDuration = new Summary( + * "app_request_duration_seconds", + * "Time to process app requests", + * ); + * + * async function handleRequest(...) { + * const observeDuration = requestDuration.startTimer(); + * try { + * // Do async stuff. + * } finally { + * observeDuration(); + * } + * } + * ``` + * + * Note that reporting quantiles is note supported. This makes the Summary + * less insightful than the Histogram while using less resources. + * + * The `.startTimer()` method provides a convenient interface for adding + * observed durations to the summary. Other types of values can be added to + * a summary with the `.observe()` method. + * + * Like with the Counter and Gauge the Summary collector also supports + * labels. When using labels the methods for observing values into the + * summary is no longer usable directly on the counter itself, instead a + * child summary with label values set has to be retrieved with the + * `.labels()` method. For example: + * + * ```js + * const requestDuration = new Summary( + * "app_request_duration_seconds", + * "Time to process app requests", + * { labels: ["endpoint"] } + * ); + * + * async function handleRequest(endpoint, ...) { + * const observeDuration = requestDuration.labels(endpoint).startTimer(); + * try { + * // Do async stuff. + * } finally { + * observeDuration(); + * } + * } + * ``` + * + * When using labels the summary is no longer initalized with a default + * value, and this may cause issues with aggregating and querying the + * summary in Prometheus. When dynamically setting labels it is recommended + * where possible to initialize all possible combinations of label values + * that will be used. This is done by calling `.labels()` for each + * combination, for example: + * + * ```js + * for (let endpoint of ["/status", "/api", ...) { + * requestDuration.labels(endpoint); + * } + * ``` + * + * Note that every combination of label values used creates two new time + * series that need to be stored and processed. You should carefully + * evaluate which labels you actually need as resource usage for a metric + * increases exponentially with the number of labels used. + * + * @static + */ +class Summary extends LabeledCollector { + /** + * Create optionally labeled summary + * + * @param {string} name - Name of the metric. + * @param {string} help - Help text for metric. + * @param {Object=} options - options for collector. + * @param {Array=} options.labels - + * Labels for this metric, defaults to no labels. + * @param {boolean=} options.register - + * If true registers this collector with the default registry. + * Defaults to true. + * @param {function()=} options.callback - + * Possibly async function that is called when the metric is + * collected. The collector being collected is passed as the + * argument. + */ + constructor(name, help, options = {}) { + super("summary", name, help, options, SummaryChild); + + this._sumValues = new Map(); + this._countValues = new Map(); + this._defaultChild = this.metric.labels.length ? null : this.labels({}); + } + + async* collect() { + if (this.callback) { await this.callback(this); } + yield { + metric: this.metric, + samples: new Map([ + ["_sum", this._sumValues], + ["_count", this._countValues], + ]), + }; + } + + /** + * Sum of all observations for label set + * + * Note: Only available if this is an unlabeled collector. + * @type {number} + */ + get sum() { + return this._defaultChild.sum; + } + + /** + * Count of observations for label set + * + * Note: Only available if this is an unlabeled collector. + * @type {number} + */ + get count() { + return this._defaultChild.count; + } + + /** + * Observe a given value and increment matching buckets + * + * Note: Only works if this is an unlabeled collector. + * @param {number} value - number to count into histogram buckets. + */ + observe(value) { + this._defaultChild.observe(value); + } + + /** + * Start a timer for observing a duration + * + * Note: Only works if this is an unlabeled collector. + * @returns {function()} + * function that when called will store the duration in seconds from + * when the timer was started into the metric. + */ + startTimer() { + return this._defaultChild.startTimer(); + } + + remove(...labels) { + let key = super.remove(...labels); + this._sumValues.delete(key); + this._countValues.delete(key); + } + + removeAll(labels) { + super.removeAll(labels); + removeMatchingLabels(this._sumValues, labels); + removeMatchingLabels(this._countValues, labels); + } + + clear() { + super.clear(); + this._sumValues = new Map(); + this._countValues = new Map(); + } +} + function formatValue(value) { if (value === Infinity) { return "+Inf"; @@ -1047,7 +1272,7 @@ class HistogramChild { * * Like with the Counter and Gauge the Histogram collector also supports * labels. When using labels the methods for observing values into the - * histogram is not longer usable directly on the counter itself, instead a + * histogram is no longer usable directly on the counter itself, instead a * child histogram with label values set has to be retrieved with the * `.labels()` method. For example: * @@ -1567,6 +1792,7 @@ defaultCollectors.processStartTimeSeconds.setToCurrentTime(); module.exports = { Counter, Gauge, + Summary, Histogram, CollectorRegistry, Collector, diff --git a/test/lib/prometheus.js b/test/lib/prometheus.js index 12d75532..311a893c 100644 --- a/test/lib/prometheus.js +++ b/test/lib/prometheus.js @@ -210,6 +210,114 @@ describe("lib/prometheus", function() { }); }); + describe("class Summary", function() { + let summary; + let labeledSummary; + beforeEach(function() { + summary = new prometheus.Summary( + "test", "Help", { register: false } + ); + labeledSummary = new prometheus.Summary( + "test", "Help", { register: false, labels: ["a", "b"] } + ); + }); + + describe("constructor", function() { + it("should have sum and count initialized to 0", function() { + assert.equal(summary.sum, 0); + assert.equal(summary.count, 0); + }); + it("should throw on unrecoginzed option", function() { + assert.throws( + () => new prometheus.Summary("test", "help", { invalid: 1 }), + new Error("Unrecognized option 'invalid'") + ); + }); + }); + + describe(".observe()", function() { + it("should increment sum and count", function() { + summary.observe(2); + assert.equal(summary.sum, 2); + assert.equal(summary.count, 1); + + summary.observe(4); + assert.equal(summary.sum, 6); + assert.equal(summary.count, 2); + }); + }); + + describe(".startTimer()", function() { + it("should add observations when closure is called", function() { + let observeDuration = summary.startTimer(); + assert.equal(summary.count, 0); + observeDuration(); + assert.equal(summary.count, 1); + observeDuration(); + assert.equal(summary.count, 2); + }); + }); + + describe(".labels()", function() { + it("should initialize a value for the label set", function() { + labeledSummary.labels({ a: "1", b: "2" }); + assert(labeledSummary._sumValues.has('a="1",b="2"'), "sum value not set"); + assert(labeledSummary._countValues.has('a="1",b="2"'), "count value not set"); + + labeledSummary.labels("4", "5"); + assert(labeledSummary._sumValues.has('a="4",b="5"'), "sum value not set"); + assert(labeledSummary._countValues.has('a="4",b="5"'), "count value not set"); + }); + it("should return a child supporting the summary methods", function() { + let child = labeledSummary.labels({ a: "1", b: "2" }); + void child.sum; + void child.count; + child.observe(2); + let observeDuration = child.startTimer(); + observeDuration(); + child = labeledSummary.labels("7", "8"); + void child.sum; + void child.count; + child.observe(2); + observeDuration = child.startTimer(); + observeDuration(); + }); + it("should return the same child for the same labels", function() { + let child = labeledSummary.labels("1", "2"); + assert.equal(labeledSummary.labels("1", "2"), child); + assert.equal(labeledSummary.labels({ a: "1", b: "2" }), child); + }); + }); + describe(".remove()", function() { + it("should remove a label set created with .labels()", function() { + labeledSummary.labels("1", "2"); + labeledSummary.remove("1", "2"); + assert(!labeledSummary._sumValues.has('a="1",b="2"'), "sum value not removed"); + assert(!labeledSummary._countValues.has('a="1",b="2"'), "count value not removed"); + }); + }); + describe(".removeAll()", function() { + it("should remove a label set created with .labels()", function() { + labeledSummary.labels("1", "2"); + labeledSummary.labels("1", "3"); + labeledSummary.labels("4", "6"); + labeledSummary.removeAll({ a: "1" }); + assert.deepEqual(labeledSummary._sumValues, new Map([['a="4",b="6"', 0]])); + assert.deepEqual(labeledSummary._countValues, new Map([['a="4",b="6"', 0]])); + }); + }); + describe(".clear()", function() { + it("should remove all label sets created with .labels()", function() { + labeledSummary.labels("1", "2"); + labeledSummary.labels("1", "3"); + labeledSummary.labels("4", "6"); + labeledSummary.clear(); + assert(labeledSummary._sumValues.size === 0, "sums were not cleared"); + assert(labeledSummary._countValues.size === 0, "counts were not cleared"); + }); + }); + }); + describe("class Histogram", function() { let histogram; let labeledHistogram; From 5ad89f072f8c4762609a760ae5fac7c5ac6a7836 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Wed, 8 Feb 2023 22:08:45 +0100 Subject: [PATCH 06/23] Replace endpointHitCounter Track summary statistic of total HTTP response durations using a middleware that applies for all requests instead of having every request needing to update the hit counter manually. Deprecates endpointHitCounter. --- packages/master/src/Master.js | 19 ++++++++++++++----- packages/master/src/metrics.js | 7 ++++++- packages/master/src/routes.js | 7 ------- plugins/player_auth/master.js | 4 ---- plugins/subspace_storage/master.js | 2 +- plugins/subspace_storage/routes.js | 4 +--- test/mock.js | 4 +--- 7 files changed, 23 insertions(+), 24 deletions(-) diff --git a/packages/master/src/Master.js b/packages/master/src/Master.js index 2bb3ccb7..3e57f984 100644 --- a/packages/master/src/Master.js +++ b/packages/master/src/Master.js @@ -6,7 +6,7 @@ const fs = require("fs-extra"); const http = require("http"); const https = require("https"); const path = require("path"); - +const stream = require("stream"); const libConfig = require("@clusterio/lib/config"); const libData = require("@clusterio/lib/data"); @@ -26,9 +26,10 @@ const UserManager = require("./UserManager"); const WsServer = require("./WsServer"); -const hitCounter = new libPrometheus.Counter( - "clusterio_master_http_hits_total", - "How many HTTP requests in total have been received" +const endpointDurationSummary = new libPrometheus.Summary( + "clusterio_master_http_endpoint_duration_seconds", + "Time it took to respond to a an HTTP request", + { labels: ["route"] } ); /** @@ -487,7 +488,15 @@ class Master { static addAppRoutes(app, pluginInfos) { app.use((req, res, next) => { - hitCounter.inc(); + let start = process.hrtime.bigint(); + stream.finished(res, () => { + let routePath = "static"; + if (req.route && req.route.path) { + routePath = req.route.path; + } + let end = process.hrtime.bigint(); + endpointDurationSummary.labels(routePath).observe(Number(end - start) / 1e9); + }); next(); }); app.use(compression()); diff --git a/packages/master/src/metrics.js b/packages/master/src/metrics.js index eb753828..ad400eed 100644 --- a/packages/master/src/metrics.js +++ b/packages/master/src/metrics.js @@ -1,9 +1,14 @@ // Shared metrics "use strict"; const libPrometheus = require("@clusterio/lib/prometheus"); +const util = require("util"); exports.endpointHitCounter = new libPrometheus.Counter( "clusterio_master_http_endpoint_hits_total", "How many requests a particular HTTP endpoint has gotten", - { labels: ["route"] } + { labels: ["route"], register: false } +); + +exports.endpointHitCounter.labels = util.deprecate( + exports.endpointHitCounter.labels, "incrementing endpointHitCounter is no longer needed and has no effect" ); diff --git a/packages/master/src/routes.js b/packages/master/src/routes.js index 4e48f1f5..e9af7ff3 100644 --- a/packages/master/src/routes.js +++ b/packages/master/src/routes.js @@ -19,8 +19,6 @@ const libPlugin = require("@clusterio/lib/plugin"); const libPrometheus = require("@clusterio/lib/prometheus"); const { logger } = require("@clusterio/lib/logging"); -const { endpointHitCounter } = require("./metrics"); - const finished = util.promisify(nodeStream.finished); @@ -41,8 +39,6 @@ function mergeSamples(destinationResult, sourceResult) { // Prometheus polling endpoint async function getMetrics(req, res, next) { - endpointHitCounter.labels(req.route.path).inc(); - let results = []; let pluginResults = await libPlugin.invokeHook(req.app.locals.master.plugins, "onMetrics"); for (let metricIterator of pluginResults) { @@ -181,7 +177,6 @@ function validateUserToken(req, res, next) { // Handle an uploaded export package. async function uploadExport(req, res) { - endpointHitCounter.labels(req.route.path).inc(); if (req.get("Content-Type") !== "application/zip") { res.sendStatus(415); return; @@ -340,7 +335,6 @@ async function uploadSave(req, res) { res.status(403).json({ request_errors: [err.message] }); return; } - endpointHitCounter.labels(req.route.path).inc(); let contentType = req.get("Content-Type"); let match = contentTypeRegExp.exec(contentType); @@ -479,7 +473,6 @@ async function uploadMod(req, res) { res.status(403).json({ request_errors: [err.message] }); return; } - endpointHitCounter.labels(req.route.path).inc(); let contentType = req.get("Content-Type"); let match = contentTypeRegExp.exec(contentType); diff --git a/plugins/player_auth/master.js b/plugins/player_auth/master.js index 143b2c28..f6b0dcc6 100644 --- a/plugins/player_auth/master.js +++ b/plugins/player_auth/master.js @@ -44,8 +44,6 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { }, 60e3).unref(); this.master.app.get("/api/player_auth/servers", (req, res) => { - this.metrics.endpointHitCounter.labels(req.route.path).inc(); - let servers = []; for (let instance of this.master.instances.values()) { if (instance.status === "running" && instance.config.get("player_auth.load_plugin")) { @@ -56,12 +54,10 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { }); this.master.app.post("/api/player_auth/player_code", express.json(), (req, res, next) => { - this.metrics.endpointHitCounter.labels(req.route.path).inc(); this.handlePlayerCode(req, res).catch(next); }); this.master.app.post("/api/player_auth/verify", express.json(), (req, res, next) => { - this.metrics.endpointHitCounter.labels(req.route.path).inc(); this.handleVerify(req, res).catch(next); }); } diff --git a/plugins/subspace_storage/master.js b/plugins/subspace_storage/master.js index dc8e32d7..9e8a4ffc 100644 --- a/plugins/subspace_storage/master.js +++ b/plugins/subspace_storage/master.js @@ -86,7 +86,7 @@ class MasterPlugin extends libPlugin.BaseMasterPlugin { this.subscribedControlLinks = new Set(); - routes.addApiRoutes(this.master.app, this.items, this.metrics.endpointHitCounter); + routes.addApiRoutes(this.master.app, this.items); } updateStorage() { diff --git a/plugins/subspace_storage/routes.js b/plugins/subspace_storage/routes.js index 16223b9a..4ce93818 100644 --- a/plugins/subspace_storage/routes.js +++ b/plugins/subspace_storage/routes.js @@ -1,5 +1,5 @@ "use strict"; -function addApiRoutes(app, items, endpointHitCounter) { +function addApiRoutes(app, items) { /** * GET endpoint to read the masters current inventory of items. @@ -10,7 +10,6 @@ function addApiRoutes(app, items, endpointHitCounter) { * @returns {object[]} JSON [{name:"iron-plate", count:100},{name:"copper-plate",count:5}] */ app.get("/api/inventory", (req, res) => { - endpointHitCounter.labels(req.route.path).inc(); res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); // Check it and send it @@ -31,7 +30,6 @@ function addApiRoutes(app, items, endpointHitCounter) { * @returns {object} JSON {"iron-plate":100, "copper-plate":5} */ app.get("/api/inventoryAsObject", (req, res) => { - endpointHitCounter.labels(req.route.path).inc(); res.header("Access-Control-Allow-Origin", "*"); res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); // Check it and send it diff --git a/test/mock.js b/test/mock.js index 00d46ceb..a346d408 100644 --- a/test/mock.js +++ b/test/mock.js @@ -219,9 +219,7 @@ class MockMaster { async function createMasterPlugin(MasterPluginClass, info) { let master = new MockMaster(); - let metrics = { - endpointHitCounter: new libPrometheus.Counter("hit_counter", "Hit Counter", { labels: ["route"] }), - }; + let metrics = {}; let logger = new MockLogger(); let plugin = new MasterPluginClass(info, master, metrics, logger); await plugin.init(); From 3d186a24ff49883271f6814dde71723d8c733804 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Wed, 8 Feb 2023 22:52:09 +0100 Subject: [PATCH 07/23] Fix incorrect handling of 0 ids Fix tests of slave and instance ids that incorrectly return the wrong result when the id is 0. --- packages/lib/logging.js | 4 ++-- packages/master/src/ControlConnection.js | 2 +- packages/web_ui/src/util/websocket.jsx | 4 ++-- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/lib/logging.js b/packages/lib/logging.js index e55fa73e..fe6c6300 100644 --- a/packages/lib/logging.js +++ b/packages/lib/logging.js @@ -58,11 +58,11 @@ class WebConsoleFormat { transform(info, options) { let src = " "; - if (info.slave_id) { + if (info.slave_id !== undefined) { let slaveName = info.slave_id; src += `s:${slaveName} - `; } - if (info.instance_id) { + if (info.instance_id !== undefined) { let instanceName = info.instance_id; src += `i:${instanceName}`; } diff --git a/packages/master/src/ControlConnection.js b/packages/master/src/ControlConnection.js index 9856b33b..2ce4d417 100644 --- a/packages/master/src/ControlConnection.js +++ b/packages/master/src/ControlConnection.js @@ -289,7 +289,7 @@ class ControlConnection extends BaseConnection { async updateInstanceConfig(instance) { let slaveId = instance.config.get("instance.assigned_slave"); - if (slaveId) { + if (slaveId !== null) { let connection = this._master.wsServer.slaveConnections.get(slaveId); if (connection) { await libLink.messages.assignInstance.send(connection, { diff --git a/packages/web_ui/src/util/websocket.jsx b/packages/web_ui/src/util/websocket.jsx index fd9b56a3..b6d72b15 100644 --- a/packages/web_ui/src/util/websocket.jsx +++ b/packages/web_ui/src/util/websocket.jsx @@ -34,10 +34,10 @@ function logFilter({ all, master, slave_ids, instance_ids }, info) { if (master && info.slave_id === undefined) { return true; } - if (info.slave_id && slave_ids.includes(info.slave_id)) { + if (info.slave_id !== undefined && slave_ids.includes(info.slave_id)) { return true; } - if (info.instance_id && instance_ids.includes(info.instance_id)) { + if (info.instance_id !== undefined && instance_ids.includes(info.instance_id)) { return true; } return false; From 351fcba4f8219d135b4b40342a4a2a01aea21c0f Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Wed, 8 Feb 2023 23:09:40 +0100 Subject: [PATCH 08/23] Print version on startup Help determining what is running by printing the version that is running on master and slave startup. --- packages/master/master.js | 12 +++++++----- packages/slave/slave.js | 6 +++++- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/packages/master/master.js b/packages/master/master.js index 39214a98..59bd8317 100755 --- a/packages/master/master.js +++ b/packages/master/master.js @@ -38,6 +38,7 @@ const libLoggingUtils = require("@clusterio/lib/logging_utils"); const Master = require("./src/Master"); const UserManager = require("./src/UserManager"); +const version = require("./package").version; // globals let master; @@ -284,6 +285,12 @@ async function initialize() { })); } + let command = parameters.args._[0]; + if (command === "run") { + logger.info(`Starting Clusterio master ${version}`); + parameters.shouldRun = true; + } + logger.info(`Loading available plugins from ${parameters.args.pluginList}`); let pluginList = new Map(); try { @@ -295,7 +302,6 @@ async function initialize() { } // If the command is plugin management we don't try to load plugins - let command = parameters.args._[0]; if (command === "plugin") { await libSharedCommands.handlePluginCommand(parameters.args, pluginList, parameters.args.pluginList); return parameters; @@ -336,15 +342,11 @@ async function initialize() { await libSharedCommands.handleConfigCommand( parameters.args, parameters.masterConfig, parameters.masterConfigPath ); - return parameters; } else if (command === "bootstrap") { await handleBootstrapCommand(parameters.args, parameters.masterConfig); - return parameters; } - // If we get here the command was run - parameters.shouldRun = true; return parameters; } diff --git a/packages/slave/slave.js b/packages/slave/slave.js index 8f9f70e7..f58c4c31 100755 --- a/packages/slave/slave.js +++ b/packages/slave/slave.js @@ -129,6 +129,11 @@ async function startSlave() { })); } + let command = args._[0]; + if (command === "run") { + logger.info(`Starting Clusterio slave ${version}`); + } + // add better stack traces on promise rejection process.on("unhandledRejection", err => logger.error(`Unhandled rejection:\n${err.stack}`)); @@ -143,7 +148,6 @@ async function startSlave() { } // If the command is plugin management we don't try to load plugins - let command = args._[0]; if (command === "plugin") { await libSharedCommands.handlePluginCommand(args, pluginList, args.pluginList); return; From d39b525b07f85535cc28e95290dd58ed555d65db Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 9 Feb 2023 23:13:15 +0100 Subject: [PATCH 09/23] Add clusterio_master_log_bytes metric Export a metric for total size of logs stored on the master server. --- packages/lib/file_ops.js | 34 ++++++++++++++++++++++++++++++++++ packages/master/src/Master.js | 19 +++++++++++++++++++ test/lib/file_ops.js | 15 +++++++++++++++ 3 files changed, 68 insertions(+) diff --git a/packages/lib/file_ops.js b/packages/lib/file_ops.js index be007912..e0b4639d 100644 --- a/packages/lib/file_ops.js +++ b/packages/lib/file_ops.js @@ -5,6 +5,7 @@ const fs = require("fs-extra"); const path = require("path"); const crypto = require("crypto"); // needed for getTempFile + /** * Returns the newest file in a directory * @@ -32,6 +33,38 @@ async function getNewestFile(directory, filter = (name) => true) { return newestFile; } +/** + * Returns the total size of all files in a directory + * + * Sums up the file size of all files in the given directory if it exists. + * Error reading the size of files are ignored, and error reading the + * directory will result in 0 being returned. + * + * @param {string} directory - The directory to sum files in. + * @returns {Promise} The size in bytes of all files in the directory. + */ +async function directorySize(directory) { + let dirEntries; + try { + dirEntries = await fs.readdir(directory, { withFileTypes: true }); + } catch (err) { + if (err.code === "ENOENT") { + return 0; + } + throw err; + } + let statTasks = []; + for (let entry of dirEntries) { + if (entry.isFile()) { + statTasks.push(fs + .stat(path.join(directory, entry.name)) + .then(stat => stat.size, _ => 0) + ); + } + } + return (await Promise.all(statTasks)).reduce((a, v) => a + v, 0); +} + /** * Modifies name in case it already exisist at the given directory * @@ -193,6 +226,7 @@ function cleanFilename(name) { module.exports = { getNewestFile, + directorySize, findUnusedName, getTempFile, safeOutputFile, diff --git a/packages/master/src/Master.js b/packages/master/src/Master.js index 3e57f984..be8b5b2a 100644 --- a/packages/master/src/Master.js +++ b/packages/master/src/Master.js @@ -32,6 +32,11 @@ const endpointDurationSummary = new libPrometheus.Summary( { labels: ["route"] } ); +const logSizeGauge = new libPrometheus.Gauge( + "clusterio_master_log_bytes", + "Size of all log files currently stored on the master." +); + /** * Manages all master related operations * @alias module:master/src/Master @@ -131,6 +136,19 @@ class Master { } } + /** + * Get the total size of the logs stored + * + * @returns {Promise} size in bytes of stored log files. + */ + async logSize() { + return (await Promise.all([ + libFileOps.directorySize(path.join(this.logDirectory, "cluster")), + libFileOps.directorySize(path.join(this.logDirectory, "master")), + libFileOps.directorySize(path.join(this.logDirectory, "slave")), + ])).reduce((a, v) => a + v, 0); + } + async _startInternal(args) { this.logDirectory = args.logDirectory; this.clusterLogIndex = await libLoggingUtils.LogIndex.load(path.join(this.logDirectory, "cluster")); @@ -139,6 +157,7 @@ class Master { err => logger.error(`Error building cluster log index:\n${err.stack}`) ); }, 600e3); + logSizeGauge.callback = async () => { logSizeGauge.set(await this.logSize()); }; // Start webpack development server if enabled if (args.dev || args.devPlugin) { diff --git a/test/lib/file_ops.js b/test/lib/file_ops.js index da256995..e9184090 100644 --- a/test/lib/file_ops.js +++ b/test/lib/file_ops.js @@ -41,6 +41,21 @@ describe("lib/file_ops", function() { }); }); + describe("getNewestFile()", function() { + it("returns 0 if directory does not exist", async function() { + let size = await libFileOps.directorySize(path.join(baseDir, "invalid")); + assert.equal(size, 0); + }); + it("returns 0 if directory is empty", async function() { + let size = await libFileOps.directorySize(path.join(baseDir, "test", "folder")); + assert.equal(size, 0); + }); + it("returns size of files in directory", async function() { + let size = await libFileOps.directorySize(path.join(baseDir, "test")); + assert.equal(size, 21); + }); + }); + describe("findUnusedName()", function() { it("should return named unchanged if it does not exist", async function() { let cases = [ From a7dedcd3b7067f327f3e348754aec55a0e79f35f Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Thu, 9 Feb 2023 23:24:20 +0100 Subject: [PATCH 10/23] Trim whitespace from token login in Web UI Prevent accidentally copying spaces before and/or after the token when pasting it into the login form from failing to log in. --- packages/web_ui/src/components/LoginForm.jsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/web_ui/src/components/LoginForm.jsx b/packages/web_ui/src/components/LoginForm.jsx index d3b22a38..3b2d9630 100644 --- a/packages/web_ui/src/components/LoginForm.jsx +++ b/packages/web_ui/src/components/LoginForm.jsx @@ -13,7 +13,7 @@ function TokenAuth(props) { If you have generated an authentication token from the master server, use that here.
{ props.setToken(values.token); }} + onFinish={(values) => { props.setToken(values.token.trim()); }} layout="inline" > Date: Fri, 10 Feb 2023 00:59:56 +0100 Subject: [PATCH 11/23] Fix incorrect JSDoc async return annotations The return type of an async function should be documented as a Promise of what the function returns as this is what JSDoc expects. --- packages/lib/command.js | 6 +++--- packages/lib/factorio/export.js | 8 ++++---- packages/lib/factorio/server.js | 10 +++++----- packages/lib/file_ops.js | 6 +++--- packages/lib/helpers.js | 2 +- packages/lib/link/messages.js | 2 +- packages/lib/logging_utils.js | 4 ++-- packages/lib/plugin.js | 2 +- packages/lib/plugin_loader.js | 8 ++++---- packages/master/src/Master.js | 6 +++--- packages/slave/src/Instance.js | 8 ++++---- packages/slave/src/Slave.js | 4 ++-- 12 files changed, 33 insertions(+), 33 deletions(-) diff --git a/packages/lib/command.js b/packages/lib/command.js index f0738e8b..85b0f864 100644 --- a/packages/lib/command.js +++ b/packages/lib/command.js @@ -142,7 +142,7 @@ class CommandTree { * * @param {module:lib/link.Link} client - link to master server to query slave on. * @param {string} slaveName - string with name or id of slave. - * @returns {number} slave ID. + * @returns {Promise} slave ID. * @static */ async function resolveSlave(client, slaveName) { @@ -174,7 +174,7 @@ async function resolveSlave(client, slaveName) { * * @param {module:lib/link.Link} client - link to master server to query instance on. * @param {string} instanceName - string with name or id of instance. - * @returns {number} instance ID. + * @returns {Promise} instance ID. * @static */ async function resolveInstance(client, instanceName) { @@ -239,7 +239,7 @@ async function resolveModPack(client, modPackName) { * * @param {module:lib/link.Link} client - link to master server to query role on. * @param {string} roleName - string with name or id of role. - * @returns {Object} Role info. + * @returns {Promise} Role info. * @static */ async function retrieveRole(client, roleName) { diff --git a/packages/lib/factorio/export.js b/packages/lib/factorio/export.js index 22c74de9..f30d33bf 100644 --- a/packages/lib/factorio/export.js +++ b/packages/lib/factorio/export.js @@ -67,7 +67,7 @@ async function loadZip(server, modVersions, mod) { * The server to load the file from. * @param {Map} modVersions - Mapping of mod to version used. * @param {string} modPath - Factorio style path to the file to load. - * @returns {?Buffer} The content of the file or null if not found. + * @returns {Promise} The content of the file or null if not found. * @memberof module:lib/factorio * @private * @inner @@ -255,7 +255,7 @@ function filterItems(prototypes) { * @param {Map} modVersions - * Mapping of mod name to versions to get icons from. * @param {Array} items - Array of item prototypes. - * @return {{itemSheet: Jimp, itemData: Map}} + * @return {Promise<{itemSheet: Jimp, itemData: Map}>} * Item spritesheet and metadata. * @memberof module:lib/factorio * @private @@ -327,7 +327,7 @@ async function exportItems(server, modVersions, items) { * Mapping of mod name to version to export locale from. * @param {Array} modOrder - Load order of the mods. * @param {string} languageCode - Language to export locale for. - * @returns {Map} merged locale information + * @returns {Promise>} merged locale information * @memberof module:lib/factorio * @private * @inner @@ -379,7 +379,7 @@ async function exportLocale(server, modVersions, modOrder, languageCode) { * * @param {module:lib/factorio.FactorioServer} server - * The server to export the data from. - * @returns {JSZip} zip file with exported data. + * @returns {Promise} zip file with exported data. * @memberof module:lib/factorio */ async function exportData(server) { diff --git a/packages/lib/factorio/server.js b/packages/lib/factorio/server.js index a199f04e..83451086 100644 --- a/packages/lib/factorio/server.js +++ b/packages/lib/factorio/server.js @@ -86,7 +86,7 @@ function versionOrder(a, b) { * @param {string} targetVersion - * Version to look for, supports the special value "latest" for the * latest version available. - * @returns {Array} Array with path to data dir and version found. + * @returns {Promise} Array with path to data dir and version found. * @memberof module:lib/factorio * @private * @inner @@ -163,7 +163,7 @@ function randomDynamicPort() { * the given length. * * @param {number} length - the length of the password to generate. - * @return {string} password of the given length + * @return {Promise} password of the given length * @memberof module:lib/factorio * @private * @inner @@ -902,7 +902,7 @@ class FactorioServer extends events.EventEmitter { * @param {boolean} expectEmpty - * if true throw if the response is not empty. Useful for detecting * errors that might have been sent in response. - * @returns {string} response from server. + * @returns {Promise} response from server. */ async sendRcon(message, expectEmpty) { this._check(["running", "stopping"]); @@ -1036,7 +1036,7 @@ class FactorioServer extends events.EventEmitter { * Ensures achievements are disabled on the save that's running. This is * necessary in order to run any commands at all. * - * @returns {boolean} + * @returns {Promise} * True if acheivements got disabled and false if they already where * disabled. */ @@ -1104,7 +1104,7 @@ class FactorioServer extends events.EventEmitter { * * Loads server-settings.example.json from the data dir. * - * @returns {object} the parsed server-settings. + * @returns {Promise} the parsed server-settings. */ async exampleSettings() { return JSON.parse(await fs.readFile(this.dataPath("server-settings.example.json"), "utf-8")); diff --git a/packages/lib/file_ops.js b/packages/lib/file_ops.js index e0b4639d..0e14b39d 100644 --- a/packages/lib/file_ops.js +++ b/packages/lib/file_ops.js @@ -13,7 +13,7 @@ const crypto = require("crypto"); // needed for getTempFile * @param {function(string): boolean} filter - * Optional function to filter out files to ignore. Should return true * if the file is to be considered. - * @returns {?string} + * @returns {Promise} * Name of the file with the newest timestamp or null if the * directory contains no files. */ @@ -80,8 +80,8 @@ async function directorySize(directory) { * @param {string} directory - directory to check in. * @param {string} name - file name to check for, may have extension. * @param {string} extension - dot extension used for the file name. - * @returns {string} modified name with extension that likely does not exist - * in the folder + * @returns {Promise} modified name with extension that likely does + * not exist in the folder */ async function findUnusedName(directory, name, extension = "") { if (extension && name.endsWith(extension)) { diff --git a/packages/lib/helpers.js b/packages/lib/helpers.js index 3738666e..9069a198 100644 --- a/packages/lib/helpers.js +++ b/packages/lib/helpers.js @@ -63,7 +63,7 @@ async function timeout(promise, time, timeoutResult) { * returns all the data which was read from the stream. * * @param {Readable} stream - byte stream to read to the end. - * @returns {Buffer} content of the stream. + * @returns {Promise} content of the stream. * @static */ async function readStream(stream) { diff --git a/packages/lib/link/messages.js b/packages/lib/link/messages.js index 3da48aec..21a063db 100644 --- a/packages/lib/link/messages.js +++ b/packages/lib/link/messages.js @@ -255,7 +255,7 @@ class Request extends Message { * * @param {module:lib/link.Link} link - Link to send request over. * @param {Object} data - Data to send with the request. - * @returns {object} response data + * @returns {Promise} response data */ async send(link, data = {}) { // XXX validate link target/source? diff --git a/packages/lib/logging_utils.js b/packages/lib/logging_utils.js index 37e818e7..d23a80cb 100644 --- a/packages/lib/logging_utils.js +++ b/packages/lib/logging_utils.js @@ -205,7 +205,7 @@ class LogIndex { * module:lib/logging_utils.LogIndex#save}. * * @param {string} logDirectory - Path to directory to load from. - * @returns {module:lib/logging_utils.LogIndex} loaded or created log index. + * @returns {Promise} loaded or created log index. */ static async load(logDirectory) { try { @@ -391,7 +391,7 @@ function logFilter({ all, master, slave_ids, instance_ids, max_level }) { * Filter to limit logs by. * @param {module:lib/logging_utils.LogIndex=} index - * Index to speed up query with. - * @returns {Array} log entries matching the filter + * @returns {Promise>} log entries matching the filter */ async function queryLog(logDirectory, filter, index) { let files = (await fs.readdir(logDirectory)).filter(entry => logFileGlob.test(entry)); diff --git a/packages/lib/plugin.js b/packages/lib/plugin.js index ac2d3ffb..4b56b976 100644 --- a/packages/lib/plugin.js +++ b/packages/lib/plugin.js @@ -812,7 +812,7 @@ function attachPluginMessages(link, plugin) { * Mapping of plugin names to plugins to invoke the hook on. * @param {string} hook - Name of hook to invoke. * @param {...*} args - Arguments to pass on to the hook. - * @returns {Array} Non-empty return values from the hooks. + * @returns {Promise} Non-empty return values from the hooks. */ async function invokeHook(plugins, hook, ...args) { let results = []; diff --git a/packages/lib/plugin_loader.js b/packages/lib/plugin_loader.js index b5d38eeb..6ec0310a 100644 --- a/packages/lib/plugin_loader.js +++ b/packages/lib/plugin_loader.js @@ -19,7 +19,7 @@ const path = require("path"); * * @param {Map} pluginList - * Mapping of plugin name to require path for the plugins to load. - * @returns {Array} Array of plugin info modules. + * @returns {Promise>} Array of plugin info modules. * @static */ async function loadPluginInfos(pluginList) { @@ -71,7 +71,7 @@ function loadPluginClass(entrypointName, className, pluginClass, pluginInfo) { * @param {Object} pluginInfo - * Plugin info object returned from {@link * module:lib/plugin_loader.loadPluginInfos} to load class from. - * @returns {function} plugin class + * @returns {Promise} plugin class * @static */ async function loadMasterPluginClass(pluginInfo) { @@ -84,7 +84,7 @@ async function loadMasterPluginClass(pluginInfo) { * @param {Object} pluginInfo - * Plugin info object returned from {@link * module:lib/plugin_loader.loadPluginInfos} to load class from. - * @returns {function} plugin class + * @returns {Promise} plugin class * @static */ async function loadInstancePluginClass(pluginInfo) { @@ -97,7 +97,7 @@ async function loadInstancePluginClass(pluginInfo) { * @param {Object} pluginInfo - * Plugin info object returned from {@link * module:lib/plugin_loader.loadPluginInfos} to load class from. - * @returns {function} plugin class + * @returns {Promise} plugin class * @static */ async function loadControlPluginClass(pluginInfo) { diff --git a/packages/master/src/Master.js b/packages/master/src/Master.js index be8b5b2a..6fb10989 100644 --- a/packages/master/src/Master.js +++ b/packages/master/src/Master.js @@ -484,7 +484,7 @@ class Master { * @param {module:lib/logging_utils~QueryLogFilter} filter - * Filter to limit entries with. Note that only the master log can * be queried from this function. - * @returns {Array} log entries matching the filter + * @returns {Promise>} log entries matching the filter */ async queryMasterLog(filter) { return await libLoggingUtils.queryLog( @@ -497,7 +497,7 @@ class Master { * * @param {module:lib/logging_utils~QueryLogFilter} filter - * Filter to limit entries with. - * @returns {Array} log entries matching the filter + * @returns {Promise>} log entries matching the filter */ async queryClusterLog(filter) { return await libLoggingUtils.queryLog( @@ -811,7 +811,7 @@ class Master { * @param {object} data - Data to pass with the request. * @param {number} data.instance_id - * ID of instance to send this request towards - * @returns {object} data returned from the request. + * @returns {Promise} data returned from the request. */ async forwardRequestToInstance(request, data) { let instance = this.getRequestInstance(data.instance_id); diff --git a/packages/slave/src/Instance.js b/packages/slave/src/Instance.js index c698f955..a3909c07 100644 --- a/packages/slave/src/Instance.js +++ b/packages/slave/src/Instance.js @@ -193,7 +193,7 @@ class Instance extends libLink.Link { /** * Mod pack currently running on this instance - * @type {module:lib/data.ModPack>} + * @type {module:lib/data.ModPack} */ this.activeModPack = undefined; @@ -538,7 +538,7 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " "); * entries from the given settings object. * * @param {Object} overrides - Server settings to override. - * @returns {Object} + * @returns {Promise} * server example settings with the given settings applied over it. */ async resolveServerSettings(overrides) { @@ -699,10 +699,10 @@ rcon.print(game.table_to_json(players))`.replace(/\r?\n/g, " "); * * Creates a new save if no save is passed and patches it with modules. * - * @param {String|null} saveName - + * @param {string|null} saveName - * Save to prepare from the instance saves directory. Creates a new * save if null. - * @returns {String} Name of the save prepared. + * @returns {Promise} Name of the save prepared. */ async prepareSave(saveName) { // Use latest save if no save was specified diff --git a/packages/slave/src/Slave.js b/packages/slave/src/Slave.js index c8b85f57..f3af8dca 100644 --- a/packages/slave/src/Slave.js +++ b/packages/slave/src/Slave.js @@ -41,7 +41,7 @@ function checkRequestSaveName(name) { * instanceInfo objects. * * @param {string} instancesDir - Directory containing instances - * @returns {Map} + * @returns {Promise>} * mapping between instance id and information about this instance. * @private */ @@ -379,7 +379,7 @@ class Slave extends libLink.Link { * Initialize and connect an unloaded instance * * @param {number} instanceId - ID of instance to initialize. - * @returns {module:slave/slave~InstanceConnection} connection to instance. + * @returns {Promise} connection to instance. */ async _connectInstance(instanceId) { let instanceInfo = this.getRequestInstanceInfo(instanceId); From 082d07d718f027efa3efae9c4d2ed0aa344aef32 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Fri, 10 Feb 2023 01:33:34 +0100 Subject: [PATCH 12/23] Fix no way to unassign instances in Web UI --- packages/master/src/ControlConnection.js | 3 +++ packages/web_ui/src/components/AssignInstanceModal.jsx | 9 ++++++++- packages/web_ui/src/components/InstanceList.jsx | 3 +++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/packages/master/src/ControlConnection.js b/packages/master/src/ControlConnection.js index 2ce4d417..43379633 100644 --- a/packages/master/src/ControlConnection.js +++ b/packages/master/src/ControlConnection.js @@ -356,6 +356,9 @@ class ControlConnection extends BaseConnection { instance_id, serialized_config: instance.config.serialize("slave"), }); + } else { + instance.status = "unassigned"; + this._master.instanceUpdated(instance); } } diff --git a/packages/web_ui/src/components/AssignInstanceModal.jsx b/packages/web_ui/src/components/AssignInstanceModal.jsx index 00e4b047..8035a7eb 100644 --- a/packages/web_ui/src/components/AssignInstanceModal.jsx +++ b/packages/web_ui/src/components/AssignInstanceModal.jsx @@ -28,6 +28,10 @@ export default function AssignInstanceModal(props) { return; } + if (slaveId === "null") { + slaveId = null; + } + setApplying(true); libLink.messages.assignInstanceCommand.send(control, { instance_id: props.id, @@ -67,9 +71,12 @@ export default function AssignInstanceModal(props) { the instance. Note that reassigning an instance from one slave to another will not move the server save over. - +