add maximum time after schedule to shut down regardless of player count

This commit is contained in:
Robin Appelman 2024-06-13 23:00:05 +02:00
commit 0319d8eefa
2 changed files with 42 additions and 22 deletions

View file

@ -184,4 +184,11 @@ pub struct DynDnsConfig {
pub struct ScheduleConfig { pub struct ScheduleConfig {
pub start: String, pub start: String,
pub stop: String, pub stop: String,
#[serde(default = "default_stop_grace_time")]
pub stop_grace_time: u64,
}
/// 1h
fn default_stop_grace_time() -> u64 {
60 * 60
} }

View file

@ -13,7 +13,7 @@ use ssh::SshSession;
use std::net::IpAddr; use std::net::IpAddr;
use std::str::FromStr; use std::str::FromStr;
use std::sync::Arc; use std::sync::Arc;
use std::time::Duration; use std::time::{Duration, Instant};
use thiserror::Error; use thiserror::Error;
use tokio::signal::ctrl_c; use tokio::signal::ctrl_c;
use tokio::time::sleep; use tokio::time::sleep;
@ -247,12 +247,16 @@ async fn run_loop(
); );
} }
let mut start_of_stop_time = None;
let stop_grace_time = Duration::from_secs(config.schedule.stop_grace_time);
loop { loop {
let next_start = start_schedule.upcoming(Utc).next().unwrap(); let next_start = start_schedule.upcoming(Utc).next().unwrap();
let next_stop = stop_schedule.upcoming(Utc).next().unwrap(); let next_stop = stop_schedule.upcoming(Utc).next().unwrap();
// we're between start time and stop time // we're between start time and stop time
if active_server.is_none() && next_start > next_stop { if active_server.is_none() && next_start > next_stop {
start_of_stop_time = None;
println!("Starting server"); println!("Starting server");
match start(cloud.as_ref(), &config).await { match start(cloud.as_ref(), &config).await {
Ok(server) => active_server = Some(server), Ok(server) => active_server = Some(server),
@ -272,6 +276,14 @@ async fn run_loop(
// we're between stop time and start time // we're between stop time and start time
if active_server.is_some() && next_stop > next_start { if active_server.is_some() && next_stop > next_start {
let stop_elapsed = start_of_stop_time
.get_or_insert_with(|| Instant::now())
.elapsed();
let stop = if stop_elapsed > stop_grace_time {
warn!("Server took longer than the grace time of {} seconds to empty, shutting down with players left", stop_grace_time.as_secs());
true
} else {
let active_players_res = match Rcon::new( let active_players_res = match Rcon::new(
(active_server.as_ref().unwrap().ip, 27015), (active_server.as_ref().unwrap().ip, 27015),
&config.server.rcon, &config.server.rcon,
@ -281,7 +293,7 @@ async fn run_loop(
Ok(mut rcon) => rcon.player_count().await, Ok(mut rcon) => rcon.player_count().await,
Err(e) => Err(e), Err(e) => Err(e),
}; };
let stop = match active_players_res { match active_players_res {
Ok(0) => true, Ok(0) => true,
Ok(count) => { Ok(count) => {
info!( info!(
@ -294,6 +306,7 @@ async fn run_loop(
error!("Error while trying get player count: {}", e); error!("Error while trying get player count: {}", e);
false false
} }
}
}; };
if stop { if stop {
let id = &active_server.as_ref().unwrap().id; let id = &active_server.as_ref().unwrap().id;