forked from halo-dev/halo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support restarting Halo (halo-dev#4361)
#### What type of PR is this? /kind feature /area core /milestone 2.9.x #### What this PR does / why we need it: Support restarting Halo and enable restart endpoint by default. Restart endpoint detail: request uri: `/actuator/restart` request method: `POST` Please note that memory usage may slightly increase after restarting Halo. #### Does this PR introduce a user-facing change? ```release-note 支持在线重启 Halo。 ```
- Loading branch information
Showing
7 changed files
with
92 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
application/src/main/java/run/halo/app/actuator/RestartEndpoint.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package run.halo.app.actuator; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.Collections; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; | ||
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint; | ||
import org.springframework.boot.context.event.ApplicationStartedEvent; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationListener; | ||
import org.springframework.context.ConfigurableApplicationContext; | ||
import org.springframework.stereotype.Component; | ||
import run.halo.app.Application; | ||
|
||
@WebEndpoint(id = "restart") | ||
@Component | ||
@Slf4j | ||
public class RestartEndpoint implements ApplicationListener<ApplicationStartedEvent> { | ||
|
||
private SpringApplication application; | ||
|
||
private String[] args; | ||
|
||
private ConfigurableApplicationContext context; | ||
|
||
@WriteOperation | ||
public Object restart() { | ||
var threadGroup = new ThreadGroup("RestartGroup"); | ||
var thread = new Thread(threadGroup, this::doRestart, "restartMain"); | ||
thread.setDaemon(false); | ||
thread.setContextClassLoader(Application.class.getClassLoader()); | ||
thread.start(); | ||
return Collections.singletonMap("message", "Restarting"); | ||
} | ||
|
||
private synchronized void doRestart() { | ||
log.info("Restarting..."); | ||
if (this.context != null) { | ||
try { | ||
closeRecursively(this.context); | ||
var shutdownHandlers = SpringApplication.getShutdownHandlers(); | ||
if (shutdownHandlers instanceof Runnable runnable) { | ||
// clear closedContext in org.springframework.boot.SpringApplicationShutdownHook | ||
runnable.run(); | ||
} | ||
this.context = this.application.run(args); | ||
log.info("Restarted"); | ||
} catch (Throwable t) { | ||
log.error("Failed to restart.", t); | ||
} | ||
} | ||
} | ||
|
||
private static void closeRecursively(ApplicationContext ctx) { | ||
while (ctx != null) { | ||
if (ctx instanceof Closeable closeable) { | ||
try { | ||
closeable.close(); | ||
} catch (IOException e) { | ||
log.error("Cannot close context: {}", ctx.getId(), e); | ||
} | ||
} | ||
ctx = ctx.getParent(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onApplicationEvent(ApplicationStartedEvent event) { | ||
if (this.context == null) { | ||
this.application = event.getSpringApplication(); | ||
this.args = event.getArgs(); | ||
this.context = event.getApplicationContext(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters