use std::path::Path;
use shep_core::logtail::read_tail;
use shep_core::protocol::{LogStream, LogTail, ProcessInfo, Response, RpcError, RpcErrorCode};
use super::context::RpcContext;
use super::error::rpc_error;
use super::selector_verbs::not_found;
pub(super) async fn answer(
id: u32,
stream: LogStream,
lines: u32,
ctx: &RpcContext,
) -> Result<Response, RpcError> {
let rows = ctx
.supervisor
.list_checked()
.await
.map_err(|err| rpc_error(&err))?;
let row = rows.iter().find(|row| row.id == id).ok_or_else(not_found)?;
tail_row(row, stream, lines).await.map(Response::LogTail)
}
pub(super) async fn tail_row(
row: &ProcessInfo,
stream: LogStream,
lines: u32,
) -> Result<LogTail, RpcError> {
let recorded = match stream {
LogStream::Out => &row.out_file,
LogStream::Err => &row.err_file,
_ => &None,
};
let Some(path) = recorded.clone() else {
return Ok(LogTail::NoPath);
};
let stamped = row.log_timestamps.unwrap_or(true);
let limit = usize::try_from(lines).unwrap_or(usize::MAX);
let read = tokio::task::spawn_blocking({
let path = path.clone();
move || read_tail(Path::new(&path), limit, stamped)
})
.await
.map_err(|err| RpcError {
code: RpcErrorCode::Internal,
message: format!("the log read did not complete ({err})"),
daemon_version: None,
})?;
Ok(match read {
Ok((lines, truncated)) => LogTail::Lines { lines, truncated },
Err(err) if err.kind() == std::io::ErrorKind::NotFound => LogTail::Missing { path },
Err(err) => LogTail::Unreadable {
path,
reason: err.to_string(),
},
})
}