From d8bc214ad59b7a5735e2e85ee141f09e7e16b531 Mon Sep 17 00:00:00 2001 From: Federico Ponzi Date: Sat, 2 Nov 2024 12:00:14 +0000 Subject: [PATCH] Implements missing methods, minor refactorings --- Cargo.lock | 55 ++------------------------- commands/README.md | 1 + horust/src/horust/commands_handler.rs | 46 ++++++++++++++++------ horust/src/horust/formats/mod.rs | 3 -- horust/src/main.rs | 2 +- 5 files changed, 39 insertions(+), 68 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index a06fdd8..2bd9701 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -331,19 +331,6 @@ dependencies = [ "regex", ] -[[package]] -name = "env_logger" -version = "0.10.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4cd405aab171cb85d6735e5c8d9db038c17d3ca007a4d2c25f337935c3d90580" -dependencies = [ - "humantime", - "is-terminal", - "log", - "regex", - "termcolor", -] - [[package]] name = "env_logger" version = "0.11.5" @@ -500,12 +487,6 @@ version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" -[[package]] -name = "hermit-abi" -version = "0.4.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fbf6a919d6cf397374f7dfeeea91d974c7c0a7221d0d0f4f20d859d329e53fcc" - [[package]] name = "horust" version = "0.1.8" @@ -515,7 +496,7 @@ dependencies = [ "bytefmt", "clap", "crossbeam", - "env_logger 0.11.5", + "env_logger", "horust-commands-lib", "humantime-serde", "libc", @@ -553,8 +534,7 @@ dependencies = [ "anyhow", "clap", "crossbeam", - "env_logger 0.11.5", - "horust-commands-lib", + "env_logger", "log", ] @@ -678,17 +658,6 @@ version = "2.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ddc24109865250148c2e0f3d25d4f0f479571723792d3802153c60922a4fb708" -[[package]] -name = "is-terminal" -version = "0.4.13" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "261f68e344040fbd0edea105bef17c66edf46f984ddb1115b775ce31be948f4b" -dependencies = [ - "hermit-abi 0.4.0", - "libc", - "windows-sys 0.52.0", -] - [[package]] name = "is_terminal_polyfill" version = "1.70.1" @@ -780,7 +749,7 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" dependencies = [ - "hermit-abi 0.3.9", + "hermit-abi", "libc", "wasi", "windows-sys 0.52.0", @@ -1307,15 +1276,6 @@ dependencies = [ "windows-sys 0.59.0", ] -[[package]] -name = "termcolor" -version = "1.4.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" -dependencies = [ - "winapi-util", -] - [[package]] name = "termtree" version = "0.4.1" @@ -1591,15 +1551,6 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" -[[package]] -name = "winapi-util" -version = "0.1.9" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" -dependencies = [ - "windows-sys 0.59.0", -] - [[package]] name = "winapi-x86_64-pc-windows-gnu" version = "0.4.0" diff --git a/commands/README.md b/commands/README.md index 676f8f7..d740a40 100644 --- a/commands/README.md +++ b/commands/README.md @@ -1,4 +1,5 @@ ## Compile + To compile this crate, you will need protobuf compiler. On debian-like you can run: apt-get install protobuf-compiler diff --git a/horust/src/horust/commands_handler.rs b/horust/src/horust/commands_handler.rs index 8f97ec5..ff88e94 100644 --- a/horust/src/horust/commands_handler.rs +++ b/horust/src/horust/commands_handler.rs @@ -1,6 +1,7 @@ use crate::horust::bus::BusConnector; use crate::horust::formats::{ServiceName, ServiceStatus}; use crate::horust::Event; +use anyhow::{anyhow, Result}; use horust_commands_lib::{CommandsHandlerTrait, HorustMsgServiceStatus}; use std::collections::HashMap; use std::os::unix::net::UnixListener; @@ -67,18 +68,39 @@ impl CommandsHandlerTrait for CommandsHandler { fn get_unix_listener(&mut self) -> &mut UnixListener { &mut self.uds_listener } + fn get_service_status(&self, service_name: &str) -> anyhow::Result { + self.services + .get(service_name) + .map(from_service_status) + .ok_or_else(|| anyhow!("Error: service {service_name} not found.")) + } + fn update_service_status( + &self, + service_name: &str, + new_status: HorustMsgServiceStatus, + ) -> Result<()> { + /* + match self.services.get(service_name) { + None => bail!("Service {service_name} not found."), + Some(service_status) if from_service_status(service_status) != new_status => { + //self.bus.send_event(Event::Kill()) + } + _ => (), + };*/ + todo!(); + } +} - fn get_service_status(&self, service_name: String) -> Option { - self.services.get(&service_name).map(|status| match status { - ServiceStatus::Starting => HorustMsgServiceStatus::Starting, - ServiceStatus::Started => HorustMsgServiceStatus::Started, - ServiceStatus::Running => HorustMsgServiceStatus::Running, - ServiceStatus::InKilling => HorustMsgServiceStatus::Inkilling, - ServiceStatus::Success => HorustMsgServiceStatus::Success, - ServiceStatus::Finished => HorustMsgServiceStatus::Finished, - ServiceStatus::FinishedFailed => HorustMsgServiceStatus::Finishedfailed, - ServiceStatus::Failed => HorustMsgServiceStatus::Failed, - ServiceStatus::Initial => HorustMsgServiceStatus::Initial, - }) +fn from_service_status(status: &ServiceStatus) -> HorustMsgServiceStatus { + match status { + ServiceStatus::Starting => HorustMsgServiceStatus::Starting, + ServiceStatus::Started => HorustMsgServiceStatus::Started, + ServiceStatus::Running => HorustMsgServiceStatus::Running, + ServiceStatus::InKilling => HorustMsgServiceStatus::Inkilling, + ServiceStatus::Success => HorustMsgServiceStatus::Success, + ServiceStatus::Finished => HorustMsgServiceStatus::Finished, + ServiceStatus::FinishedFailed => HorustMsgServiceStatus::Finishedfailed, + ServiceStatus::Failed => HorustMsgServiceStatus::Failed, + ServiceStatus::Initial => HorustMsgServiceStatus::Initial, } } diff --git a/horust/src/horust/formats/mod.rs b/horust/src/horust/formats/mod.rs index 751ebd5..357f941 100644 --- a/horust/src/horust/formats/mod.rs +++ b/horust/src/horust/formats/mod.rs @@ -15,7 +15,6 @@ pub enum ShuttingDown { #[derive(Debug, Clone, PartialEq, Eq)] pub enum Event { PidChanged(ServiceName, Pid), - ServiceStarted(ServiceName), // This command updates the service status. StatusUpdate(ServiceName, ServiceStatus), // This event represents a status change. @@ -27,8 +26,6 @@ pub enum Event { Run(ServiceName), ShuttingDownInitiated(ShuttingDown), HealthCheck(ServiceName, HealthinessStatus), - // TODO: to allow changes of service at supervisor: - //ServiceCreated(ServiceHandler) } impl Event { diff --git a/horust/src/main.rs b/horust/src/main.rs index d36ec3e..fdaffca 100644 --- a/horust/src/main.rs +++ b/horust/src/main.rs @@ -30,7 +30,7 @@ struct Opts { /// Path to service file or a directory containing services to run. You can provide more than one argument to load multiple directories / services. services_paths: Vec, - #[arg(required = false, default_value = "/var/run/horust")] + #[arg(required = false, long, default_value = "/var/run/horust")] /// Path to the folder that contains the Unix Domain Socket, used to communicate with horustctl uds_folder_path: PathBuf,