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
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
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.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({
instance_id: this.config.get("instance.id"),
instance_name: this.config.get("instance.name"),
instance_id: this.id,
instance_name: this.name,
});
this._configFieldChanged = (group, field, prev) => {
@@ -168,7 +174,7 @@ class Instance extends libLink.Link {
);
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);
});
@@ -185,7 +191,7 @@ class Instance extends libLink.Link {
this.server.on("ipc-player_event", event => {
libLink.messages.playerEvent.send(this, {
instance_id: this.config.get("instance.id"),
instance_id: this.id,
...event,
});
libPlugin.invokeHook(this.plugins, "onPlayerEvent", event);
@@ -193,7 +199,7 @@ class Instance extends libLink.Link {
}
async sendRcon(message, expectEmpty, plugin = "") {
let instanceId = String(this.config.get("instance.id"));
let instanceId = String(this.id);
let observeDuration = instanceRconCommandDuration.labels(instanceId).startTimer();
try {
return await this.server.sendRcon(message, expectEmpty);
@@ -205,13 +211,13 @@ class Instance extends libLink.Link {
async _autosave(name) {
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) {
this._status = status;
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.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) {
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
*/
async updateInstanceData() {
let name = libLuaTools.escapeString(this.config.get("instance.name"));
let id = this.config.get("instance.id");
await this.sendRcon(`/sc clusterio_private.update_instance(${id}, "${name}")`, true);
let name = libLuaTools.escapeString(this.name);
await this.sendRcon(`/sc clusterio_private.update_instance(${this.id}, "${name}")`, true);
}
async updateFactorioSettings(current, previous) {
@@ -648,8 +653,8 @@ class Instance extends libLink.Link {
let pid = this.server.pid;
if (pid) {
let stats = await pidusage(pid);
instanceFactorioCpuTime.labels(String(this.config.get("instance.id"))).set(stats.ctime / 1000);
instanceFactorioMemoryUsage.labels(String(this.config.get("instance.id"))).set(stats.memory);
instanceFactorioCpuTime.labels(String(this.id)).set(stats.ctime / 1000);
instanceFactorioMemoryUsage.labels(String(this.id)).set(stats.memory);
}
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}"`);
}
let instanceId = this.instance.config.get("instance.id");
let instanceId = this.instance.id;
instanceGameTicksTotal.labels(String(instanceId)).set(stats.game_tick);
instancePlayerCount.labels(String(instanceId)).set(stats.player_count);
+2 -2
View File
@@ -39,7 +39,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
async provideItems(items) {
this.info.messages.place.send(this.instance, {
items,
instance_id: this.instance.config.get("instance.id"),
instance_id: this.instance.id,
});
if (this.instance.config.get("subspace_storage.log_item_transfers")) {
@@ -52,7 +52,7 @@ class InstancePlugin extends libPlugin.BaseInstancePlugin {
async requestItems(items) {
// Request the items all at once
let response = await this.info.messages.remove.send(this.instance, {
instance_id: this.instance.config.get("instance.id"),
instance_id: this.instance.id,
items,
});
+1
View File
@@ -103,6 +103,7 @@ class MockInstance extends libLink.Link {
this.logger = new MockLogger();
this.server = new MockServer();
this.name = "test";
this.id = 7357;
this.mockConfigEntries = new Map([
["instance.id", 7357],
["factorio.enable_save_patching", true],