Skip to content

Commit

Permalink
support kill for pipeline
Browse files Browse the repository at this point in the history
fixed #55
  • Loading branch information
tao-guo committed Oct 17, 2023
1 parent d5154d2 commit 6a44afa
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions src/child.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ impl CmdChildren {
}
}

/// Waits for the children processes to exit completely, returning the status that they exited with.
pub fn wait(&mut self) -> CmdResult {
// wait for the last child result
let handle = self.children.pop().unwrap();
Expand Down Expand Up @@ -61,6 +62,22 @@ impl CmdChildren {
}
ret
}

/// Forces the children processes to exit.
pub fn kill(&mut self) -> CmdResult {
let mut ret = Ok(());
while let Some(child_handle) = self.children.pop() {
match child_handle {
Err(e) => ret = Err(e),
Ok(child_handle) => {
if let Err(e) = child_handle.kill() {
ret = Err(e);
}
}
}
}
ret
}
}

/// Representation of running or exited children processes with output, connected with pipes
Expand All @@ -73,6 +90,7 @@ pub struct FunChildren {
}

impl FunChildren {
/// Waits for the children processes to exit completely, returning the output.
pub fn wait_with_output(&mut self) -> FunResult {
// wait for the last child result
let handle = self.children.pop().unwrap();
Expand Down Expand Up @@ -106,6 +124,8 @@ impl FunChildren {
}
}

/// Waits for the children processes to exit completely, pipe content will be processed by
/// provided function.
pub fn wait_with_pipe(&mut self, f: &mut dyn FnMut(Box<dyn Read>)) -> CmdResult {
let child = self.children.pop().unwrap()?;
let polling_stderr = StderrLogging::new(&child.cmd, child.stderr);
Expand Down Expand Up @@ -186,6 +206,10 @@ impl CmdChild {
}
Ok(buf)
}

fn kill(self) -> CmdResult {
self.handle.kill()
}
}

pub(crate) enum CmdChildHandle {
Expand Down Expand Up @@ -259,6 +283,18 @@ impl CmdChildHandle {
)
}
}

fn kill(self) -> CmdResult {
match self {
CmdChildHandle::Proc(mut proc) => {
proc.kill()
}
CmdChildHandle::Thread(_thread) => {
panic!("thread killing not suppported!")
}
CmdChildHandle::SyncFn => { Ok(()) }
}
}
}

struct StderrLogging {
Expand Down

0 comments on commit 6a44afa

Please sign in to comment.