diff options
Diffstat (limited to 'src/service/systemd.rs')
-rw-r--r-- | src/service/systemd.rs | 31 |
1 files changed, 30 insertions, 1 deletions
diff --git a/src/service/systemd.rs b/src/service/systemd.rs index 45f3bf9..e3b4d1b 100644 --- a/src/service/systemd.rs +++ b/src/service/systemd.rs @@ -1,9 +1,12 @@ -use std::{fmt::Display, process::Command}; +use std::{fmt::Display, process::Command, time::Duration}; use serde::Deserialize; +use tokio::sync::watch::Sender; use crate::{Error, Status}; +use super::ServiceSpawner; + #[derive(Debug, Clone, Deserialize)] pub struct Systemd { pub service: String, @@ -15,6 +18,32 @@ impl Display for Systemd { } } +impl ServiceSpawner for Systemd { + async fn spawn(self, tx: Sender<Status>) -> Result<(), Error> { + let mut command = Command::new("systemctl"); + command.arg("is-active").arg(&self.service); + + let mut interval = tokio::time::interval(Duration::from_secs(5)); + loop { + interval.tick().await; + + let status = match command.output() { + Ok(output) if output.status.success() => Status::Pass, + Ok(output) => { + let stdout = String::from_utf8_lossy(&output.stdout).trim().to_string(); + Status::Fail(Some(format!("Service state: {}", stdout))) + } + Err(err) => { + tracing::error!("Failed to spawn process: {err}"); + Status::Unknown + } + }; + + tx.send_if_modified(|s| s.update(status)); + } + } +} + impl Systemd { pub async fn check(&self) -> Result<Status, Error> { let output = Command::new("systemctl") |