From 05dfd19d8091bee028affa96d639d0e72060d7a9 Mon Sep 17 00:00:00 2001 From: "[object Object]" Date: Fri, 30 Dec 2022 13:17:04 -0800 Subject: [PATCH] Implement config handling --- res/lazymc.toml | 4 ++++ src/server.rs | 55 ++++++++++++++++++++++++------------------------- 2 files changed, 31 insertions(+), 28 deletions(-) diff --git a/res/lazymc.toml b/res/lazymc.toml index 38fe8e2..e1f35e8 100644 --- a/res/lazymc.toml +++ b/res/lazymc.toml @@ -33,6 +33,10 @@ directory = "." # Warning: if using a bash script read: https://git.io/JMIKH command = "java -Xmx1G -Xms1G -jar server.jar --nogui" +# Freeze the server process instead of restarting it when no players online, making it start up faster. +# Only works on Unix (Linux or MacOS) +#freeze_process = true + # Immediately wake server when starting lazymc. #wake_on_start = false diff --git a/src/server.rs b/src/server.rs index e717057..d46602f 100644 --- a/src/server.rs +++ b/src/server.rs @@ -217,9 +217,10 @@ impl Server { None => info!(target: "lazymc", "Starting server..."), } - // TODO add config option for this - if let Some(pid) = *server.pid.lock().await { - return os::unfreeze(pid); + if config.server.freeze_process { + if let Some(pid) = *server.pid.lock().await { + return os::unfreeze(pid); + } } // Spawn server in new task @@ -590,32 +591,30 @@ async fn stop_server_signal(config: &Config, server: &Server) -> bool { } }; - // Send kill signal - // TODO uncomment this and add a config option - /*if !crate::os::kill_gracefully(pid) { - error!(target: "lazymc", "Failed to send stop signal to server process"); - return false; - }*/ - if !os::freeze(pid) { - error!(target: "lazymc", "Failed to send freeze signal to server process."); + if config.server.freeze_process { + if !os::freeze(pid) { + error!(target: "lazymc", "Failed to send freeze signal to server process."); + } + + server + .update_state_from(Some(State::Starting), State::Stopped, config) + .await; + server + .update_state_from(Some(State::Started), State::Stopped, config) + .await; + } else { + if !crate::os::kill_gracefully(pid) { + error!(target: "lazymc", "Failed to send stop signal to server process"); + return false; + } + + server + .update_state_from(Some(State::Starting), State::Stopping, config) + .await; + server + .update_state_from(Some(State::Started), State::Stopping, config) + .await; } - // Update from starting/started to stopping - - /* TODO uncomment this and add a config option - server - .update_state_from(Some(State::Starting), State::Stopping, config) - .await; - server - .update_state_from(Some(State::Started), State::Stopping, config) - .await; - */ - server - .update_state_from(Some(State::Starting), State::Stopped, config) - .await; - server - .update_state_from(Some(State::Started), State::Stopped, config) - .await; - true }