Skip to content

Commit

Permalink
removes itertools as dependency, implement default for SH
Browse files Browse the repository at this point in the history
  • Loading branch information
FedericoPonzi committed Oct 2, 2022
1 parent 5e3de2c commit 0776eba
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 24 deletions.
1 change: 0 additions & 1 deletion Cargo.lock

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

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ maplit = "~1.0"
shellexpand = "~2.1"
anyhow = "~1.0"
thiserror = "~1.0"
itertools = "~0.10"

[features]
default = ["http-healthcheck"]
Expand Down
8 changes: 5 additions & 3 deletions src/horust/error.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use itertools::Itertools;

#[derive(Debug, thiserror::Error)]
#[error("Found following errors during validation phase: {}", validation_errors(.0))]
pub struct ValidationErrors(Vec<ValidationError>);
Expand All @@ -11,7 +9,11 @@ impl ValidationErrors {
}

fn validation_errors(errors: &[ValidationError]) -> String {
errors.iter().map(|s| format!("* {}", s)).join("\n")
errors
.iter()
.map(|s| format!("* {}", s))
.collect::<Vec<String>>()
.join("\n")
}

#[derive(Debug, thiserror::Error)]
Expand Down
3 changes: 2 additions & 1 deletion src/horust/formats/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ impl User {
}
}

#[derive(Serialize, Clone, Deserialize, Debug, Eq, PartialEq, Hash)]
#[derive(Serialize, Clone, Deserialize, Debug, Eq, PartialEq, Hash, Default)]
pub enum ServiceStatus {
/// The service will be started asap
Starting,
Expand All @@ -387,6 +387,7 @@ pub enum ServiceStatus {
Failed,
/// This is the initial state: A service in Initial state is marked to be runnable:
/// it will be run as soon as possible.
#[default]
Initial,
}

Expand Down
28 changes: 13 additions & 15 deletions src/horust/supervisor/service_handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::horust::Event;

use super::{LifecycleStatus, ShuttingDown};

#[derive(Clone, Debug, Eq, PartialEq)]
#[derive(Clone, Debug, Eq, PartialEq, Default)]
pub(crate) struct ServiceHandler {
service: Service,
/// Status of this service.
Expand All @@ -29,16 +29,21 @@ impl From<Service> for ServiceHandler {
fn from(service: Service) -> Self {
ServiceHandler {
service,
status: ServiceStatus::Initial,
pid: None,
shutting_down_start: None,
restart_attempts: 0,
healthiness_checks_failed: None,
..Default::default()
}
}
}

impl ServiceHandler {
fn is_alive_state(&self) -> bool {
const ALIVE_STATES: [ServiceStatus; 3] = [
ServiceStatus::Running,
ServiceStatus::Started,
ServiceStatus::Starting,
];
ALIVE_STATES.contains(&self.status)
}

pub fn start_after(&self) -> &Vec<String> {
self.service.start_after.as_ref()
}
Expand Down Expand Up @@ -69,15 +74,8 @@ impl ServiceHandler {
pub fn change_status(&self, new_status: ServiceStatus) -> (ServiceHandler, ServiceStatus) {
handle_status_change(self, new_status)
}
fn is_alive_state(&self) -> bool {
const ALIVE_STATES: [ServiceStatus; 3] = [
ServiceStatus::Running,
ServiceStatus::Started,
ServiceStatus::Starting,
];
ALIVE_STATES.contains(&self.status)
}

/// Restart attempts are over if the attempts field is zero or we already retried enough times.
pub fn restart_attempts_are_over(&self) -> bool {
self.service.restart.attempts == 0 || self.restart_attempts > self.service.restart.attempts
}
Expand Down Expand Up @@ -286,7 +284,7 @@ fn handle_status_change(
}
}
} else {
debug!(
error!(
"Tried to make an illegal transition: (current) {} ⇾ {} (received) for service: {}",
service_handler.status,
next_status,
Expand Down
8 changes: 5 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use clap::Parser;
use horust::horust::ExitStatus;
use horust::horust::HorustConfig;
use horust::Horust;
use itertools::Itertools;
use log::{error, info};

#[derive(clap::Parser, Debug)]
Expand Down Expand Up @@ -56,7 +55,7 @@ fn main() -> Result<()> {

let mut horust = if !opts.command.is_empty() {
info!("Running command: {:?}", opts.command);
Horust::from_command(opts.command.into_iter().join(" "))
Horust::from_command(opts.command.join(" "))
} else {
info!(
"Loading services from {}",
Expand Down Expand Up @@ -84,7 +83,10 @@ fn display_directories(dirs: &[PathBuf]) -> String {
1 => format!("directory: {}", dirs.first().unwrap().display()),
_ => format!(
"directories:\n{}",
dirs.iter().map(|d| format!("* {}", d.display())).join("\n")
dirs.iter()
.map(|d| format!("* {}", d.display()))
.collect::<Vec<String>>()
.join("\n"),
),
}
}

0 comments on commit 0776eba

Please sign in to comment.