1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
use crate::{process, CmdResult, FunResult};
use log::{error, info, warn};
use os_pipe::PipeReader;
use std::io::{BufRead, BufReader, Error, ErrorKind, Read, Result};
use std::process::{Child, ExitStatus};
use std::thread::JoinHandle;

/// Representation of running or exited children processes, connected with pipes
/// optionally.
///
/// Calling `spawn!` or `spawn_with_output!` macro will return `Result<CmdChildren>`
pub struct CmdChildren {
    children: Vec<CmdChild>,
    ignore_error: bool,
}

impl CmdChildren {
    pub(crate) fn from(children: Vec<CmdChild>, ignore_error: bool) -> Self {
        Self {
            children,
            ignore_error,
        }
    }

    pub fn wait_cmd_result(&mut self) -> CmdResult {
        let ret = self.wait_cmd_result_nolog();
        if let Err(ref err) = ret {
            if self.ignore_error {
                return Ok(());
            } else {
                error!(
                    "Running {} failed, Error: {}",
                    CmdChild::get_full_cmd(&self.children),
                    err
                );
            }
        }
        ret
    }

    pub(crate) fn wait_cmd_result_nolog(&mut self) -> CmdResult {
        // wait last process result
        let handle = self.children.pop().unwrap();
        if let Err(e) = handle.wait(true, self.ignore_error) {
            if !self.ignore_error {
                let _ = Self::wait_children(&mut self.children, self.ignore_error);
                return Err(e);
            }
        }
        Self::wait_children(&mut self.children, self.ignore_error)
    }

    fn wait_children(children: &mut Vec<CmdChild>, ignore_error: bool) -> CmdResult {
        let mut ret = Ok(());
        while !children.is_empty() {
            let child_handle = children.pop().unwrap();
            if let Err(e) = child_handle.wait(false, ignore_error) {
                ret = Err(e);
            }
        }
        ret
    }

    pub fn wait_fun_result(&mut self) -> FunResult {
        let ret = self.wait_fun_result_nolog();
        if let Err(ref err) = ret {
            if self.ignore_error {
                return Ok("".into());
            } else {
                error!(
                    "Running {} failed, Error: {}",
                    CmdChild::get_full_cmd(&self.children),
                    err
                );
            }
        }
        ret
    }

    pub(crate) fn wait_fun_result_nolog(&mut self) -> FunResult {
        // wait last process result
        let handle = self.children.pop().unwrap();
        let wait_last = handle.wait_with_output(self.ignore_error);
        match wait_last {
            Err(e) => {
                let _ = CmdChildren::wait_children(&mut self.children, self.ignore_error);
                Err(e)
            }
            Ok(output) => {
                let mut ret = String::from_utf8_lossy(&output).to_string();
                if ret.ends_with('\n') {
                    ret.pop();
                }
                CmdChildren::wait_children(&mut self.children, self.ignore_error)?;
                Ok(ret)
            }
        }
    }

    pub fn wait_with_pipe(&mut self, f: &mut dyn FnMut(Box<dyn Read>)) {
        let handle = self.children.pop().unwrap();
        match handle {
            CmdChild::Proc {
                mut child,
                stdout,
                stderr,
                cmd,
                ..
            } => {
                let polling_stderr = stderr.map(CmdChild::log_stderr_output);
                if let Some(stdout) = stdout {
                    f(Box::new(stdout));
                    let _ = child.kill();
                }
                CmdChild::wait_logging_thread(&cmd, polling_stderr);
            }
            CmdChild::ThreadFn {
                stderr,
                stdout,
                cmd,
                ..
            } => {
                let polling_stderr = stderr.map(CmdChild::log_stderr_output);
                if let Some(stdout) = stdout {
                    f(Box::new(stdout));
                }
                CmdChild::wait_logging_thread(&cmd, polling_stderr);
            }
            CmdChild::SyncFn {
                stderr,
                stdout,
                cmd,
                ..
            } => {
                let polling_stderr = stderr.map(CmdChild::log_stderr_output);
                if let Some(stdout) = stdout {
                    f(Box::new(stdout));
                }
                CmdChild::wait_logging_thread(&cmd, polling_stderr);
            }
        };
        let _ = Self::wait_children(&mut self.children, self.ignore_error);
    }
}

#[derive(Debug)]
pub(crate) enum CmdChild {
    Proc {
        child: Child,
        cmd: String,
        stdout: Option<PipeReader>,
        stderr: Option<PipeReader>,
    },
    ThreadFn {
        child: JoinHandle<CmdResult>,
        cmd: String,
        stdout: Option<PipeReader>,
        stderr: Option<PipeReader>,
    },
    SyncFn {
        cmd: String,
        stdout: Option<PipeReader>,
        stderr: Option<PipeReader>,
    },
}

impl CmdChild {
    fn wait(self, is_last: bool, ignore_error: bool) -> CmdResult {
        match self {
            CmdChild::Proc {
                mut child,
                stderr,
                cmd,
                ..
            } => {
                let polling_stderr = stderr.map(CmdChild::log_stderr_output);
                let status = child.wait()?;
                Self::wait_logging_thread(&cmd, polling_stderr);
                if !status.success() {
                    if !ignore_error && (is_last || process::pipefail_enabled()) {
                        return Err(Self::status_to_io_error(
                            status,
                            &format!("{} exited with error", cmd),
                        ));
                    } else if process::debug_enabled() {
                        warn!("{} exited with error: {}", cmd, status);
                    }
                }
            }
            CmdChild::ThreadFn {
                child, cmd, stderr, ..
            } => {
                let polling_stderr = stderr.map(CmdChild::log_stderr_output);
                let status = child.join();
                Self::wait_logging_thread(&cmd, polling_stderr);
                match status {
                    Err(e) => {
                        return Err(Error::new(
                            ErrorKind::Other,
                            format!("{} thread joined with error: {:?}", cmd, e),
                        ));
                    }
                    Ok(result) => {
                        if let Err(e) = result {
                            if !ignore_error && (is_last || process::pipefail_enabled()) {
                                return Err(e);
                            } else if process::debug_enabled() {
                                warn!("{} exited with error: {:?}", cmd, e);
                            }
                        }
                    }
                }
            }
            CmdChild::SyncFn { stderr, cmd, .. } => {
                Self::wait_logging_thread(&cmd, stderr.map(Self::log_stderr_output));
            }
        }
        Ok(())
    }

    fn wait_with_output(self, ignore_error: bool) -> Result<Vec<u8>> {
        let read_to_buf = |stdout: Option<PipeReader>| -> Result<Vec<u8>> {
            if let Some(mut out) = stdout {
                let mut buf = vec![];
                out.read_to_end(&mut buf)?;
                Ok(buf)
            } else {
                Ok(vec![])
            }
        };
        match self {
            CmdChild::Proc {
                mut child,
                cmd,
                stdout,
                stderr,
            } => {
                drop(child.stdin.take());
                let polling_stderr = stderr.map(CmdChild::log_stderr_output);
                let buf = read_to_buf(stdout)?;
                let status = child.wait()?;
                Self::wait_logging_thread(&cmd, polling_stderr);
                if !status.success() && !ignore_error {
                    return Err(Self::status_to_io_error(
                        status,
                        &format!("{} exited with error", cmd),
                    ));
                } else if process::debug_enabled() {
                    warn!("{} exited with error: {}", cmd, status);
                }
                Ok(buf)
            }
            CmdChild::ThreadFn {
                cmd,
                stdout,
                stderr,
                child,
                ..
            } => {
                let polling_stderr = stderr.map(CmdChild::log_stderr_output);
                let buf = read_to_buf(stdout)?;
                let status = child.join();
                Self::wait_logging_thread(&cmd, polling_stderr);
                match status {
                    Err(e) => {
                        return Err(Error::new(
                            ErrorKind::Other,
                            format!("{} thread joined with error: {:?}", cmd, e),
                        ));
                    }
                    Ok(result) => {
                        if let Err(e) = result {
                            if !ignore_error {
                                return Err(e);
                            } else if process::debug_enabled() {
                                warn!("{} exited with error: {:?}", cmd, e);
                            }
                        }
                    }
                }
                Ok(buf)
            }
            CmdChild::SyncFn {
                cmd,
                stdout,
                stderr,
                ..
            } => {
                Self::wait_logging_thread(&cmd, stderr.map(Self::log_stderr_output));
                if let Some(mut out) = stdout {
                    let mut buf = vec![];
                    out.read_to_end(&mut buf)?;
                    return Ok(buf);
                }
                Ok(vec![])
            }
        }
    }

    fn log_stderr_output(stderr: PipeReader) -> JoinHandle<()> {
        std::thread::spawn(move || {
            BufReader::new(stderr)
                .lines()
                .filter_map(|line| line.ok())
                .for_each(|line| info!("{}", line))
        })
    }

    fn wait_logging_thread(cmd: &str, thread: Option<JoinHandle<()>>) {
        if let Some(thread) = thread {
            if let Err(e) = thread.join() {
                warn!("{} logging thread exited with error: {:?}", cmd, e);
            }
        }
    }

    fn status_to_io_error(status: ExitStatus, command: &str) -> Error {
        if let Some(code) = status.code() {
            Error::new(
                ErrorKind::Other,
                format!("{}; status code: {}", command, code),
            )
        } else {
            Error::new(
                ErrorKind::Other,
                format!("{}; terminated by {}", command, status),
            )
        }
    }

    fn get_full_cmd(children: &[Self]) -> String {
        children
            .iter()
            .map(|child| match child {
                CmdChild::Proc { cmd, .. } => cmd.to_owned(),
                CmdChild::ThreadFn { cmd, .. } => cmd.to_owned(),
                CmdChild::SyncFn { cmd, .. } => cmd.to_owned(),
            })
            .collect::<Vec<_>>()
            .join(" | ")
    }
}