terminate after 30 seconds (#2513)

This commit is contained in:
Nikolay Volf 2016-10-07 14:10:53 +03:00 committed by Gav Wood
parent 4655fd04a5
commit 5354a0905e
4 changed files with 29 additions and 6 deletions

1
Cargo.lock generated
View File

@ -399,6 +399,7 @@ dependencies = [
"log 0.3.6 (registry+https://github.com/rust-lang/crates.io-index)",
"nanomsg 0.5.1 (git+https://github.com/ethcore/nanomsg.rs.git)",
"semver 0.2.3 (registry+https://github.com/rust-lang/crates.io-index)",
"time 0.1.35 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

View File

@ -13,6 +13,7 @@ nanomsg = { git = "https://github.com/ethcore/nanomsg.rs.git" }
ethcore-ipc-nano = { path = "../nano" }
semver = "0.2"
log = "0.3"
time = "0.1"
[build-dependencies]
ethcore-ipc-codegen = { path = "../codegen" }

View File

@ -22,6 +22,7 @@ extern crate ethcore_ipc as ipc;
extern crate ethcore_ipc_nano as nanoipc;
extern crate semver;
#[macro_use] extern crate log;
extern crate time;
pub mod service;
@ -187,23 +188,40 @@ impl Hypervisor {
}
/// Waits for every required module to check in
pub fn wait_for_shutdown(&self) {
pub fn wait_for_shutdown(&self) -> bool {
use time::{PreciseTime, Duration};
let mut worker = self.ipc_worker.write().unwrap();
let start = PreciseTime::now();
while !self.modules_shutdown() {
worker.poll()
worker.poll();
if start.to(PreciseTime::now()) > Duration::seconds(30) {
warn!("Some modules failed to shutdown gracefully, they will be terminated.");
break;
}
}
self.modules_shutdown()
}
/// Shutdown the ipc and all managed child processes
pub fn shutdown(&self) {
let mut childs = self.processes.write().unwrap();
for (ref mut module, _) in childs.iter_mut() {
for (ref module, _) in childs.iter() {
trace!(target: "hypervisor", "Stopping process module: {}", module);
self.service.send_shutdown(**module);
}
trace!(target: "hypervisor", "Waiting for shutdown...");
self.wait_for_shutdown();
if self.wait_for_shutdown() {
trace!(target: "hypervisor", "All modules reported shutdown");
return;
}
for (ref module, ref mut process) in childs.iter_mut() {
if self.service.is_running(**module) {
process.kill().unwrap();
trace!("Terminated {}", module);
}
}
}
}

View File

@ -39,7 +39,6 @@ pub struct ModuleState {
shutdown: bool,
}
#[ipc]
pub trait ControlService {
fn shutdown(&self) -> bool;
@ -106,6 +105,10 @@ impl HypervisorService {
self.modules.read().unwrap().iter().filter(|&(_, module)| module.started && !module.shutdown).count()
}
pub fn is_running(&self, id: IpcModuleId) -> bool {
self.modules.read().unwrap().get(&id).map(|module| module.started && !module.shutdown).unwrap_or(false)
}
pub fn send_shutdown(&self, module_id: IpcModuleId) {
let modules = self.modules.read().unwrap();
modules.get(&module_id).map(|module| {