Fix onExit not being invoked if plugin load fails

Fix instance not properly exiting if an error occurs during the loading
of a plugin.
This commit is contained in:
Hornwitser committed 2020-05-08 18:15:17 +02:00
1 parent 1beffb1bfd
commit b8b8c44083
2 files changed
+22 -10

No files matched your search

+5 -1
View File
@@ -73,7 +73,11 @@ class BaseInstancePlugin {
async onStop() { }
/**
* Called when the Factorio server exited
* Called when the instance exits
*
* Invoked when the instance is shut down. This may occur after init
* has been called if an error occurs during startup. Note that if
* the plugin's init() throws this method will still be invoked.
*/
onExit() { }
+17 -9
View File
@@ -129,6 +129,17 @@ class Instance extends link.Link{
}
}
async _loadPlugin(pluginInfo, slave) {
let pluginLoadStarted = Date.now();
let { InstancePlugin } = require(`./plugins/${pluginInfo.name}/${pluginInfo.instanceEntrypoint}`);
let instancePlugin = new InstancePlugin(pluginInfo, this, slave);
this.plugins.set(pluginInfo.name, instancePlugin);
await instancePlugin.init();
plugin.attachPluginMessages(this, pluginInfo, instancePlugin);
console.log(`Clusterio | Loaded plugin ${pluginInfo.name} in ${Date.now() - pluginLoadStarted}ms`);
}
async init(pluginInfos, masterPlugins, slave) {
await this.server.init();
@@ -142,15 +153,12 @@ class Instance extends link.Link{
continue;
}
// require plugin class and initialize it
let pluginLoadStarted = Date.now();
let { InstancePlugin } = require(`./plugins/${pluginInfo.name}/${pluginInfo.instanceEntrypoint}`);
let instancePlugin = new InstancePlugin(pluginInfo, this, slave);
await instancePlugin.init();
this.plugins.set(pluginInfo.name, instancePlugin);
plugin.attachPluginMessages(this, pluginInfo, instancePlugin);
console.log(`Clusterio | Loaded plugin ${pluginInfo.name} in ${Date.now() - pluginLoadStarted}ms`);
try {
await this._loadPlugin(pluginInfo, slave);
} catch (err) {
this.notifyExit();
throw err;
}
}
}