|
| 1 | +use std::{ |
| 2 | + io::{BufRead, BufReader}, |
| 3 | + process::{Child, Command, Stdio}, |
| 4 | +}; |
| 5 | + |
| 6 | +use anyhow::{anyhow, Context}; |
| 7 | +use crossbeam_channel::Sender; |
| 8 | + |
| 9 | +use crate::{command::dev::do_dev::log_err_and_continue, RoverError, RoverResult}; |
| 10 | + |
| 11 | +#[derive(Debug)] |
| 12 | +pub struct BackgroundTask { |
| 13 | + child: Child, |
| 14 | +} |
| 15 | + |
| 16 | +pub enum BackgroundTaskLog { |
| 17 | + Stdout(String), |
| 18 | + Stderr(String), |
| 19 | +} |
| 20 | + |
| 21 | +impl BackgroundTask { |
| 22 | + pub fn new(command: String, log_sender: Sender<BackgroundTaskLog>) -> RoverResult<Self> { |
| 23 | + let args: Vec<&str> = command.split(' ').collect(); |
| 24 | + let (bin, args) = match args.len() { |
| 25 | + 0 => Err(anyhow!("the command you passed is empty")), |
| 26 | + 1 => Ok((args[0], Vec::new())), |
| 27 | + _ => Ok((args[0], Vec::from_iter(args[1..].iter()))), |
| 28 | + }?; |
| 29 | + tracing::info!("starting `{}`", &command); |
| 30 | + if which::which(bin).is_err() { |
| 31 | + return Err(anyhow!("{} is not installed on this machine", &bin).into()); |
| 32 | + } |
| 33 | + |
| 34 | + let mut command = Command::new(bin); |
| 35 | + command.args(args).env("APOLLO_ROVER", "true"); |
| 36 | + |
| 37 | + command.stdout(Stdio::piped()).stderr(Stdio::piped()); |
| 38 | + |
| 39 | + let mut child = command |
| 40 | + .spawn() |
| 41 | + .with_context(|| "could not spawn child process")?; |
| 42 | + |
| 43 | + if let Some(stdout) = child.stdout.take() { |
| 44 | + let log_sender = log_sender.clone(); |
| 45 | + rayon::spawn(move || { |
| 46 | + let stdout = BufReader::new(stdout); |
| 47 | + stdout.lines().for_each(|line| { |
| 48 | + if let Ok(line) = line { |
| 49 | + log_sender |
| 50 | + .send(BackgroundTaskLog::Stdout(line)) |
| 51 | + .expect("could not update stdout logs for command"); |
| 52 | + } |
| 53 | + }); |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + if let Some(stderr) = child.stderr.take() { |
| 58 | + rayon::spawn(move || { |
| 59 | + let stderr = BufReader::new(stderr); |
| 60 | + stderr.lines().for_each(|line| { |
| 61 | + if let Ok(line) = line { |
| 62 | + log_sender |
| 63 | + .send(BackgroundTaskLog::Stderr(line)) |
| 64 | + .expect("could not update stderr logs for command"); |
| 65 | + } |
| 66 | + }); |
| 67 | + }); |
| 68 | + } |
| 69 | + |
| 70 | + Ok(Self { child }) |
| 71 | + } |
| 72 | + |
| 73 | + pub fn kill(&mut self) { |
| 74 | + let pid = self.id(); |
| 75 | + tracing::info!("killing child with pid {}", &pid); |
| 76 | + let _ = self.child.kill().map_err(|_| { |
| 77 | + log_err_and_continue(RoverError::new(anyhow!( |
| 78 | + "could not kill child with pid {}", |
| 79 | + &pid |
| 80 | + ))); |
| 81 | + }); |
| 82 | + } |
| 83 | + |
| 84 | + pub fn id(&self) -> u32 { |
| 85 | + self.child.id() |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +impl Drop for BackgroundTask { |
| 90 | + fn drop(&mut self) { |
| 91 | + self.kill() |
| 92 | + } |
| 93 | +} |
0 commit comments