Add option to restart after a crash

This commit is contained in:
timvisee 2021-11-11 00:11:17 +01:00
parent 69b964b603
commit 7b4a90bb34
No known key found for this signature in database
GPG Key ID: B8DB720BC383E172
5 changed files with 20 additions and 4 deletions

View File

@ -36,6 +36,9 @@ command = "java -Xmx1G -Xms1G -jar server.jar --nogui"
# Immediately wake server when starting lazymc. # Immediately wake server when starting lazymc.
#wake_on_start = false #wake_on_start = false
# Immediately wake server after crash.
#wake_on_crash = false
[time] [time]
# Sleep after number of seconds. # Sleep after number of seconds.
#sleep_after = 60 #sleep_after = 60

View File

@ -132,6 +132,10 @@ pub struct Server {
/// Immediately wake server when starting lazymc. /// Immediately wake server when starting lazymc.
#[serde(default)] #[serde(default)]
pub wake_on_start: bool, pub wake_on_start: bool,
/// Immediately wake server after crash.
#[serde(default)]
pub wake_on_crash: bool,
} }
/// Time configuration. /// Time configuration.

View File

@ -62,7 +62,7 @@ pub async fn monitor_server(config: Arc<Config>, server: Arc<Server>) {
// Check whether we should force kill server // Check whether we should force kill server
if server.should_kill() { if server.should_kill() {
error!(target: "lazymc::montior", "Force killing server, took too long to start/stop"); error!(target: "lazymc::montior", "Force killing server, took too long to start or stop");
if !server.force_kill().await { if !server.force_kill().await {
warn!(target: "lazymc", "Failed to force kill server"); warn!(target: "lazymc", "Failed to force kill server");
} }

View File

@ -28,7 +28,7 @@ pub fn force_kill(pid: u32) -> bool {
/// # Panics /// # Panics
/// ///
/// Panics on platforms other than Unix. /// Panics on platforms other than Unix.
#[allow(unreachable_code)] #[allow(unreachable_code, dead_code, unused_variables)]
pub fn kill_gracefully(pid: u32) -> bool { pub fn kill_gracefully(pid: u32) -> bool {
#[cfg(unix)] #[cfg(unix)]
unsafe { unsafe {

View File

@ -348,23 +348,32 @@ pub async fn invoke_server_cmd(
.replace(child.id().expect("unknown server PID")); .replace(child.id().expect("unknown server PID"));
// Wait for process to exit, handle status // Wait for process to exit, handle status
match child.wait().await { let crashed = match child.wait().await {
Ok(status) if status.success() => { Ok(status) if status.success() => {
debug!(target: "lazymc", "Server process stopped successfully ({})", status); debug!(target: "lazymc", "Server process stopped successfully ({})", status);
false
} }
Ok(status) => { Ok(status) => {
warn!(target: "lazymc", "Server process stopped with error code ({})", status); warn!(target: "lazymc", "Server process stopped with error code ({})", status);
state.state() == State::Started
} }
Err(err) => { Err(err) => {
error!(target: "lazymc", "Failed to wait for server process to quit: {}", err); error!(target: "lazymc", "Failed to wait for server process to quit: {}", err);
error!(target: "lazymc", "Assuming server quit, cleaning up..."); error!(target: "lazymc", "Assuming server quit, cleaning up...");
false
} }
} };
// Set state to stopped, update server PID // Set state to stopped, update server PID
state.pid.lock().unwrap().take(); state.pid.lock().unwrap().take();
state.update_state(State::Stopped, &config); state.update_state(State::Stopped, &config);
// Restart on crash
if crashed && config.server.wake_on_crash {
warn!(target: "lazymc", "Server crashed, restarting...");
Server::start(config, state, None);
}
Ok(()) Ok(())
} }