tokio_process_tools/
output_stream.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
use crate::collector::{AsyncCollectFn, Collector, Sink};
use crate::inspector::Inspector;
use crate::CollectorError;
use std::error::Error;
use std::fmt::{Debug, Formatter};
use std::future::Future;
use std::io;
use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use thiserror::Error;
use tokio::io::{AsyncBufRead, AsyncBufReadExt, AsyncWriteExt, BufReader, Lines};
use tokio::process::Child;
use tokio::sync::RwLock;
use tokio::task::{AbortHandle, JoinHandle};
use tokio::time::error::Elapsed;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum OutputType {
    StdOut,
    StdErr,
}

pub struct OutputStream {
    ty: OutputType,
    line_follower_task: JoinHandle<()>,
    sender: tokio::sync::broadcast::Sender<Option<String>>,
}

impl Drop for OutputStream {
    fn drop(&mut self) {
        self.line_follower_task.abort();
    }
}

impl Debug for OutputStream {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("OutputStream")
            .field("ty", &self.ty)
            .field(
                "sender",
                &"non-debug < tokio::sync::broadcast::Sender<Option<String>> >",
            )
            .finish()
    }
}

#[derive(Debug, Error)]
pub enum WaitError {
    #[error("A general io error occurred")]
    IoError(#[from] io::Error),

    #[error("Collector failed")]
    CollectorFailed(#[from] CollectorError),
}

pub struct WaitFor {
    task: AbortHandle,
}

impl Drop for WaitFor {
    fn drop(&mut self) {
        self.task.abort();
    }
}

impl OutputStream {
    pub fn ty(&self) -> OutputType {
        self.ty
    }

    pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<Option<String>> {
        self.sender.subscribe()
    }

    #[must_use = "If not at least assigned to a variable, the return value will be dropped immediately, which in turn drops the internal tokio task, meaning that your callback is never called and the inspector effectively dies immediately. You can safely do a `let _inspector = ...` binding to ignore the typical 'unused' warning."]
    pub fn inspect(&self, f: impl Fn(String) + Send + 'static) -> Inspector {
        let (term_sig_tx, mut term_sig_rx) = tokio::sync::oneshot::channel::<()>();

        let mut receiver = self.subscribe();
        let task = tokio::spawn(async move {
            loop {
                tokio::select! {
                    out = receiver.recv() => {
                        match out {
                            Ok(Some(line)) => {
                                f(line);
                            }
                            Ok(None) => {}
                            Err(err) => {
                                tracing::warn!(
                                    error = %err,
                                    "Inspector failed to receive output line"
                                );
                            }
                        }
                    }
                    _msg = &mut term_sig_rx => {
                        break;
                    }
                }
            }
        });
        Inspector {
            task: Some(task),
            task_termination_sender: Some(term_sig_tx),
        }
    }

    // NOTE: We only use Pin<Box> here to force usage of Higher-Rank Trait Bounds (HRTBs).
    // The returned futures will most-likely capture the `&mut T`and are therefore poised
    // by its lifetime. Without the trait-object usage, this would not work.
    #[must_use = "If not at least assigned to a variable, the return value will be dropped immediately, which in turn drops the internal tokio task, meaning that your callback is never called and the inspector effectively dies immediately. You can safely do a `let _inspector = ...` binding to ignore the typical 'unused' warning."]
    pub fn inspect_async<F>(&self, f: F) -> Inspector
    where
        F: Fn(
                String,
            )
                -> Pin<Box<dyn Future<Output = Result<(), Box<dyn Error + Send + Sync>>> + Send>>
            + Send
            + 'static,
    {
        let (term_sig_tx, mut term_sig_rx) = tokio::sync::oneshot::channel::<()>();

        let mut out_receiver = self.subscribe();
        let capture = tokio::spawn(async move {
            loop {
                tokio::select! {
                    out = out_receiver.recv() => {
                        match out {
                            Ok(Some(line)) => {
                                let result = f(line).await;
                                match result {
                                    Ok(()) => { /* do nothing */ }
                                    Err(err) => {
                                        tracing::warn!(?err, "Inspection failed")
                                    }
                                }
                            }
                            Ok(None) => {}
                            Err(err) => {
                                tracing::warn!(
                                    error = %err,
                                    "Inspector failed to receive output line"
                                );
                            }
                        }
                    }
                    _msg = &mut term_sig_rx => {
                        break;
                    }
                }
            }
        });

        Inspector {
            task: Some(capture),
            task_termination_sender: Some(term_sig_tx),
        }
    }

    #[must_use = "If not at least assigned to a variable, the return value will be dropped immediately, which in turn drops the internal tokio task, meaning that your callback is never called and the collector effectively dies immediately. You can safely do a `let _collector = ...` binding to ignore the typical 'unused' warning."]
    pub fn collect<S: Sink>(
        &self,
        into: S,
        collect: impl Fn(String, &mut S) + Send + 'static,
    ) -> Collector<S> {
        let target = Arc::new(RwLock::new(into));

        let (t_send, mut t_recv) = tokio::sync::oneshot::channel::<()>();

        let mut out_receiver = self.subscribe();
        let capture = tokio::spawn(async move {
            loop {
                tokio::select! {
                    out = out_receiver.recv() => {
                        match out {
                            Ok(Some(line)) => {
                                let mut write_guard = target.write().await;
                                collect(line, &mut (*write_guard));
                            }
                            Ok(None) => {}
                            Err(err) => {
                                tracing::warn!(
                                    error = %err,
                                    "Collector failed to receive output line"
                                );
                            }
                        }
                    }
                    _msg = &mut t_recv => {
                        tracing::info!("Terminating collector!");
                        break;
                    }
                }
            }
            Arc::try_unwrap(target).expect("single owner").into_inner()
        });

        Collector {
            task: Some(capture),
            task_termination_sender: Some(t_send),
        }
    }

    // NOTE: We only use Pin<Box> here to force usage of Higher-Rank Trait Bounds (HRTBs).
    // The returned futures will most-likely capture the `&mut T`and are therefore poised
    // by its lifetime. Without the trait-object usage, this would not work.
    #[must_use = "If not at least assigned to a variable, the return value will be dropped immediately, which in turn drops the internal tokio task, meaning that your callback is never called and the collector effectively dies immediately. You can safely do a `let _collector = ...` binding to ignore the typical 'unused' warning."]
    pub fn collect_async<S, F>(&self, into: S, collect: F) -> Collector<S>
    where
        S: Sink,
        F: Fn(String, &mut S) -> AsyncCollectFn<'_> + Send + 'static,
    {
        let target = Arc::new(RwLock::new(into));

        let (term_sig_tx, mut term_sig_rx) = tokio::sync::oneshot::channel::<()>();

        let mut out_receiver = self.subscribe();
        let capture = tokio::spawn(async move {
            loop {
                tokio::select! {
                    out = out_receiver.recv() => {
                        match out {
                            Ok(Some(line)) => {
                                let result = {
                                    let mut write_guard = target.write().await;
                                    collect(line, &mut *write_guard).await
                                };
                                match result {
                                    Ok(()) => { /* do nothing */ }
                                    Err(err) => {
                                        tracing::warn!(?err, "Collecting failed")
                                    }
                                }
                            }
                            Ok(None) => {}
                            Err(err) => {
                                tracing::warn!(
                                    error = %err,
                                    "Collector failed to receive output line"
                                );
                            }
                        }
                    }
                    _msg = &mut term_sig_rx => {
                        break;
                    }
                }
            }
            Arc::try_unwrap(target).expect("single owner").into_inner()
        });

        Collector {
            task: Some(capture),
            task_termination_sender: Some(term_sig_tx),
        }
    }

    #[must_use = "If not at least assigned to a variable, the return value will be dropped immediately, which in turn drops the internal tokio task, meaning that your callback is never called and the collector effectively dies immediately. You can safely do a `let _collector = ...` binding to ignore the typical 'unused' warning."]
    pub fn collect_into_vec(&self) -> Collector<Vec<String>> {
        self.collect(Vec::new(), |line, vec| vec.push(line))
    }

    #[must_use = "If not at least assigned to a variable, the return value will be dropped immediately, which in turn drops the internal tokio task, meaning that your callback is never called and the collector effectively dies immediately. You can safely do a `let _collector = ...` binding to ignore the typical 'unused' warning."]
    pub fn collect_into_write<W: Sink + AsyncWriteExt + Unpin>(&self, write: W) -> Collector<W> {
        self.collect_async(write, move |line, write| {
            Box::pin(async move {
                write.write_all(line.as_bytes()).await?;
                write.write_all("\n".as_bytes()).await?;
                Ok(())
            })
        })
    }

    #[must_use = "If not at least assigned to a variable, the return value will be dropped immediately, which in turn drops the internal tokio task, meaning that your callback is never called and the collector effectively dies immediately. You can safely do a `let _collector = ...` binding to ignore the typical 'unused' warning."]
    pub fn collect_into_write_mapped<W: Sink + AsyncWriteExt + Unpin, B: AsRef<[u8]> + Send>(
        &self,
        write: W,
        mapper: impl Fn(String) -> B + Send + Sync + Copy + 'static,
    ) -> Collector<W> {
        self.collect_async(write, move |line, write| {
            Box::pin(async move {
                let mapped = mapper(line);
                let mapped = mapped.as_ref();
                write.write_all(mapped).await?;
                Ok(())
            })
        })
    }

    /// This function is cancel safe.
    pub async fn wait_for(&self, predicate: impl Fn(String) -> bool + Send + 'static) -> WaitFor {
        let mut receiver = self.subscribe();
        let jh = tokio::spawn(async move {
            loop {
                if let Ok(Some(line)) = receiver.recv().await {
                    if predicate(line) {
                        break;
                    }
                }
            }
        });
        let result = WaitFor {
            task: jh.abort_handle(),
        };
        let _ = jh.await;
        result
    }

    /// This function is cancel safe.
    pub async fn wait_for_with_timeout(
        &self,
        predicate: impl Fn(String) -> bool + Send + 'static,
        timeout: Duration,
    ) -> Result<WaitFor, Elapsed> {
        tokio::time::timeout(timeout, self.wait_for(predicate)).await
    }
}

pub(crate) fn extract_output_streams(
    mut child: Child,
    stdout_channel_capacity: usize,
    stderr_channel_capacity: usize,
) -> (Child, OutputStream, OutputStream) {
    let stdout = child
        .stdout
        .take()
        .expect("Child process stdout not captured");
    let stderr = child
        .stderr
        .take()
        .expect("Child process stderr not captured");

    fn follow_lines_with_background_task<B: AsyncBufRead + Unpin + Send + 'static>(
        mut lines: Lines<B>,
        sender: tokio::sync::broadcast::Sender<Option<String>>,
    ) -> JoinHandle<()> {
        tokio::spawn(async move {
            loop {
                match lines.next_line().await {
                    Ok(maybe_line) => {
                        let is_none = maybe_line.is_none();
                        match sender.send(maybe_line) {
                            Ok(_received_by) => {}
                            Err(err) => {
                                // All receivers already dropped.
                                // We intentionally ignore these errors.
                                // If they occur, the user wasn't interested in seeing this message.
                                // We won't store it (history) to later feed it back to a new subscriber.
                                tracing::debug!(
                                    error = %err,
                                    "No active receivers for output line, dropping it"
                                );
                            }
                        }
                        if is_none {
                            break;
                        }
                    }
                    Err(err) => panic!("Could not read from stream: {err}"),
                }
            }
        })
    }

    // Intentionally dropping the initial subscribers.
    // This will potentially lead to the (ignored) errors in follow_lines_with_background_task.
    let (tx_stdout, _rx_stdout) =
        tokio::sync::broadcast::channel::<Option<String>>(stdout_channel_capacity);
    let (tx_stderr, _rx_stderr) =
        tokio::sync::broadcast::channel::<Option<String>>(stderr_channel_capacity);

    let stdout_jh =
        follow_lines_with_background_task(BufReader::new(stdout).lines(), tx_stdout.clone());
    let stderr_jh =
        follow_lines_with_background_task(BufReader::new(stderr).lines(), tx_stderr.clone());

    (
        child,
        OutputStream {
            ty: OutputType::StdOut,
            line_follower_task: stdout_jh,
            sender: tx_stdout,
        },
        OutputStream {
            ty: OutputType::StdErr,
            line_follower_task: stderr_jh,
            sender: tx_stderr,
        },
    )
}

#[cfg(test)]
mod tests {
    use crate::ProcessHandle;

    #[tokio::test]
    async fn collect_into_write() {
        let cmd = tokio::process::Command::new("ls");

        let mut handle = ProcessHandle::spawn("ls", cmd).unwrap();

        let file1 = tokio::fs::File::create(
            std::env::temp_dir()
                .join("tokio_process_tools_test_write_with_predefined_collector_1.txt"),
        )
        .await
        .unwrap();
        let file2 = tokio::fs::File::create(
            std::env::temp_dir()
                .join("tokio_process_tools_test_write_with_predefined_collector_2.txt"),
        )
        .await
        .unwrap();

        let collector1 = handle.stdout().collect_into_write(file1);

        let collector2 = handle
            .stdout()
            .collect_into_write_mapped(file2, |line| format!("ok-{}", line));

        let _exit_status = handle.wait().await.unwrap();
        let _file = collector1.abort().await.unwrap();
        let _file = collector2.abort().await.unwrap();
    }
}