Skip to main content

ralph_workflow/pipeline/idle_timeout/
readers.rs

1use super::{touch_activity, SharedActivityTimestamp};
2
3/// A reader wrapper that updates an activity timestamp on every read.
4///
5/// Wraps any `Read` implementation and updates a shared atomic timestamp
6/// whenever data is successfully read. This allows external monitoring of
7/// read activity for idle timeout detection.
8///
9/// Used for both stdout and stderr tracking — any output from either stream
10/// prevents idle timeout kills.
11pub struct ActivityTrackingReader<R: std::io::Read> {
12    inner: R,
13    activity_timestamp: SharedActivityTimestamp,
14}
15
16impl<R: std::io::Read> ActivityTrackingReader<R> {
17    /// Create a new activity-tracking reader.
18    ///
19    /// The provided timestamp will be updated to the current time
20    /// whenever data is successfully read from the inner reader.
21    pub fn new(inner: R, activity_timestamp: SharedActivityTimestamp) -> Self {
22        touch_activity(&activity_timestamp);
23        Self {
24            inner,
25            activity_timestamp,
26        }
27    }
28}
29
30impl<R: std::io::Read> std::io::Read for ActivityTrackingReader<R> {
31    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
32        let n = std::io::Read::read(&mut self.inner, buf)?;
33        if n > 0 {
34            touch_activity(&self.activity_timestamp);
35        }
36        Ok(n)
37    }
38}