use std::time::Duration;
use bollard::models::HealthConfig;
#[derive(Debug, Clone)]
pub struct Healthcheck {
test: Vec<String>,
interval: Option<Duration>,
timeout: Option<Duration>,
retries: Option<u32>,
start_period: Option<Duration>,
start_interval: Option<Duration>,
}
impl Healthcheck {
pub fn none() -> Self {
Self {
test: vec!["NONE".to_string()],
interval: None,
timeout: None,
retries: None,
start_period: None,
start_interval: None,
}
}
pub fn cmd_shell(command: impl Into<String>) -> Self {
Self {
test: vec!["CMD-SHELL".to_string(), command.into()],
interval: None,
timeout: None,
retries: None,
start_period: None,
start_interval: None,
}
}
pub fn cmd<I, S>(command: I) -> Self
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut test = vec!["CMD".to_string()];
test.extend(command.into_iter().map(|s| s.as_ref().to_owned()));
Self {
test,
interval: None,
timeout: None,
retries: None,
start_period: None,
start_interval: None,
}
}
pub fn empty() -> Self {
Self {
test: vec![],
interval: None,
timeout: None,
retries: None,
start_period: None,
start_interval: None,
}
}
pub fn with_interval(mut self, interval: impl Into<Option<Duration>>) -> Self {
self.interval = interval.into();
self
}
pub fn with_timeout(mut self, timeout: impl Into<Option<Duration>>) -> Self {
self.timeout = timeout.into();
self
}
pub fn with_retries(mut self, retries: impl Into<Option<u32>>) -> Self {
self.retries = retries.into();
self
}
pub fn with_start_period(mut self, start_period: impl Into<Option<Duration>>) -> Self {
self.start_period = start_period.into();
self
}
pub fn with_start_interval(mut self, interval: impl Into<Option<Duration>>) -> Self {
self.start_interval = interval.into();
self
}
pub fn test(&self) -> &[String] {
&self.test
}
pub fn interval(&self) -> Option<Duration> {
self.interval
}
pub fn timeout(&self) -> Option<Duration> {
self.timeout
}
pub fn retries(&self) -> Option<u32> {
self.retries
}
pub fn start_period(&self) -> Option<Duration> {
self.start_period
}
pub fn start_interval(&self) -> Option<Duration> {
self.start_interval
}
pub(crate) fn into_health_config(self) -> HealthConfig {
let to_nanos = |d: Duration| -> i64 { d.as_nanos().try_into().unwrap_or(i64::MAX) };
HealthConfig {
test: Some(self.test),
interval: self.interval.map(to_nanos),
timeout: self.timeout.map(to_nanos),
retries: self.retries.map(|r| r as i64),
start_period: self.start_period.map(to_nanos),
start_interval: self.start_interval.map(to_nanos),
}
}
}