use super::start::cmd_start;
use super::stop::cmd_stop;
use super::{check_liveness, read_state, ContainerStatus};
pub fn cmd_restart(name: &str, time: u64) -> Result<(), Box<dyn std::error::Error>> {
let state = read_state(name).map_err(|_| format!("no container named '{}'", name))?;
if state.status == ContainerStatus::Running {
let pid = state.pid;
cmd_stop(name)?;
let deadline = std::time::Instant::now() + std::time::Duration::from_secs(time.max(1));
while check_liveness(pid) && std::time::Instant::now() < deadline {
std::thread::sleep(std::time::Duration::from_millis(100));
}
if check_liveness(pid) {
unsafe { libc::kill(pid, libc::SIGKILL) };
let kill_deadline = std::time::Instant::now() + std::time::Duration::from_secs(5);
while check_liveness(pid) && std::time::Instant::now() < kill_deadline {
std::thread::sleep(std::time::Duration::from_millis(100));
}
}
}
cmd_start(&[name.to_string()], false, None)
}