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