-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #252 from FedericoPonzi/horustctl-status
Horust: initial support for commands uds and status command
- Loading branch information
Showing
6 changed files
with
158 additions
and
30 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
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; | ||
use std::path::PathBuf; | ||
use std::thread::JoinHandle; | ||
use std::time::Duration; | ||
use std::{fs, thread}; | ||
|
||
pub fn spawn( | ||
bus: BusConnector<Event>, | ||
uds_path: PathBuf, | ||
services: Vec<ServiceName>, | ||
) -> JoinHandle<()> { | ||
thread::spawn(move || { | ||
let mut commands_handler = CommandsHandler::new(bus, uds_path, services); | ||
commands_handler.run(); | ||
}) | ||
} | ||
|
||
struct CommandsHandler { | ||
bus: BusConnector<Event>, | ||
services: HashMap<ServiceName, ServiceStatus>, | ||
uds_listener: UnixListener, | ||
uds_path: PathBuf, | ||
} | ||
|
||
impl CommandsHandler { | ||
fn new(bus: BusConnector<Event>, uds_path: PathBuf, services: Vec<ServiceName>) -> Self { | ||
let uds_listener = UnixListener::bind(&uds_path).unwrap(); | ||
uds_listener.set_nonblocking(true).unwrap(); | ||
Self { | ||
bus, | ||
uds_path, | ||
uds_listener, | ||
services: services | ||
.into_iter() | ||
.map(|s| (s, ServiceStatus::Initial)) | ||
.collect(), | ||
} | ||
} | ||
fn run(&mut self) { | ||
loop { | ||
let evs = self.bus.try_get_events(); | ||
for ev in evs { | ||
match ev { | ||
Event::StatusChanged(name, status) => { | ||
let k = self.services.get_mut(&name).unwrap(); | ||
*k = status; | ||
} | ||
Event::ShuttingDownInitiated(_) => { | ||
fs::remove_file(&self.uds_path).unwrap(); | ||
return; | ||
} | ||
_ => {} | ||
} | ||
} | ||
self.accept().unwrap(); | ||
thread::sleep(Duration::from_millis(300)); | ||
} | ||
} | ||
} | ||
|
||
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<HorustMsgServiceStatus> { | ||
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 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, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters