mirror of https://github.com/halo-dev/halo
Cleanup resources when plugin startup fails (#5765)
Signed-off-by: JohnNiang <johnniang@foxmail.com>pull/5719/head
parent
b4b6693732
commit
b74f7c4463
|
@ -25,37 +25,47 @@ public class SpringPlugin extends Plugin {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void start() {
|
public void start() {
|
||||||
// initialize context
|
try {
|
||||||
var pluginId = pluginContext.getName();
|
// initialize context
|
||||||
this.context = contextFactory.create(pluginId);
|
var pluginId = pluginContext.getName();
|
||||||
|
this.context = contextFactory.create(pluginId);
|
||||||
|
|
||||||
var pluginOpt = context.getBeanProvider(Plugin.class)
|
var pluginOpt = context.getBeanProvider(Plugin.class)
|
||||||
.stream()
|
.stream()
|
||||||
.findFirst();
|
.findFirst();
|
||||||
context.publishEvent(new SpringPluginStartingEvent(this, this));
|
context.publishEvent(new SpringPluginStartingEvent(this, this));
|
||||||
if (pluginOpt.isPresent()) {
|
if (pluginOpt.isPresent()) {
|
||||||
this.delegate = pluginOpt.get();
|
this.delegate = pluginOpt.get();
|
||||||
if (this.delegate instanceof BasePlugin basePlugin) {
|
if (this.delegate instanceof BasePlugin basePlugin) {
|
||||||
basePlugin.setContext(pluginContext);
|
basePlugin.setContext(pluginContext);
|
||||||
|
}
|
||||||
|
this.delegate.start();
|
||||||
}
|
}
|
||||||
this.delegate.start();
|
context.publishEvent(new SpringPluginStartedEvent(this, this));
|
||||||
|
} catch (Throwable t) {
|
||||||
|
// try to stop plugin for cleaning resources if something went wrong
|
||||||
|
this.stop();
|
||||||
|
// propagate exception to invoker.
|
||||||
|
throw t;
|
||||||
}
|
}
|
||||||
context.publishEvent(new SpringPluginStartedEvent(this, this));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void stop() {
|
public void stop() {
|
||||||
if (context != null) {
|
try {
|
||||||
context.publishEvent(new SpringPluginStoppingEvent(this, this));
|
if (context != null) {
|
||||||
|
context.publishEvent(new SpringPluginStoppingEvent(this, this));
|
||||||
|
}
|
||||||
|
if (this.delegate != null) {
|
||||||
|
this.delegate.stop();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
if (context instanceof ConfigurableApplicationContext configurableContext) {
|
||||||
|
configurableContext.close();
|
||||||
|
}
|
||||||
|
// reset application context
|
||||||
|
context = null;
|
||||||
}
|
}
|
||||||
if (this.delegate != null) {
|
|
||||||
this.delegate.stop();
|
|
||||||
}
|
|
||||||
if (context instanceof ConfigurableApplicationContext configurableContext) {
|
|
||||||
configurableContext.close();
|
|
||||||
}
|
|
||||||
// reset application context
|
|
||||||
context = null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -63,6 +73,7 @@ public class SpringPlugin extends Plugin {
|
||||||
if (delegate != null) {
|
if (delegate != null) {
|
||||||
delegate.delete();
|
delegate.delete();
|
||||||
}
|
}
|
||||||
|
this.delegate = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ApplicationContext getApplicationContext() {
|
public ApplicationContext getApplicationContext() {
|
||||||
|
|
Loading…
Reference in New Issue