use std::io;
use std::pin::Pin;
use tokio::io::{AsyncRead, AsyncWrite, AsyncWriteExt as _, BufReader};
use tokio::sync::mpsc;
use tokio::sync::mpsc::error::TrySendError;
use tokio::time::{Instant, Sleep, sleep_until, timeout};
use crate::channel::{ChildMessage, ShepherdMessage};
use crate::runner::{LogCtl, LogLine, RunnerError, StdinWrite};
use super::FINAL_DRAIN;
#[cfg(unix)]
use super::READ_BUFFER;
use super::line_reader::LineReader;
use super::log_file::{LogFile, LogFiles, LogSink, PipeFds};
enum AfterLine {
KeepReading,
StreamEnded,
LogsClosed,
}
async fn deliver_line<O, E>(
result: io::Result<Option<String>>,
err: bool,
files: &mut LogFiles,
logs_tx: &mpsc::Sender<LogLine>,
ctl_rx: &mut mpsc::Receiver<LogCtl>,
streams: &mut Streams<O, E>,
) -> AfterLine
where
O: AsyncRead + Unpin,
E: AsyncRead + Unpin,
{
match result {
Ok(Some(line)) => {
files.stream(err).append(&line).await;
let Some(slot) = reserve_slot(logs_tx, files, ctl_rx, streams).await else {
return AfterLine::LogsClosed;
};
slot.send(LogLine { err, line });
AfterLine::KeepReading
}
Ok(None) => AfterLine::StreamEnded, Err(error) => {
tracing::error!(path = ?files.stream(err).path, %error, "log stream read failed");
AfterLine::StreamEnded
}
}
}
async fn reserve_slot<'tx, O, E>(
logs_tx: &'tx mpsc::Sender<LogLine>,
files: &mut LogFiles,
ctl_rx: &mut mpsc::Receiver<LogCtl>,
streams: &mut Streams<O, E>,
) -> Option<mpsc::Permit<'tx, LogLine>>
where
O: AsyncRead + Unpin,
E: AsyncRead + Unpin,
{
match logs_tx.try_reserve() {
Ok(slot) => return Some(slot),
Err(TrySendError::Closed(())) => return None,
Err(TrySendError::Full(())) => {}
}
loop {
let flush_at = files.flush_deadline();
tokio::select! {
slot = logs_tx.reserve() => return slot.ok(),
ctl = ctl_rx.recv() => match ctl {
Some(ctl) => files.serve(ctl, streams).await,
None => return logs_tx.reserve().await.ok(),
},
() = sleep_until(flush_at.unwrap_or_else(Instant::now)), if flush_at.is_some() => {
files.flush_idle().await;
}
}
}
}
async fn next_line_ref<R>(lines: &mut Option<LineReader<R>>) -> io::Result<Option<&str>>
where
R: AsyncRead + Unpin,
{
match lines {
Some(lines) => lines.read_line().await,
None => core::future::pending().await,
}
}
async fn next_line<R>(lines: &mut Option<LineReader<R>>) -> io::Result<Option<String>>
where
R: AsyncRead + Unpin,
{
match lines {
Some(lines) => lines.read_line_owned().await,
None => core::future::pending().await,
}
}
fn arm_idle_flush(timer: &mut Option<Pin<Box<Sleep>>>, deadline: Option<Instant>) {
match (timer.as_mut(), deadline) {
(_, None) => *timer = None,
(Some(sleep), Some(at)) if sleep.deadline() == at => {}
(Some(sleep), Some(at)) => sleep.as_mut().reset(at),
(None, Some(at)) => *timer = Some(Box::pin(sleep_until(at))),
}
}
async fn idle_flush_due(timer: &mut Option<Pin<Box<Sleep>>>) {
match timer {
Some(sleep) => sleep.as_mut().await,
None => core::future::pending().await,
}
}
#[cfg(unix)]
fn with_read_buffer<R: AsyncRead>(reader: R) -> BufReader<R> {
BufReader::with_capacity(READ_BUFFER, reader)
}
#[cfg(not(unix))]
fn with_read_buffer<R: AsyncRead>(reader: R) -> BufReader<R> {
BufReader::new(reader)
}
pub(super) struct Streams<O, E> {
pub(super) out: Option<LineReader<O>>,
pub(super) err: Option<LineReader<E>>,
}
#[cfg(unix)]
pub(super) async fn drain_ready<R>(lines: &mut Option<LineReader<R>>, file: &mut LogFile)
where
R: AsyncRead + Unpin,
{
let Some(reader) = lines.as_mut() else {
return;
};
while reader.get_ref().buffer().contains(&b'\n') {
let Ok(Some(line)) = reader.read_line().await else {
return;
};
file.append(line).await;
}
}
async fn final_drain<O, E>(files: &mut LogFiles, streams: &mut Streams<O, E>)
where
O: AsyncRead + Unpin,
E: AsyncRead + Unpin,
{
let drained = timeout(FINAL_DRAIN, async {
while streams.out.is_some() || streams.err.is_some() {
tokio::select! {
result = next_line_ref(&mut streams.out) => {
match result {
Ok(Some(line)) => files.stream(false).append(line).await,
Ok(None) | Err(_) => streams.out = None,
}
}
result = next_line_ref(&mut streams.err) => {
match result {
Ok(Some(line)) => files.stream(true).append(line).await,
Ok(None) | Err(_) => streams.err = None,
}
}
}
}
})
.await;
if drained.is_err() {
tracing::debug!("a pump left the pipe still open at its sheep task's exit");
}
}
#[allow(
clippy::too_many_arguments,
reason = "eight: both streams, both sinks, both channels, the fds, the stamp"
)]
pub(super) fn spawn_log_pump<O, E>(
stdout: Option<O>,
stderr: Option<E>,
out_sink: LogSink,
err_sink: LogSink,
stamped: bool,
logs_tx: mpsc::Sender<LogLine>,
mut ctl_rx: mpsc::Receiver<LogCtl>,
pipes: PipeFds,
) where
O: AsyncRead + Unpin + Send + 'static,
E: AsyncRead + Unpin + Send + 'static,
{
tokio::spawn(async move {
let mut files = LogFiles {
out: LogFile::from_sink(out_sink, stamped).await,
err: LogFile::from_sink(err_sink, stamped).await,
pipes,
#[cfg(unix)]
parked: false,
};
let mut streams = Streams {
out: stdout.map(|reader| LineReader::new(with_read_buffer(reader))),
err: stderr.map(|reader| LineReader::new(with_read_buffer(reader))),
};
let mut idle_flush = None;
while streams.out.is_some() || streams.err.is_some() {
arm_idle_flush(&mut idle_flush, files.flush_deadline());
tokio::select! {
result = next_line(&mut streams.out), if files.reading() => {
let after =
deliver_line(result, false, &mut files, &logs_tx, &mut ctl_rx, &mut streams)
.await;
match after {
AfterLine::KeepReading => {}
AfterLine::StreamEnded => {
streams.out = None;
#[cfg(unix)]
{
files.pipes.out = None;
}
}
AfterLine::LogsClosed => break,
}
}
result = next_line(&mut streams.err), if files.reading() => {
let after =
deliver_line(result, true, &mut files, &logs_tx, &mut ctl_rx, &mut streams)
.await;
match after {
AfterLine::KeepReading => {}
AfterLine::StreamEnded => {
streams.err = None;
#[cfg(unix)]
{
files.pipes.err = None;
}
}
AfterLine::LogsClosed => break,
}
}
ctl = ctl_rx.recv() => {
match ctl {
Some(ctl) => files.serve(ctl, &mut streams).await,
None => break, }
}
() = logs_tx.closed() => break,
() = idle_flush_due(&mut idle_flush) => files.flush_idle().await,
}
}
if files.reading() {
final_drain(&mut files, &mut streams).await;
}
files.flush_idle().await;
});
}
pub(super) fn spawn_stdin_pump<W>(stdin: Option<W>, mut rx: mpsc::Receiver<StdinWrite>)
where
W: AsyncWrite + Unpin + Send + 'static,
{
tokio::spawn(async move {
let Some(mut stdin) = stdin else {
while let Some(StdinWrite { done, .. }) = rx.recv().await {
let _ = done.send(Err(RunnerError::WriteFailed(
"this child has no stdin pipe".to_string(),
)));
}
return;
};
while let Some(StdinWrite { line, done }) = rx.recv().await {
if done.is_closed() {
continue;
}
let mut bytes = line.into_bytes();
bytes.push(b'\n');
let result = match stdin.write_all(&bytes).await {
Ok(()) => stdin.flush().await,
Err(error) => Err(error),
};
let _ = done.send(result.map_err(|error| RunnerError::WriteFailed(error.to_string())));
}
});
}
pub(super) fn spawn_channel_pumps<S>(
daemon_end: S,
from_child_tx: mpsc::Sender<ChildMessage>,
mut to_child_rx: mpsc::Receiver<ShepherdMessage>,
) where
S: AsyncRead + AsyncWrite + Send + Unpin + 'static,
{
let (read_half, mut write_half) = tokio::io::split(daemon_end);
tokio::spawn(async move {
let mut lines = LineReader::new(BufReader::new(read_half));
loop {
match lines.read_line().await {
Ok(Some(line)) => match serde_json::from_str::<ChildMessage>(line) {
Ok(msg) => {
if from_child_tx.send(msg).await.is_err() {
break; }
}
Err(error) => {
tracing::warn!(%line, %error, "malformed shepherd-channel frame");
}
},
Ok(None) => break, Err(error) => {
tracing::error!(%error, "shepherd-channel read failed");
break;
}
}
}
});
tokio::spawn(async move {
while let Some(msg) = to_child_rx.recv().await {
let mut line = match serde_json::to_string(&msg) {
Ok(json) => json,
Err(error) => {
tracing::error!(%error, "shepherd message encode failed");
continue;
}
};
line.push('\n');
if write_half.write_all(line.as_bytes()).await.is_err() {
break; }
}
});
}