From 34da073800025fd7cf03778fb4b20b1cc0b765ac Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 28 Oct 2024 19:22:54 +0100 Subject: [PATCH 1/2] Fix setting instance config fields not being saved If only the config of an instance is updated the dirty flag of the instance is not set, which results in changes being discarded when the controller shuts down afterwards. Fix by always triggering the update logic when an instance field is changed. --- packages/controller/src/Controller.ts | 4 +--- test/controller/Controller.js | 12 +++++++++++- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/packages/controller/src/Controller.ts b/packages/controller/src/Controller.ts index 22b54567..405df8b7 100644 --- a/packages/controller/src/Controller.ts +++ b/packages/controller/src/Controller.ts @@ -924,9 +924,7 @@ export default class Controller { addInstanceHooks(instance: InstanceInfo) { instance.config.on("fieldChanged", (field: string, curr: any, prev: any) => { instance.updatedAtMs = Date.now(); - if (field === "instance.name") { - this.instances.set(instance); // Trigger updated logic - } + this.instances.set(instance); // Trigger update logic lib.invokeHook(this.plugins, "onInstanceConfigFieldChanged", instance, field, curr, prev); }); diff --git a/test/controller/Controller.js b/test/controller/Controller.js index 4ff84f43..44ce9b11 100644 --- a/test/controller/Controller.js +++ b/test/controller/Controller.js @@ -17,7 +17,9 @@ class MockInstanceConfig extends EventEmitter { } set(field, value) { - return this._data.set(field, value); + const prev = this.get(field); + this._data.set(field, value); + this.emit("fieldChanged", field, value, prev); } } @@ -179,5 +181,13 @@ describe("controller/src/Controller", function() { ); }); }); + + describe(".instances", function() { + it("should set the dirty flag if the config is updated", function() { + controller.instances.dirty = false; + mockInstanceConfig.set("instance.assigned_host", null); + assert(controller.instances.dirty === true, "dirty flag was not set"); + }); + }); }); }); From 6f85ec9dd6d3535764559fdc1ffdab6bc6b58569 Mon Sep 17 00:00:00 2001 From: Hornwitser Date: Mon, 28 Oct 2024 19:24:31 +0100 Subject: [PATCH 2/2] Log files written to aid debugging Log to the verbose level when a datastore file read or written to in order to make it easier to diagnose whether the files are written or not. --- packages/lib/src/datastore.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/packages/lib/src/datastore.ts b/packages/lib/src/datastore.ts index 20c38683..4da49273 100644 --- a/packages/lib/src/datastore.ts +++ b/packages/lib/src/datastore.ts @@ -2,6 +2,7 @@ import fs from "fs-extra"; import { safeOutputFile } from "./file_ops"; import { SubscribableValue } from "./subscriptions"; +import { logger } from "./logging"; import { EventEmitter } from "stream"; import { ControllerConfig } from "./config"; import path from "path"; @@ -60,6 +61,7 @@ export class JsonDatastoreProvider< // Save the data to the json file async save(data: Map) { + logger.verbose(`Saving ${this.filePath}`); await safeOutputFile(this.filePath, JSON.stringify(Object.fromEntries(data), null, "\t")); } @@ -68,6 +70,7 @@ export class JsonDatastoreProvider< // Read the raw json from the file let rawJson; try { + logger.verbose(`Loading ${this.filePath}`); rawJson = JSON.parse(await fs.readFile(this.filePath, { encoding: "utf8" })); } catch (err: any) { if (err.code !== "ENOENT") { @@ -101,6 +104,7 @@ export class JsonIdDatastoreProvider< // Save the data to the json file async save(data: Map) { + logger.verbose(`Saving ${this.filePath}`); await safeOutputFile(this.filePath, JSON.stringify([...data.values()], null, "\t")); } @@ -109,6 +113,7 @@ export class JsonIdDatastoreProvider< // Read the raw json from the file let rawJson; try { + logger.verbose(`Loading ${this.filePath}`); rawJson = JSON.parse(await fs.readFile(this.filePath, { encoding: "utf8" })); } catch (err: any) { if (err.code !== "ENOENT") {