From 2df86268298de8a4961c3296c19b0767565453e8 Mon Sep 17 00:00:00 2001 From: yuzu Date: Fri, 1 Aug 2025 08:48:17 +0000 Subject: unix socket get git-svn-id: svn+ssh://diminuette.aengel.lesbianunix.dev/salaryman/trunk@17 b9215c17-b818-4693-b096-d1e41a411fef --- src/service.rs | 43 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 39 insertions(+), 4 deletions(-) (limited to 'src/service.rs') diff --git a/src/service.rs b/src/service.rs index 28e4401..97dc359 100644 --- a/src/service.rs +++ b/src/service.rs @@ -21,6 +21,7 @@ pub struct ServiceConf { args: Option, directory: Option, autostart: bool, + oneshot: Option, } impl Default for ServiceConf { fn default() -> Self { @@ -37,6 +38,7 @@ impl ServiceConf { args: None, directory: None, autostart: false, + oneshot: None, } } /// Returns a new `ServiceConf` from parts. @@ -47,6 +49,7 @@ impl ServiceConf { args: Option, directory: Option, autostart: bool, + oneshot: Option, ) -> Self { Self { uuid, @@ -55,6 +58,7 @@ impl ServiceConf { args, directory, autostart, + oneshot, } } /// Returns a new `ServiceConf` from parts with new uuid. @@ -64,6 +68,7 @@ impl ServiceConf { args: Option, directory: Option, autostart: bool, + oneshot: Option, ) -> Self { Self { uuid: Uuid::new_v4(), @@ -72,6 +77,7 @@ impl ServiceConf { args, directory, autostart, + oneshot, } } /// Returns the `uuid::Uuid` associated with the service config @@ -98,6 +104,10 @@ impl ServiceConf { pub fn get_autostart(&self) -> bool { self.autostart } + /// Returns the oneshot value of the described service + pub fn get_oneshot(&self) -> &Option { + &self.oneshot + } /// Sets the name of the described service pub fn name(&mut self, name: &str) -> &mut Self { self.name = String::from(name); @@ -123,6 +133,15 @@ impl ServiceConf { self.autostart = autostart; self } + /// Sets the oneshot flag for the described service + pub fn oneshot(&mut self, oneshot: bool) -> &mut Self { + if oneshot { + self.oneshot = Some(true); + } else { + self.oneshot = None; + } + self + } /// Builds a Service from this object #[inline] pub fn build(&self) -> Result> { @@ -175,6 +194,15 @@ impl<'a> Service { pub fn name(&self) -> &str { &self.config().get_name() } + /// Returns the uuid of the service, shorthand for Service::config().get_uuid() + #[inline] + pub fn uuid(&self) -> &Uuid { + &self.config().get_uuid() + } + #[inline] + pub fn oneshot(&self) -> &Option { + &self.config().get_oneshot() + } #[inline] fn create_dirs(&self) -> Result<(), Box> { match std::fs::create_dir("./logs") { @@ -243,14 +271,21 @@ impl<'a> Service { } } /// Returns the state of the service - pub fn state(&mut self) -> Result> { + pub fn state(&mut self) -> ServiceState { if let Some(proc) = self.proc.as_mut() { match proc.try_wait() { - Err(_) | Ok(Some(_)) => Ok(ServiceState::Failed), - Ok(None) => Ok(ServiceState::Running), + Err(_) | Ok(Some(_)) => { + if let Some(b) = self.oneshot() { + if *b { + return ServiceState::Stopped; + } + } + ServiceState::Failed + }, + Ok(None) => ServiceState::Running, } } else { - Ok(ServiceState::Stopped) + ServiceState::Stopped } } /// Invokes kill on the service process -- cgit 1.4.1-2-gfad0