add setState command, detect crashed/stopped servers periodically

This commit is contained in:
2026-06-12 00:01:23 +02:00
parent 0bbb8b3245
commit 5125aed0da
4 changed files with 54 additions and 2 deletions

View File

@ -4,6 +4,7 @@ pub mod remove;
pub mod ls;
pub mod start;
pub mod stop;
pub mod set_state;
pub mod env_settings;

33
src/cli/set_state.rs Normal file
View File

@ -0,0 +1,33 @@
use clap::Arg;
use clap::ArgMatches;
use clap::Command;
use diesel::prelude::*;
use crate::DbPool;
use crate::schema;
use crate::status::ServerStatus;
pub fn create_subcommand() -> Command {
Command::new("setState")
.about("force the known state of a server (will not change the actual state)")
.arg(Arg::new("NAME").required(true))
.arg(Arg::new("STATE").required(true))
}
pub async fn subcommand(arguments: &ArgMatches, pool: DbPool) -> Result<(), String> {
use schema::servers::dsl::*;
let server_name = arguments.get_one::<String>("NAME").unwrap();
let wanted_state = match arguments.get_one::<String>("STATE").unwrap().as_str() {
"stopped" => {ServerStatus::Stopped},
"running" => {ServerStatus::Running},
_ => { return Err("invalid state, must be `stoppped` or `running`".to_string()) }
};
let conn = &mut pool.get().unwrap();
diesel::update(servers).filter(name.eq(server_name)).set(status.eq(wanted_state)).execute(conn).map_err(|e| format!("Failed to set the server to the wanted state : {}", e))?;
Ok(())
}