Provide controller resources at construction time

Load hosts, instances, mod packs, mods and users before constructing the
Controller instances.  This allows these resources to be correctly typed
as always available and prevents any possibility of save being initiated
on these resources before they are loaded.
This commit is contained in:
Hornwitser committed 2023-11-30 18:31:42 +01:00
1 parent a0b02ee68b
commit ab52cc35bf
11 files changed
+91 -77

No files matched your search

+48 -40
View File
@@ -48,17 +48,6 @@ export default class Controller {
config: lib.ControllerConfig;
app: Application;
/** Mapping of host id to host info */
hosts: Map<number, HostInfo> | null = null;
/** Mapping of instance id to instance info */
instances: Map<number, InstanceInfo> | null = null;
/** Mapping of mod pack id to mod pack */
modPacks: Map<number, lib.ModPack> | null = null;
/** Mapping of mod names to mod infos */
mods: Map<string, lib.ModInfo> | null = null;
/** User and roles manager for the cluster */
userManager: UserManager;
/** True if {@link Controller.hosts} has changed since last time it was saved */
hostsDirty = false;
/** True if {@link Controller.instances} has changed since last time it was saved */
@@ -98,11 +87,45 @@ export default class Controller {
clusterLogIndex: lib.LogIndex | null = null;
clusterLogBuildInterval: ReturnType<typeof setInterval> | null = null;
static async bootstrap(config: lib.ControllerConfig) {
let databaseDirectory = config.get("controller.database_directory");
await fs.ensureDir(databaseDirectory);
const hosts = await Controller.loadHosts(path.join(databaseDirectory, "hosts.json"));
const instances = await Controller.loadInstances(path.join(databaseDirectory, "instances.json"));
const modPacks = await Controller.loadModPacks(path.join(databaseDirectory, "mod-packs.json"));
const userManager = new UserManager(config);
await userManager.load(path.join(databaseDirectory, "users.json"));
let modsDirectory = config.get("controller.mods_directory");
await fs.ensureDir(modsDirectory);
const mods = await Controller.loadModInfos(modsDirectory);
return [
hosts,
instances,
modPacks,
mods,
userManager,
] as const;
}
constructor(
clusterLogger: winston.Logger,
pluginInfos: any[],
configPath: string,
config: lib.ControllerConfig
config: lib.ControllerConfig,
/** Mapping of host id to host info */
public hosts = new Map<number, HostInfo>(),
/** Mapping of instance id to instance info */
public instances = new Map<number, InstanceInfo>(),
/** Mapping of mod pack id to mod pack */
public modPacks = new Map<number, lib.ModPack>(),
/** Mapping of mod names to mod infos */
public mods = new Map<string, lib.ModInfo>(),
/** User and roles manager for the cluster */
public userManager = new UserManager(config),
) {
this.clusterLogger = clusterLogger;
this.pluginInfos = pluginInfos;
@@ -114,7 +137,6 @@ export default class Controller {
this.app.locals.streams = new Map();
this.wsServer = new WsServer(this);
this.userManager = new UserManager(this.config);
this.subscriptions = new lib.SubscriptionController(this);
// Handle subscriptions for all internal properties
@@ -178,25 +200,13 @@ export default class Controller {
this._startDevServer(args);
}
let databaseDirectory = this.config.get("controller.database_directory");
await fs.ensureDir(databaseDirectory);
this.hosts = await Controller.loadHosts(path.join(databaseDirectory, "hosts.json"));
this.instances = await Controller.loadInstances(path.join(databaseDirectory, "instances.json"));
this.modPacks = await Controller.loadModPacks(path.join(databaseDirectory, "mod-packs.json"));
await this.userManager.load(path.join(databaseDirectory, "users.json"));
let modsDirectory = this.config.get("controller.mods_directory");
await fs.ensureDir(modsDirectory);
this.mods = await Controller.loadModInfos(modsDirectory);
this.config.on("fieldChanged", (group, field, prev) => {
if (group.name === "controller" && field === "autosave_interval") {
this.onAutosaveIntervalChanged();
}
lib.invokeHook(this.plugins, "onControllerConfigFieldChanged", group, field, prev);
});
for (let instance of this.instances!.values()) {
for (let instance of this.instances.values()) {
this.addInstanceHooks(instance);
}
@@ -346,9 +356,7 @@ export default class Controller {
await lib.invokeHook(this.plugins, "onShutdown");
if (this.wsServer) {
await this.wsServer.stop();
}
await this.wsServer.stop();
let stopTasks = [];
logger.info("Stopping HTTP(S) server");
@@ -401,22 +409,22 @@ export default class Controller {
}
let databaseDirectory = this.config.get("controller.database_directory");
if (this.hosts && this.hostsDirty) {
if (this.hostsDirty) {
this.hostsDirty = false;
await Controller.saveHosts(path.join(databaseDirectory, "hosts.json"), this.hosts);
}
if (this.instances && this.instancesDirty) {
if (this.instancesDirty) {
this.instancesDirty = false;
await Controller.saveInstances(path.join(databaseDirectory, "instances.json"), this.instances);
}
if (this.modPacks && this.modPacksDirty) {
if (this.modPacksDirty) {
this.modPacksDirty = false;
await Controller.saveModPacks(path.join(databaseDirectory, "mod-packs.json"), this.modPacks);
}
if (this.userManager && this.userManager.dirty) {
if (this.userManager.dirty) {
await this.userManager.save(path.join(databaseDirectory, "users.json"));
}
@@ -548,14 +556,14 @@ export default class Controller {
async deleteMod(name: string, version: string): Promise<void> {
let filename = `${name}_${version}.zip`;
let mod = this.mods!.get(filename);
let mod = this.mods.get(filename);
if (!mod) {
throw new Error(`Mod ${filename} does not exist`);
}
let modsDirectory = this.config.get("controller.mods_directory");
await fs.unlink(path.join(modsDirectory, filename));
this.mods!.delete(filename);
this.mods.delete(filename);
mod.isDeleted = true;
this.modUpdated(mod);
}
@@ -662,7 +670,7 @@ export default class Controller {
* @throws {module:lib.RequestError} if the instance does not exist.
*/
getRequestInstance(instanceId:number): InstanceInfo {
let instance = this.instances!.get(instanceId);
let instance = this.instances.get(instanceId);
if (!instance) {
throw new lib.RequestError(`Instance with ID ${instanceId} does not exist`);
}
@@ -688,7 +696,7 @@ export default class Controller {
*/
async instanceCreate(instanceConfig: lib.InstanceConfig): Promise<InstanceInfo> {
let instanceId = instanceConfig.get("instance.id");
if (this.instances!.has(instanceId)) {
if (this.instances.has(instanceId)) {
throw new lib.RequestError(`Instance with ID ${instanceId} already exists`);
}
@@ -719,7 +727,7 @@ export default class Controller {
instanceConfig.set("factorio.settings", settings);
let instance = new InstanceInfo({ config: instanceConfig, status: "unassigned" });
this.instances!.set(instanceId, instance);
this.instances.set(instanceId, instance);
this.instancesDirty = true;
await lib.invokeHook(this.plugins, "onInstanceStatusChanged", instance);
this.addInstanceHooks(instance);
@@ -793,7 +801,7 @@ export default class Controller {
if (hostId !== null) {
await this.sendTo({ hostId }, new lib.InstanceDeleteInternalRequest(instanceId));
}
this.instances!.delete(instanceId);
this.instances.delete(instanceId);
this.instancesDirty = true;
let prev = instance.status;
@@ -1173,7 +1181,7 @@ export default class Controller {
connection = this.wsServer.controlConnections.get(dst.id);
} else if (dst.type === lib.Address.instance) {
let instance = this.instances!.get(dst.id);
let instance = this.instances.get(dst.id);
if (!instance) {
return;
}