Implement minimum_online_time option

This commit is contained in:
timvisee 2021-11-08 18:07:32 +01:00
parent e115bba827
commit 9874c9dd30
No known key found for this signature in database
GPG Key ID: B8DB720BC383E172
3 changed files with 32 additions and 8 deletions

View File

@ -112,7 +112,7 @@ pub struct Time {
/// Minimum time in seconds to stay online when server is started.
// TODO: implement this
#[serde(alias = "minimum_online_time")]
#[serde(default, alias = "minimum_online_time")]
pub min_online_time: u32,
}

View File

@ -32,7 +32,7 @@ pub async fn monitor_server(config: Arc<Config>, state: Arc<ServerState>) {
// Poll server state and update internal status
trace!(target: "lazymc::monitor", "Fetching status for {} ... ", addr);
let status = poll_server(addr).await;
state.update_status(status);
state.update_status(&config, status);
// Sleep server when it's bedtime
if state.should_sleep(&config) {

View File

@ -34,6 +34,9 @@ pub struct ServerState {
///
/// The last known time when the server was active with online players.
last_active: Mutex<Option<Instant>>,
/// Keep server online until.
keep_online_until: Mutex<Option<Instant>>,
}
impl ServerState {
@ -65,6 +68,7 @@ impl ServerState {
// TODO: should we set this?
self.set_online(false);
self.set_keep_online_until(None);
return true;
}
@ -90,9 +94,21 @@ impl ServerState {
self.status.lock().unwrap().replace(status);
}
/// Update the last active time.
pub fn update_last_active_time(&self) {
self.last_active.lock().unwrap().replace(Instant::now());
}
/// Update the last active time.
pub fn set_keep_online_until(&self, duration: Option<u32>) {
*self.keep_online_until.lock().unwrap() = duration
.filter(|d| *d > 0)
.map(|d| Instant::now() + Duration::from_secs(d as u64));
}
/// Update the server status, online state and last active time.
// TODO: clean this up
pub fn update_status(&self, status: Option<ServerStatus>) {
pub fn update_status(&self, config: &Config, status: Option<ServerStatus>) {
let stopping = self.stopping.load(Ordering::Relaxed);
let was_online = self.online();
let online = status.is_some() && !stopping;
@ -103,6 +119,7 @@ impl ServerState {
// TODO: move this somewhere else
info!(target: "lazymc::monitor", "Server is now online");
self.update_last_active_time();
self.set_keep_online_until(Some(config.time.min_online_time));
}
// // If server just went offline, reset stopping state
@ -122,17 +139,24 @@ impl ServerState {
}
}
/// Update the last active time.
pub fn update_last_active_time(&self) {
self.last_active.lock().unwrap().replace(Instant::now());
}
/// Check whether the server should now sleep.
pub fn should_sleep(&self, config: &Config) -> bool {
// TODO: when initating server start, set last active time!
// TODO: do not initiate sleep when starting?
// TODO: do not initiate sleep when already initiated (with timeout)
// Don't sleep when keep online until isn't expired
let keep_online = self
.keep_online_until
.lock()
.unwrap()
.map(|i| i >= Instant::now())
.unwrap_or(false);
if keep_online {
info!("Not sleeping because of keep online");
return false;
}
// Server must be online, and must not be starting
if !self.online() || !self.starting() {
return false;