diff --git a/application/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java b/application/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java index acc253bf9..6024bb28c 100644 --- a/application/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java +++ b/application/src/main/java/run/halo/app/core/extension/reconciler/PluginReconciler.java @@ -1,5 +1,6 @@ package run.halo.app.core.extension.reconciler; +import static org.pf4j.util.FileUtils.isJarFile; import static run.halo.app.plugin.PluginConst.DELETE_STAGE; import java.io.IOException; @@ -22,6 +23,7 @@ import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.BooleanUtils; import org.apache.commons.lang3.StringUtils; import org.pf4j.PluginDescriptor; +import org.pf4j.PluginRuntimeException; import org.pf4j.PluginState; import org.pf4j.PluginWrapper; import org.pf4j.RuntimeMode; @@ -277,6 +279,15 @@ public class PluginReconciler implements Reconciler { throw e; } } + + if (currentState != desiredState) { + log.error("Plugin [{}] state transition failed: {}", name, + haloPluginManager.getPluginStartingError(name)); + var e = new PluginRuntimeException("Plugin [" + name + "] state transition from [" + + currentState + "] to [" + desiredState + "] failed"); + persistenceFailureStatus(name, e); + throw e; + } } void persistenceFailureStatus(String pluginName, Throwable e) { @@ -554,25 +565,41 @@ public class PluginReconciler implements Reconciler { } PluginWrapper pluginWrapper = haloPluginManager.getPlugin(name); - if (pluginWrapper == null) { - return; - } - // pluginWrapper must not be null in below code - // stop and unload plugin, see also PluginBeforeStopSyncListener - if (!haloPluginManager.unloadPlugin(name)) { - throw new IllegalStateException("Failed to unload plugin: " + name); + if (pluginWrapper != null) { + // pluginWrapper must not be null in below code + // stop and unload plugin, see also PluginBeforeStopSyncListener + if (!haloPluginManager.unloadPlugin(name)) { + throw new IllegalStateException("Failed to unload plugin: " + name); + } } + // delete plugin resources - if (RuntimeMode.DEPLOYMENT.equals(pluginWrapper.getRuntimeMode())) { + Path pluginPath = determinePluginLocation(plugin); + if (pluginPath != null && !isDevelopmentMode(name) && isJarFile(pluginPath)) { // delete plugin file try { - Files.deleteIfExists(pluginWrapper.getPluginPath()); + Files.deleteIfExists(pluginPath); } catch (IOException e) { throw new RuntimeException(e); } } } + @Nullable + Path determinePluginLocation(Plugin plugin) { + final var name = plugin.getMetadata().getName(); + PluginWrapper pluginWrapper = haloPluginManager.getPlugin(name); + return Optional.ofNullable(pluginWrapper) + .map(PluginWrapper::getPluginPath) + .orElseGet(() -> { + var localtionUri = plugin.statusNonNull().getLoadLocation(); + if (localtionUri != null) { + return Path.of(localtionUri); + } + return null; + }); + } + void createInitialReverseProxyIfNotPresent(Plugin plugin) { String pluginName = plugin.getMetadata().getName(); String reverseProxyName = initialReverseProxyName(pluginName);