diff --git a/packages/lib/plugin.js b/packages/lib/plugin.js index 4dd71d08..834d2c96 100644 --- a/packages/lib/plugin.js +++ b/packages/lib/plugin.js @@ -256,6 +256,7 @@ class BaseMasterPlugin { * or received notice from a slave that the status of an instance has * changed. The possible statuses that can be notified about are: * - `unassigned:`: Instance is no longer asssigned to a slave. + * - `unknown`: Slave assigned to instance is offline. * - `stopped`: Instance is no longer running or was just assigned to a * slave. * - `starting`: Instance is in the process of starting up. @@ -270,7 +271,8 @@ class BaseMasterPlugin { * they are assigned to a slave, when the slave then connects to the * master and informs of the current status of its instances this hook * is invoked for all those instances. You can detect this situation by - * checking if prev equals `unknown`. + * checking if prev equals `unknown`. If the slave disconnects the + * instances will again get the `unknown` status. * * When instances are created on the master they will notify of a status * change with prev set to null. While the status of new instances is diff --git a/packages/master/src/SlaveConnection.js b/packages/master/src/SlaveConnection.js index f4c71d1b..6c2319dd 100644 --- a/packages/master/src/SlaveConnection.js +++ b/packages/master/src/SlaveConnection.js @@ -39,6 +39,20 @@ class SlaveConnection extends BaseConnection { } }); } + + this.connector.on("close", () => { + // Update status to unknown for instances on this slave. + for (let instance of this._master.instances.values()) { + if (instance.config.get("instance.assigned_slave") !== this._id) { + continue; + } + + let prev = instance.status; + instance.status = "unknown"; + this._master.instanceUpdated(instance); + libPlugin.invokeHook(this._master.plugins, "onInstanceStatusChanged", instance, prev); + } + }); } /** diff --git a/test/integration/clusterio.js b/test/integration/clusterio.js index c5007d57..ea92fd4d 100644 --- a/test/integration/clusterio.js +++ b/test/integration/clusterio.js @@ -88,6 +88,9 @@ describe("Integration of Clusterio", function() { slaveProcess = await spawn( "alt-slave:", `node ../../packages/slave run --config ${config}`, /Started slave/ ); + // Add instance to test the unknown status afterwards + await execCtl("instance create alt-test --id 99"); + await execCtl("instance assign alt-test 5"); } finally { if (slaveProcess) { slaveProcess.kill("SIGINT"); @@ -524,7 +527,7 @@ describe("Integration of Clusterio", function() { describe("instanceUpdateEventHandler()", function() { it("should have triggered for the previous instance status updates", function() { let statusesToCheck = new Set([ - "unassigned", "stopped", "creating_save", "exporting_data", + "unassigned", "unknown", "stopped", "creating_save", "exporting_data", "starting", "running", "stopping", "deleted", ]); let statusesNotSeen = new Set(statusesToCheck);