Skip to content

Commit

Permalink
Remove request-to-reload annotation manually
Browse files Browse the repository at this point in the history
Signed-off-by: JohnNiang <[email protected]>
  • Loading branch information
JohnNiang authored and halo-dev-bot committed May 10, 2024
1 parent c9abb71 commit a43c780
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,17 @@ private void syncPluginState(Plugin plugin) {

private static boolean requestToReload(Plugin plugin) {
var annotations = plugin.getMetadata().getAnnotations();
return annotations != null && annotations.remove(RELOAD_ANNO) != null;
return annotations != null && annotations.get(RELOAD_ANNO) != null;
}

private static void removeRequestToReload(Plugin plugin) {
var annotations = plugin.getMetadata().getAnnotations();
if (annotations != null) {
annotations.remove(RELOAD_ANNO);
}
}


private void cleanupResources(Plugin plugin) {
var pluginName = plugin.getMetadata().getName();
var reverseProxyName = buildReverseProxyName(pluginName);
Expand Down Expand Up @@ -394,6 +402,8 @@ private void loadOrReload(Plugin plugin) {
}
p = pluginManager.getPlugin(pluginName);
}
// ensure removing the reload annotation after the plugin is reloaded
removeRequestToReload(plugin);
}
if (p != null && pluginManager.getUnresolvedPlugins().contains(p)) {
pluginManager.unloadPlugin(pluginName);
Expand Down Expand Up @@ -586,6 +596,7 @@ public void pluginStateChanged(PluginStateEvent event) {
client.fetch(Plugin.class, pluginId)
.ifPresent(plugin -> {
if (!Objects.equals(true, plugin.getSpec().getEnabled())) {
log.info("Observed plugin {} started, enabling it.", pluginId);
plugin.getSpec().setEnabled(true);
client.update(plugin);
}
Expand All @@ -604,6 +615,7 @@ public void pluginStateChanged(PluginStateEvent event) {
.ifPresent(plugin -> {
if (!requestToReload(plugin)
&& Objects.equals(true, plugin.getSpec().getEnabled())) {
log.info("Observed plugin {} stopped, disabling it.", pluginId);
plugin.getSpec().setEnabled(false);
client.update(plugin);
}
Expand Down
22 changes: 21 additions & 1 deletion application/src/main/java/run/halo/app/plugin/SpringPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,36 @@ public SpringPlugin(PluginApplicationContextFactory contextFactory,

@Override
public void start() {
log.info("Preparing starting plugin {}", pluginContext.getName());
var pluginId = pluginContext.getName();
try {
// initialize context
var pluginId = pluginContext.getName();
this.context = contextFactory.create(pluginId);
log.info("Application context {} for plugin {} is created", this.context, pluginId);

var pluginOpt = context.getBeanProvider(Plugin.class)
.stream()
.findFirst();
log.info("Before publishing plugin starting event for plugin {}", pluginId);
context.publishEvent(new SpringPluginStartingEvent(this, this));
log.info("After publishing plugin starting event for plugin {}", pluginId);
if (pluginOpt.isPresent()) {
this.delegate = pluginOpt.get();
if (this.delegate instanceof BasePlugin basePlugin) {
basePlugin.setContext(pluginContext);
}
log.info("Starting {} for plugin {}", this.delegate, pluginId);
this.delegate.start();
log.info("Started {} for plugin {}", this.delegate, pluginId);
}
log.info("Before publishing plugin started event for plugin {}", pluginId);
context.publishEvent(new SpringPluginStartedEvent(this, this));
log.info("After publishing plugin started event for plugin {}", pluginId);
} catch (Throwable t) {
// try to stop plugin for cleaning resources if something went wrong
log.error(
"Cleaning up plugin resources for plugin {} due to not being able to start plugin.",
pluginId);
this.stop();
// propagate exception to invoker.
throw t;
Expand All @@ -54,16 +65,25 @@ public void start() {
public void stop() {
try {
if (context != null) {
log.info("Before publishing plugin stopping event for plugin {}",
pluginContext.getName());
context.publishEvent(new SpringPluginStoppingEvent(this, this));
log.info("After publishing plugin stopping event for plugin {}",
pluginContext.getName());
}
if (this.delegate != null) {
log.info("Stopping {} for plugin {}", this.delegate, pluginContext.getName());
this.delegate.stop();
log.info("Stopped {} for plugin {}", this.delegate, pluginContext.getName());
}
} finally {
if (context instanceof ConfigurableApplicationContext configurableContext) {
log.info("Closing plugin context for plugin {}", pluginContext.getName());
configurableContext.close();
log.info("Closed plugin context for plugin {}", pluginContext.getName());
}
// reset application context
log.info("Reset plugin context for plugin {}", pluginContext.getName());
context = null;
}
}
Expand Down

0 comments on commit a43c780

Please sign in to comment.