diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index 41eda66e..da5f2369 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -2,6 +2,7 @@ import type winston from "winston"; import { BlockList, type AddressInfo, isIPv4, isIPv6 } from "net"; import type { ControllerArgs } from "../controller.js"; import type ControlConnection from "./ControlConnection.js"; +import type HostConnection from "./HostConnection.js"; import express, { type Request, type Response, type NextFunction, type Application } from "express"; import type { Static } from "@sinclair/typebox"; import finalhandler from "finalhandler"; @@ -508,11 +509,11 @@ export default class Controller { } this.hostPairing.start(); - this.onAutosaveIntervalChanged(); - this.onSystemMetricsIntervalChanged(); logger.info("Started controller"); this.startedAtMs = Date.now(); + this.onAutosaveIntervalChanged(); + this.onSystemMetricsIntervalChanged(); this._state = "running"; } @@ -669,6 +670,7 @@ export default class Controller { this.updateSystems.bind(this), systemMetricsIntervalSeconds * 1000 ); + this.updateSystems(); } } @@ -694,6 +696,20 @@ export default class Controller { } } + /** Fetch system info from a host that just connected instead of waiting for the next interval */ + async updateHostSystem(hostConnection: HostConnection) { + if (!(this.config.get("controller.system_metrics_interval") > 0)) { + return; + } + try { + this.systems.set(await hostConnection.send(new lib.SystemInfoRequest())); + } catch (err: any) { + logger.error( + `Unexpected error updating system info of host ${hostConnection.id}:\n${err.stack ?? err.message}` + ); + } + } + /** Plugins on the controller, including those installed or updated since it started */ async listPlugins() { return await lib.listPluginDetails( diff --git a/packages/controller/src/HostConnection.ts b/packages/controller/src/HostConnection.ts index 5df65ab3..03af5243 100644 --- a/packages/controller/src/HostConnection.ts +++ b/packages/controller/src/HostConnection.ts @@ -51,6 +51,7 @@ export default class HostConnection extends BaseConnection { for (let event of ["connect", "drop", "resume", "close"] as const) { this.connector.on(event, () => this._controller.hooks.hostConnectionEvent.invoke(this, event)); } + this.connector.on("connect", () => { this._controller.updateHostSystem(this); }); this.connector.on("close", () => { // Update status to unknown for instances on this host. diff --git a/test/controller/Controller.js b/test/controller/Controller.js index abaff944..1eaa26d9 100644 --- a/test/controller/Controller.js +++ b/test/controller/Controller.js @@ -634,6 +634,32 @@ describe("controller/src/Controller", function() { assert.equal(controller.config.restartRequired, true); }); }); + describe(".onSystemMetricsIntervalChanged()", function() { + let calls; + beforeEach(function() { + calls = 0; + controller.updateSystems = async () => { calls += 1; }; + }); + afterEach(function() { + clearInterval(controller.systemMetricsInterval); + controller.systemMetricsInterval = undefined; + delete controller.updateSystems; + controller.config.set("controller.system_metrics_interval", 10); + }); + it("updates systems right away and then on every interval", async function() { + controller.config.set("controller.system_metrics_interval", 0.01); + controller.onSystemMetricsIntervalChanged(); + assert.equal(calls, 1, "first update waited for the interval"); + await new Promise(resolve => setTimeout(resolve, 35)); + assert(calls >= 3, `expected interval updates, got ${calls} calls`); + }); + it("does nothing when the interval is 0", function() { + controller.config.set("controller.system_metrics_interval", 0); + controller.onSystemMetricsIntervalChanged(); + assert.equal(calls, 0); + assert.equal(controller.systemMetricsInterval, undefined); + }); + }); describe(".checkRestartDowngrade()", function() { it("returns null when the installed version matches", async function() { controller.config.set("controller.version", controllerVersion); diff --git a/test/controller/HostConnection.test.js b/test/controller/HostConnection.test.js index 123c9d3d..39afc0be 100644 --- a/test/controller/HostConnection.test.js +++ b/test/controller/HostConnection.test.js @@ -136,6 +136,46 @@ describe("controller/src/HostConnection", function() { }); }); + describe("system info on connect", function() { + let controller; + let connector; + let connection; + + beforeEach(function() { + controller = new Controller(lib.logger, [], new lib.ControllerConfig("controller")); + controller.hosts.records.set(new HostRecord(10, "host-10", "1.0.0", new Map())); + connector = new MockConnector(addr("controller"), addr({ hostId: 10 })); + connection = new HostConnection( + new lib.RegisterHostData("", "2.0.0", 10, {}), connector, controller, "10.0.0.1" + ); + }); + + function systemInfoRequests() { + return connector.sentMessages.filter(message => message.name === "SystemInfoRequest"); + } + + it("should request system info as soon as the host connects", async function() { + connector.emit("connect"); + const [request] = systemInfoRequests(); + assert(request, "SystemInfoRequest was not sent"); + assert.equal(connection.id, 10); + + const info = await lib.gatherSystemInfo(10, false, true, 0); + connector.emit("message", new lib.MessageResponse(request.seq, request.dst, request.src, info)); + await new Promise(resolve => setImmediate(resolve)); + assert.equal(controller.systems.get(10)?.restartRequired, true); + }); + it("should not request system info when a session resumes", function() { + connector.emit("resume"); + assert.deepEqual(systemInfoRequests(), []); + }); + it("should not request system info when system metrics are disabled", function() { + controller.config.set("controller.system_metrics_interval", 0); + connector.emit("connect"); + assert.deepEqual(systemInfoRequests(), []); + }); + }); + describe(".handleInstanceSaveDetailsUpdatesEvent()", function() { // Instance 1 is on host 10, instance 2 is on host 20. const assignments = new Map([[1, 10], [2, 20]]);