Add Instance.id shortcut

Add shortcut for instance.config.get("instance.id") similar to the
already existing shortcut for its name.
This commit is contained in:
Hornwitser committed 2021-02-19 14:57:27 +01:00
1 parent 1de8b76303
commit 7e9f1562e4
5 files changed
+24 -18

No files matched your search

+1 -1
View File
@@ -660,7 +660,7 @@ set it accordingly, for example:
); );
// Somewhere in the instance plugin code // Somewhere in the instance plugin code
barMetric.labels(String(this.instance.config.get("instance.id"))).set(someValue); barMetric.labels(String(this.instance.id)).set(someValue);
Metrics are automatically registered to the default registry, and this Metrics are automatically registered to the default registry, and this
default registry is automatically polled by the master server on slaves. default registry is automatically polled by the master server on slaves.
+19 -14
View File
@@ -128,9 +128,15 @@ class Instance extends libLink.Link {
this.plugins = new Map(); this.plugins = new Map();
this.config = instanceConfig; this.config = instanceConfig;
/**
* ID of this instance, equivalenet to `instance.config.get("instance.id")`.
* @constant {number}
*/
this.id = this.config.get("instance.id");
this.logger = logger.child({ this.logger = logger.child({
instance_id: this.config.get("instance.id"), instance_id: this.id,
instance_name: this.config.get("instance.name"), instance_name: this.name,
}); });
this._configFieldChanged = (group, field, prev) => { this._configFieldChanged = (group, field, prev) => {
@@ -168,7 +174,7 @@ class Instance extends libLink.Link {
); );
this.server.on("output", (parsed, line) => { this.server.on("output", (parsed, line) => {
this.logger.log("server", { message: line, instance_id: this.config.get("instance.id"), parsed }); this.logger.log("server", { message: line, instance_id: this.id, parsed });
libPlugin.invokeHook(this.plugins, "onOutput", parsed, line); libPlugin.invokeHook(this.plugins, "onOutput", parsed, line);
}); });
@@ -185,7 +191,7 @@ class Instance extends libLink.Link {
this.server.on("ipc-player_event", event => { this.server.on("ipc-player_event", event => {
libLink.messages.playerEvent.send(this, { libLink.messages.playerEvent.send(this, {
instance_id: this.config.get("instance.id"), instance_id: this.id,
...event, ...event,
}); });
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event); libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
@@ -193,7 +199,7 @@ class Instance extends libLink.Link {
} }
async sendRcon(message, expectEmpty, plugin = "") { async sendRcon(message, expectEmpty, plugin = "") {
let instanceId = String(this.config.get("instance.id")); let instanceId = String(this.id);
let observeDuration = instanceRconCommandDuration.labels(instanceId).startTimer(); let observeDuration = instanceRconCommandDuration.labels(instanceId).startTimer();
try { try {
return await this.server.sendRcon(message, expectEmpty); return await this.server.sendRcon(message, expectEmpty);
@@ -205,13 +211,13 @@ class Instance extends libLink.Link {
async _autosave(name) { async _autosave(name) {
let stat = await fs.stat(this.path("saves", `${name}.zip`)); let stat = await fs.stat(this.path("saves", `${name}.zip`));
instanceFactorioAutosaveSize.labels(String(this.config.get("instance.id"))).set(stat.size); instanceFactorioAutosaveSize.labels(String(this.id)).set(stat.size);
} }
notifyStatus(status) { notifyStatus(status) {
this._status = status; this._status = status;
libLink.messages.instanceStatusChanged.send(this, { libLink.messages.instanceStatusChanged.send(this, {
instance_id: this.config.get("instance.id"), status, instance_id: this.id, status,
}); });
} }
@@ -238,7 +244,7 @@ class Instance extends libLink.Link {
collector instanceof libPrometheus.ValueCollector collector instanceof libPrometheus.ValueCollector
&& collector.metric.labels.includes("instance_id") && collector.metric.labels.includes("instance_id")
) { ) {
collector.removeAll({ instance_id: String(this.config.get("instance.id")) }); collector.removeAll({ instance_id: String(this.id) });
} }
} }
@@ -285,7 +291,7 @@ class Instance extends libLink.Link {
for (let [name, plugin] of this.plugins) { for (let [name, plugin] of this.plugins) {
plugins[name] = plugin.info.version; plugins[name] = plugin.info.version;
} }
libLink.messages.instanceInitialized.send(this, { instance_id: this.config.get("instance.id"), plugins }); libLink.messages.instanceInitialized.send(this, { instance_id: this.id, plugins });
} }
/** /**
@@ -543,9 +549,8 @@ class Instance extends libLink.Link {
* Update instance information on the Factorio side * Update instance information on the Factorio side
*/ */
async updateInstanceData() { async updateInstanceData() {
let name = libLuaTools.escapeString(this.config.get("instance.name")); let name = libLuaTools.escapeString(this.name);
let id = this.config.get("instance.id"); await this.sendRcon(`/sc clusterio_private.update_instance(${this.id}, "${name}")`, true);
await this.sendRcon(`/sc clusterio_private.update_instance(${id}, "${name}")`, true);
} }
async updateFactorioSettings(current, previous) { async updateFactorioSettings(current, previous) {
@@ -648,8 +653,8 @@ class Instance extends libLink.Link {
let pid = this.server.pid; let pid = this.server.pid;
if (pid) { if (pid) {
let stats = await pidusage(pid); let stats = await pidusage(pid);
instanceFactorioCpuTime.labels(String(this.config.get("instance.id"))).set(stats.ctime / 1000); instanceFactorioCpuTime.labels(String(this.id)).set(stats.ctime / 1000);
instanceFactorioMemoryUsage.labels(String(this.config.get("instance.id"))).set(stats.memory); instanceFactorioMemoryUsage.labels(String(this.id)).set(stats.memory);
} }
return { results }; return { results };
+1 -1
View File
@@ -53,7 +53,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
throw new Error(`Error parsing statistics JSON: ${err.message}, content "${string}"`); throw new Error(`Error parsing statistics JSON: ${err.message}, content "${string}"`);
} }
let instanceId = this.instance.config.get("instance.id"); let instanceId = this.instance.id;
instanceGameTicksTotal.labels(String(instanceId)).set(stats.game_tick); instanceGameTicksTotal.labels(String(instanceId)).set(stats.game_tick);
instancePlayerCount.labels(String(instanceId)).set(stats.player_count); instancePlayerCount.labels(String(instanceId)).set(stats.player_count);
+2 -2
View File
@@ -39,7 +39,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
async provideItems(items) { async provideItems(items) {
this.info.messages.place.send(this.instance, { this.info.messages.place.send(this.instance, {
items, items,
instance_id: this.instance.config.get("instance.id"), instance_id: this.instance.id,
}); });
if (this.instance.config.get("subspace_storage.log_item_transfers")) { if (this.instance.config.get("subspace_storage.log_item_transfers")) {
@@ -52,7 +52,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
async requestItems(items) { async requestItems(items) {
// Request the items all at once // Request the items all at once
let response = await this.info.messages.remove.send(this.instance, { let response = await this.info.messages.remove.send(this.instance, {
instance_id: this.instance.config.get("instance.id"), instance_id: this.instance.id,
items, items,
}); });
+1
View File
@@ -103,6 +103,7 @@ class MockInstance extends libLink.Link {
this.logger = new MockLogger(); this.logger = new MockLogger();
this.server = new MockServer(); this.server = new MockServer();
this.name = "test"; this.name = "test";
this.id = 7357;
this.mockConfigEntries = new Map([ this.mockConfigEntries = new Map([
["instance.id", 7357], ["instance.id", 7357],
["factorio.enable_save_patching", true], ["factorio.enable_save_patching", true],