use super::Executor;
use crate::runtime::error::RuntimeError;
use crate::runtime::sink::OutputStream;
use std::process::Stdio;
use std::sync::Arc;
use tokio::io::{AsyncBufReadExt, BufReader};
use tokio::process::Command;
impl Executor {
pub(crate) async fn spawn_host(&self, mut cmd: Command) -> Result<i32, RuntimeError> {
if !self.sink.capture() {
let status = cmd
.status()
.await
.map_err(|e| RuntimeError::Backend(crate::container::BackendError::Spawn(e)))?;
return Ok(status.code().unwrap_or(-1));
}
cmd.stdout(Stdio::piped());
cmd.stderr(Stdio::piped());
cmd.stdin(Stdio::null());
cmd.kill_on_drop(true);
let child = cmd
.spawn()
.map_err(|e| RuntimeError::Backend(crate::container::BackendError::Spawn(e)))?;
self.stream_child_to_sink(child).await
}
pub(crate) async fn write_stdin_and_stream(
&self,
mut child: tokio::process::Child,
body: &str,
) -> Result<i32, RuntimeError> {
use tokio::io::AsyncWriteExt;
if let Some(mut stdin_handle) = child.stdin.take() {
stdin_handle
.write_all(body.as_bytes())
.await
.map_err(|e| RuntimeError::Backend(crate::container::BackendError::Spawn(e)))?;
drop(stdin_handle);
}
self.stream_child_to_sink(child).await
}
pub(crate) async fn stream_child_to_sink(
&self,
mut child: tokio::process::Child,
) -> Result<i32, RuntimeError> {
let stdout = child.stdout.take();
let stderr = child.stderr.take();
let stdout_task = stdout.map(|s| {
let sink = Arc::clone(&self.sink);
tokio::spawn(async move {
let mut lines = BufReader::new(s).lines();
while let Ok(Some(line)) = lines.next_line().await {
sink.write_line(OutputStream::Stdout, &line);
}
})
});
let stderr_task = stderr.map(|s| {
let sink = Arc::clone(&self.sink);
tokio::spawn(async move {
let mut lines = BufReader::new(s).lines();
while let Ok(Some(line)) = lines.next_line().await {
sink.write_line(OutputStream::Stderr, &line);
}
})
});
let status = child
.wait()
.await
.map_err(|e| RuntimeError::Backend(crate::container::BackendError::Spawn(e)))?;
if let Some(t) = stdout_task {
let _ = t.await;
}
if let Some(t) = stderr_task {
let _ = t.await;
}
Ok(status.code().unwrap_or(-1))
}
}