Skip to content

Commit

Permalink
horust compiling and tests passing
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoPonzi committed Nov 2, 2024
1 parent 9cc084c commit 44fb57e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 30 deletions.
56 changes: 53 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 55 additions & 12 deletions horust/src/horust/commands_handler.rs
Original file line number Diff line number Diff line change
@@ -1,31 +1,64 @@
use crate::horust::bus::BusConnector;
use crate::horust::formats::{ServiceName, ServiceStatus};
use crate::horust::Event;
use horust_commands_lib::{CommandsHandlerTrait, HorustMsgServiceStatus};
use std::collections::HashMap;
use std::os::unix::net::UnixListener;
use std::path::PathBuf;
use std::thread;
use std::thread::JoinHandle;
use std::time::Duration;
use std::{fs, thread};

pub fn spawn(bus: BusConnector<Event>, uds_folder_path: PathBuf) -> JoinHandle<()> {
pub fn spawn(
bus: BusConnector<Event>,
uds_path: PathBuf,
services: Vec<ServiceName>,
) -> JoinHandle<()> {
thread::spawn(move || {
run(bus, uds_folder_path);
let mut commands_handler = CommandsHandler::new(bus, uds_path, services);
commands_handler.run();
})
}

fn run(bus: BusConnector<Event>, uds_folder_path: PathBuf) {
let mut commands_handler = CommandsHandler::new(bus, uds_folder_path);
commands_handler.start();
}

struct CommandsHandler {
bus: BusConnector<Event>,
services: HashMap<ServiceName, ServiceStatus>,
uds_listener: UnixListener,
uds_path: PathBuf,
}

impl CommandsHandler {
fn new(bus: BusConnector<Event>, uds_folder_path: PathBuf) -> Self {
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_listener: UnixListener::bind(uds_folder_path).unwrap(),
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));
}
}
}
Expand All @@ -35,7 +68,17 @@ impl CommandsHandlerTrait for CommandsHandler {
&mut self.uds_listener
}

fn get_service_status(&self, service_name: String) -> HorustMsgServiceStatus {
todo!()
fn get_service_status(&self, service_name: String) -> Option<HorustMsgServiceStatus> {
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,
})
}
}
21 changes: 9 additions & 12 deletions horust/src/horust/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,18 @@ mod supervisor;
#[derive(Debug)]
pub struct Horust {
services: Vec<Service>,
uds_folder_path: PathBuf,
uds_path: PathBuf,
}

impl Horust {
fn new(services: Vec<Service>, uds_folder_path: PathBuf) -> Self {
Horust {
services,
uds_folder_path,
}
fn new(services: Vec<Service>, uds_path: PathBuf) -> Self {
Horust { services, uds_path }
}

/// Creates a new Horust instance from a command.
/// The command will be wrapped in a service and run with sane defaults
pub fn from_command(command: String, uds_folder_path: PathBuf) -> Self {
Self::new(vec![Service::from_command(command)], uds_folder_path)
pub fn from_command(command: String, uds_path: PathBuf) -> Self {
Self::new(vec![Service::from_command(command)], uds_path)
}

fn load_services_from_folders(paths: &[PathBuf]) -> Result<Vec<Service>> {
Expand All @@ -53,10 +50,10 @@ impl Horust {
.collect::<Result<Vec<_>>>()
}
/// Create a new horust instance from multiple paths of services.
pub fn from_services_dirs(paths: &[PathBuf], uds_folder_path: PathBuf) -> Result<Self> {
pub fn from_services_dirs(paths: &[PathBuf], uds_path: PathBuf) -> Result<Self> {
let services = Self::load_services_from_folders(paths)?;
let services = validate(services)?;
Ok(Horust::new(services, uds_folder_path))
Ok(Horust::new(services, uds_path))
}

/// Blocking call, will setup the event loop and the threads and run all the available services.
Expand All @@ -83,9 +80,9 @@ impl Horust {
// Spawn helper threads:
healthcheck::spawn(dispatcher.join_bus(), self.services.clone());
commands_handler::spawn(
self.uds_folder_path.clone(),
dispatcher.join_bus(),
self.services.clone(),
self.uds_path.clone(),
self.services.iter().map(|s| s.name.clone()).collect(),
);
let handle = supervisor::spawn(dispatcher.join_bus(), self.services.clone());
dispatcher.run();
Expand Down
8 changes: 5 additions & 3 deletions horust/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clap::Parser;
use horust::horust::{ExitStatus, HorustConfig};
use horust::Horust;
use log::{error, info};
use nix::unistd::getpid;

#[derive(clap::Parser, Debug)]
#[clap(author, about)]
Expand Down Expand Up @@ -65,22 +66,23 @@ fn main() -> Result<()> {
opts.uds_folder_path
);
}
let uds_path = horust_commands_lib::get_path(&opts.uds_folder_path, getpid().into());

let mut horust = if opts.command.is_empty() {
info!(
"Loading services from {}",
display_directories(&opts.services_paths)
);
Horust::from_services_dirs(&opts.services_paths).with_context(|| {
Horust::from_services_dirs(&opts.services_paths, uds_path).with_context(|| {
format!(
"Failed loading services from {}",
display_directories(&opts.services_paths)
)
})?
} else {
info!("Running command: {:?}", opts.command);
Horust::from_command(opts.command.join(" "))
Horust::from_command(opts.command.join(" "), uds_path)
};
horust.set_uds_folder_path();

if let ExitStatus::SomeServiceFailed = horust.run() {
if config.unsuccessful_exit_finished_failed {
Expand Down

0 comments on commit 44fb57e

Please sign in to comment.