Refactor Runner a bit; add Drop impl for eventual use

This commit is contained in:
Matt Green 2016-10-22 16:54:16 -04:00
parent a408975bdc
commit 83b91e51d0

View File

@ -26,20 +26,28 @@ impl Runner {
} }
#[cfg(target_family = "windows")] #[cfg(target_family = "windows")]
fn kill(child: &mut Child) { fn kill(&self) {
let _ = child.kill(); if let Some(ref child) = self.process {
debug!("Killing child process (pid: {})", child.id());
let _ = child.kill();
}
} }
#[cfg(target_family = "unix")] #[cfg(target_family = "unix")]
fn kill(child: &mut Child) { fn kill(&self) {
use libc; use libc;
extern { extern {
fn killpg(pgrp: libc::pid_t, sig: libc::c_int) -> libc::c_int; fn killpg(pgrp: libc::pid_t, sig: libc::c_int) -> libc::c_int;
} }
unsafe { if let Some(ref child) = self.process {
killpg(child.id() as i32, libc::SIGTERM); debug!("Killing child process (pid: {})", child.id());
unsafe {
killpg(child.id() as i32, libc::SIGTERM);
}
} }
} }
@ -52,6 +60,8 @@ impl Runner {
command.env("WATCHEXEC_UPDATED_PATH", updated_paths[0]); command.env("WATCHEXEC_UPDATED_PATH", updated_paths[0]);
} }
debug!("Executing: {}", cmd);
command.spawn().ok() command.spawn().ok()
} }
@ -67,6 +77,8 @@ impl Runner {
command.env("WATCHEXEC_UPDATED_PATH", updated_paths[0]); command.env("WATCHEXEC_UPDATED_PATH", updated_paths[0]);
} }
debug!("Executing: {}", cmd);
command command
.before_exec(|| unsafe { libc::setpgid(0, 0); Ok(()) }) .before_exec(|| unsafe { libc::setpgid(0, 0); Ok(()) })
.spawn() .spawn()
@ -74,39 +86,43 @@ impl Runner {
} }
pub fn run_command(&mut self, cmd: &str, updated_paths: Vec<&str>) { pub fn run_command(&mut self, cmd: &str, updated_paths: Vec<&str>) {
if let Some(ref mut child) = self.process { if self.restart {
if self.restart { self.kill();
debug!("Killing child process (pid: {})", child.id());
Runner::kill(child);
}
debug!("Waiting for child process (pid: {})", child.id());
Runner::wait(child);
} }
self.wait();
if self.cls { if self.cls {
self.clear(); self.clear();
} }
debug!("Executing: {}", cmd);
self.process = self.invoke(cmd, updated_paths); self.process = self.invoke(cmd, updated_paths);
} }
#[cfg(target_family = "windows")] #[cfg(target_family = "windows")]
fn wait(child: &mut Child) { fn wait(&self) {
let _ = child.wait(); if let Some(ref child) = self.process {
} debug!("Waiting for child process (pid: {})", child.id());
let _ = child.wait();
#[cfg(target_family = "unix")]
fn wait(child: &mut Child) {
use libc;
unsafe {
let pid = child.id() as i32;
let status: Box<i32> = Box::new(0);
libc::waitpid(-pid, Box::into_raw(status), 0);
} }
} }
#[cfg(target_family = "unix")]
fn wait(&self) {
use nix::sys::wait::{waitpid};
if let Some(ref child) = self.process {
debug!("Waiting for child process (pid: {})", child.id());
let pid = child.id() as i32;
let _ = waitpid(-pid, None);
}
}
}
impl Drop for Runner {
fn drop(&mut self) {
self.kill();
self.wait();
}
} }