use std::{
sync::{Arc, Mutex},
time::Duration,
};
use indicatif::ProgressBar;
use tokio::time;
use tokio_process_tools::{
Chunk, Consumable, Consumer, Delivery, Next, Replay, SingleSubscriberOutputStream,
StreamVisitor,
};
use super::{CommandError, ProcessError};
use crate::{
error::Error,
logs::{LogError, LogFile, OutputLogs},
};
const OUTPUT_REFRESH_INTERVAL: Duration = Duration::from_millis(100);
const OUTPUT_EOF_TIMEOUT: Duration = Duration::from_secs(3);
const ERROR_CAPTURE_BYTES: usize = 64 * 1024;
pub(super) struct CapturedOutput {
pub(super) stdout: Vec<u8>,
pub(super) stderr: Vec<u8>,
}
enum CaptureBuffer {
Discard,
All(Vec<u8>),
Tail {
bytes: Vec<u8>,
capacity: usize,
},
}
impl CaptureBuffer {
fn extend(&mut self, input: &[u8]) {
match self {
Self::Discard => {}
Self::All(bytes) => bytes.extend_from_slice(input),
Self::Tail { bytes, capacity } => {
if input.len() >= *capacity {
bytes.clear();
bytes.extend_from_slice(&input[input.len() - *capacity..]);
return;
}
let overflow = bytes
.len()
.saturating_add(input.len())
.saturating_sub(*capacity);
if overflow != 0 {
bytes.drain(..overflow);
}
bytes.extend_from_slice(input);
}
}
}
fn into_bytes(self) -> Vec<u8> {
match self {
Self::Discard => Vec::new(),
Self::All(bytes) | Self::Tail { bytes, .. } => bytes,
}
}
}
struct Capture {
buffer: CaptureBuffer,
log: Option<LogFile>,
error: Option<LogError>,
latest: Arc<Mutex<Option<String>>>,
}
impl StreamVisitor for Capture {
type Output = Result<Vec<u8>, LogError>;
fn on_chunk(&mut self, chunk: Chunk) -> Next {
let bytes = chunk.as_ref();
self.buffer.extend(bytes);
if self.error.is_none()
&& let Some(log) = &mut self.log
&& let Err(error) = log.write(bytes)
{
self.error = Some(error);
}
if let Some(message) = display_message(bytes) {
*self
.latest
.lock()
.expect("progress output lock should not be poisoned") = Some(message);
}
Next::Continue
}
fn into_output(self) -> Self::Output {
self.error.map_or(Ok(self.buffer.into_bytes()), Err)
}
}
pub(super) struct OutputConsumers {
stdout: Consumer<Result<Vec<u8>, LogError>>,
stderr: Consumer<Result<Vec<u8>, LogError>>,
latest: Arc<Mutex<Option<String>>>,
}
impl OutputConsumers {
pub(super) fn attach<D, R>(
stdout: &SingleSubscriberOutputStream<D, R>,
stderr: &SingleSubscriberOutputStream<D, R>,
logs: Option<OutputLogs>,
capture_stdout: bool,
) -> Self
where
D: Delivery,
R: Replay,
{
let (stdout_log, stderr_log) = match logs {
Some(OutputLogs { stdout, stderr }) => (Some(stdout), Some(stderr)),
None => (None, None),
};
let latest = Arc::new(Mutex::new(None));
let stdout_buffer = if capture_stdout {
CaptureBuffer::All(Vec::new())
} else {
CaptureBuffer::Discard
};
let stderr_buffer = CaptureBuffer::Tail {
bytes: Vec::new(),
capacity: ERROR_CAPTURE_BYTES,
};
Self {
stdout: attach(stdout, stdout_log, Arc::clone(&latest), stdout_buffer),
stderr: attach(stderr, stderr_log, Arc::clone(&latest), stderr_buffer),
latest,
}
}
pub(super) fn refresh_interval(&self) -> time::Interval {
let mut interval = time::interval(OUTPUT_REFRESH_INTERVAL);
interval.set_missed_tick_behavior(time::MissedTickBehavior::Skip);
interval
}
pub(super) fn refresh(&self, progress: &ProgressBar) {
refresh_progress(&self.latest, progress);
}
pub(super) async fn finish(
self,
command: &str,
progress: &ProgressBar,
) -> Result<CapturedOutput, Error> {
let latest = Arc::clone(&self.latest);
let completion = async {
let (stdout, stderr) = tokio::join!(self.stdout.wait(), self.stderr.wait());
let stdout = stdout
.map_err(|source| output_read_error(command, source))?
.map_err(Error::Log)?;
let stderr = stderr
.map_err(|source| output_read_error(command, source))?
.map_err(Error::Log)?;
Ok(CapturedOutput { stdout, stderr })
};
let output = time::timeout(OUTPUT_EOF_TIMEOUT, completion)
.await
.map_err(|_elapsed| {
Error::Process(ProcessError::OutputDrainTimedOut {
command: command.to_owned(),
seconds: OUTPUT_EOF_TIMEOUT.as_secs(),
})
})??;
refresh_progress(&latest, progress);
Ok(output)
}
}
fn attach<D, R>(
stream: &SingleSubscriberOutputStream<D, R>,
log: Option<LogFile>,
latest: Arc<Mutex<Option<String>>>,
buffer: CaptureBuffer,
) -> Consumer<Result<Vec<u8>, LogError>>
where
D: Delivery,
R: Replay,
{
stream
.consume(Capture {
buffer,
log,
error: None,
latest,
})
.expect("the output stream should not have another consumer")
}
fn output_read_error(command: &str, source: tokio_process_tools::ConsumerError) -> Error {
Error::Process(ProcessError::OutputRead(CommandError::new(
command.to_owned(),
source,
)))
}
fn refresh_progress(latest: &Mutex<Option<String>>, progress: &ProgressBar) {
if let Some(message) = latest
.lock()
.expect("progress output lock should not be poisoned")
.take()
{
progress.set_message(message);
}
}
fn display_message(record: &[u8]) -> Option<String> {
String::from_utf8_lossy(record)
.rsplit(['\r', '\n'])
.find(|line| !line.trim().is_empty())
.map(str::to_owned)
}
#[cfg(test)]
mod tests {
use super::{CaptureBuffer, display_message};
#[test]
fn display_message_uses_the_last_nonempty_record() {
assert_eq!(
display_message(b"building\rchecking\n\n"),
Some("checking".to_owned())
);
assert_eq!(display_message(b"\r\n"), None);
}
#[test]
fn tail_capture_is_bounded() {
let mut buffer = CaptureBuffer::Tail {
bytes: Vec::new(),
capacity: 5,
};
buffer.extend(b"abc");
buffer.extend(b"defg");
assert_eq!(buffer.into_bytes(), b"cdefg");
let mut buffer = CaptureBuffer::Tail {
bytes: Vec::new(),
capacity: 3,
};
buffer.extend(b"discard-prefix");
assert_eq!(buffer.into_bytes(), b"fix");
}
}