mirror of
https://github.com/clusterio/clusterio.git
synced 2026-11-23 00:13:13 +09:00
Load all plugin types through one short lived PluginLoader
PluginLoader.load takes the plugin type, context, export name and base class as options and is used by the controller, host, instance, ctl, web interface and migrations. Module imports go through #plugin_module so the node variant stays out of the web bundle. loadPlugin.ts and the loading half of plugin_loader.ts are merged into it. The loader lives for one load. Plugins reach it through context.plugins, with waitFor(name, timeoutMs) and checkInstalled(name), replacing the waitForPlugin methods. Waiting on a plugin that is not in the load list or failed to load settles right away, as loading can't finish while the waiting entrypoint is still running. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
7f688d2ed0
commit
c1ea4d470b
20 files changed
+500
-522
No files matched your search
@@ -331,7 +331,6 @@ export default class Host extends lib.Link {
|
||||
hooks = new HostHooks(logger);
|
||||
/** Plugins which are currently loaded */
|
||||
loadedPlugins: Set<lib.PluginNodeEnvInfo> = new Set();
|
||||
private _pluginLoader = new lib.PluginLoader(logger);
|
||||
|
||||
/** A map from instance id to instance connection. Only present when instance is running. */
|
||||
instanceConnections = new Map<number, InstanceConnection>();
|
||||
@@ -488,20 +487,6 @@ export default class Host extends lib.Link {
|
||||
this.handle(lib.InstanceDeleteInternalRequest, this.handleInstanceDeleteInternalRequest.bind(this));
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for another host plugin to load
|
||||
*
|
||||
* @param waiter - Name of the plugin waiting, used in warnings.
|
||||
* @param name - Name of the plugin to wait for.
|
||||
* @param timeoutMs - Time to wait before giving up.
|
||||
* @returns
|
||||
* What the plugin's host entrypoint returned, or undefined with
|
||||
* a warning logged if it did not load.
|
||||
*/
|
||||
waitForPlugin<T = unknown>(waiter: string, name: string, timeoutMs?: number) {
|
||||
return this._pluginLoader.waitFor<T>(waiter, name, timeoutMs);
|
||||
}
|
||||
|
||||
async loadPlugins() {
|
||||
const toLoad: lib.PluginNodeEnvInfo[] = [];
|
||||
for (let pluginInfo of this.pluginInfos) {
|
||||
@@ -520,29 +505,16 @@ export default class Host extends lib.Link {
|
||||
toLoad.push(pluginInfo);
|
||||
}
|
||||
|
||||
const context = { logger, host: this };
|
||||
await this._pluginLoader.load(toLoad, async pluginInfo => {
|
||||
let api;
|
||||
try {
|
||||
api = await lib.loadPlugin(
|
||||
pluginInfo,
|
||||
"host",
|
||||
context,
|
||||
"HostPlugin",
|
||||
BaseHostPlugin,
|
||||
);
|
||||
} catch (err: any) {
|
||||
if (err.code === "InstallationError") {
|
||||
throw err;
|
||||
}
|
||||
throw new lib.PluginError(pluginInfo.name, err);
|
||||
}
|
||||
|
||||
logger.info(`Loaded plugin ${pluginInfo.name}`);
|
||||
return api;
|
||||
const { loaded } = await lib.PluginLoader.load({
|
||||
type: "host",
|
||||
context: { host: this },
|
||||
logger,
|
||||
installed: this.pluginInfos,
|
||||
load: toLoad,
|
||||
exportName: "HostPlugin",
|
||||
baseClass: BaseHostPlugin,
|
||||
});
|
||||
|
||||
for (const pluginInfo of toLoad) {
|
||||
for (const pluginInfo of loaded.keys()) {
|
||||
this.loadedPlugins.add(pluginInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -181,7 +181,6 @@ export default class Instance extends lib.Link {
|
||||
hooks: InstanceHooks;
|
||||
/** Plugins which are currently loaded */
|
||||
loadedPlugins: Set<lib.PluginNodeEnvInfo> = new Set();
|
||||
private _pluginLoader: lib.PluginLoader;
|
||||
|
||||
|
||||
_host: Host;
|
||||
@@ -212,7 +211,6 @@ export default class Instance extends lib.Link {
|
||||
this.id = this.config.get("instance.id");
|
||||
|
||||
this.logger = instanceLogger(instanceConfig);
|
||||
this._pluginLoader = new lib.PluginLoader(this.logger);
|
||||
|
||||
this.hooks = new InstanceHooks(this.logger);
|
||||
|
||||
@@ -768,20 +766,6 @@ end`.replace(/\r?\n/g, " ");
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Wait for another instance plugin to load
|
||||
*
|
||||
* @param waiter - Name of the plugin waiting, used in warnings.
|
||||
* @param name - Name of the plugin to wait for.
|
||||
* @param timeoutMs - Time to wait before giving up.
|
||||
* @returns
|
||||
* What the plugin's instance entrypoint returned, or undefined with
|
||||
* a warning logged if it did not load.
|
||||
*/
|
||||
waitForPlugin<T = unknown>(waiter: string, name: string, timeoutMs?: number) {
|
||||
return this._pluginLoader.waitFor<T>(waiter, name, timeoutMs);
|
||||
}
|
||||
|
||||
/**
|
||||
* Load the instance plugins and report the instance as initialised
|
||||
*
|
||||
@@ -806,19 +790,17 @@ end`.replace(/\r?\n/g, " ");
|
||||
toLoad.push(pluginInfo);
|
||||
}
|
||||
|
||||
let loaded;
|
||||
try {
|
||||
await this._pluginLoader.load(toLoad, async pluginInfo => {
|
||||
let pluginLoadStartedMs = Date.now();
|
||||
const api = await lib.loadPlugin(
|
||||
pluginInfo,
|
||||
"instance",
|
||||
{ logger: this.logger, host: this._host, instance: this },
|
||||
"InstancePlugin",
|
||||
BaseInstancePlugin,
|
||||
);
|
||||
this.logger.info(`Loaded plugin ${pluginInfo.name} in ${Date.now() - pluginLoadStartedMs}ms`);
|
||||
return api;
|
||||
});
|
||||
({ loaded } = await lib.PluginLoader.load({
|
||||
type: "instance",
|
||||
context: { host: this._host, instance: this },
|
||||
logger: this.logger,
|
||||
installed: pluginInfos,
|
||||
load: toLoad,
|
||||
exportName: "InstancePlugin",
|
||||
baseClass: BaseInstancePlugin,
|
||||
}));
|
||||
} catch (err) {
|
||||
this.notifyExit();
|
||||
await this.sendSaveListUpdate();
|
||||
@@ -826,7 +808,7 @@ end`.replace(/\r?\n/g, " ");
|
||||
}
|
||||
|
||||
const plugins: Record<string, string> = {};
|
||||
for (const pluginInfo of toLoad) {
|
||||
for (const pluginInfo of loaded.keys()) {
|
||||
this.loadedPlugins.add(pluginInfo);
|
||||
plugins[pluginInfo.name] = pluginInfo.version;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user