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,27 +276,36 @@ 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 active_players_res = match Rcon::new( let stop_elapsed = start_of_stop_time
(active_server.as_ref().unwrap().ip, 27015), .get_or_insert_with(|| Instant::now())
&config.server.rcon, .elapsed();
)
.await 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());
Ok(mut rcon) => rcon.player_count().await, true
Err(e) => Err(e), } else {
}; let active_players_res = match Rcon::new(
let stop = match active_players_res { (active_server.as_ref().unwrap().ip, 27015),
Ok(0) => true, &config.server.rcon,
Ok(count) => { )
info!( .await
"Want to stop server, but there are still {} active players", {
count Ok(mut rcon) => rcon.player_count().await,
); Err(e) => Err(e),
false };
} match active_players_res {
Err(e) => { Ok(0) => true,
error!("Error while trying get player count: {}", e); Ok(count) => {
false info!(
"Want to stop server, but there are still {} active players",
count
);
false
}
Err(e) => {
error!("Error while trying get player count: {}", e);
false
}
} }
}; };
if stop { if stop {