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,6 +25,7 @@ public class SpringPlugin extends Plugin {
|
|||
|
||||
@Override
|
||||
public void start() {
|
||||
try {
|
||||
// initialize context
|
||||
var pluginId = pluginContext.getName();
|
||||
this.context = contextFactory.create(pluginId);
|
||||
|
@ -41,28 +42,38 @@ public class SpringPlugin extends Plugin {
|
|||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void stop() {
|
||||
try {
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void delete() {
|
||||
if (delegate != null) {
|
||||
delegate.delete();
|
||||
}
|
||||
this.delegate = null;
|
||||
}
|
||||
|
||||
public ApplicationContext getApplicationContext() {
|
||||
|
|
Loading…
Reference in New Issue