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
use crate::runtime::job::task::log_monitor::message::{LogMonitorCmd, LogMonitorMessage};
use anyhow::{bail, Context, Result};
use crossbeam_channel::{unbounded, Receiver, Select, Sender};
use log::{error, info};
use std::{io, process::Child, string::FromUtf8Error, thread::spawn};
#[derive(Debug)]
pub enum PipeError {
IO(io::Error),
NotUtf8(FromUtf8Error),
}
#[derive(Debug)]
pub enum PipedLine {
Line(String),
Eof,
}
#[derive(Debug)]
pub struct PipeStreamReader {
pub lines: Receiver<Result<PipedLine, PipeError>>,
}
impl PipeStreamReader {
pub fn init(mut stream: Box<dyn io::Read + Send>) -> Self {
Self {
lines: {
let (tx, rx) = unbounded();
spawn(move || {
let mut buf = Vec::new();
let mut byte = [0u8];
loop {
match stream.read(&mut byte) {
Ok(0) => {
if tx.send(Ok(PipedLine::Eof)).is_err() {}
break;
}
Ok(_) => {
if byte[0] == 0x0A {
if let Err(error) =
tx.send(match String::from_utf8(buf.clone()) {
Ok(line) => Ok(PipedLine::Line(line)),
Err(err) => Err(PipeError::NotUtf8(err)),
})
{
error!("Output stream decoding error: {:#?}", error);
}
buf.clear();
} else {
buf.push(byte[0]);
}
}
Err(error) => {
if let Err(error) = tx.send(Err(PipeError::IO(error))) {
error!("Output stream error: {:#?}", error);
}
}
}
}
});
rx
},
}
}
pub fn stream_child_output(
child: &mut Child,
log_monitor_senders: &[Sender<LogMonitorMessage>],
) -> Result<()> {
let channels = vec![
Self::init(Box::new(
child
.stdout
.take()
.context("Error building stdout channel")?,
)),
Self::init(Box::new(
child
.stderr
.take()
.context("Error building stderr channel")?,
)),
];
let mut select = Select::new();
for channel in &channels {
select.recv(&channel.lines);
}
let mut stream_eof = false;
while !stream_eof {
let operation = select.select();
let index = operation.index();
let received = operation.recv(
channels
.get(index)
.context("Error selecting stream channel")?
.map(|channel| &channel.lines),
);
if let Ok(remote_result) = received {
match remote_result {
Ok(piped_line) => match piped_line {
PipedLine::Line(line) => {
if index == 0 {
info!("{}", line);
} else {
error!("{}", line);
}
for sender in log_monitor_senders.iter() {
if sender
.send(
LogMonitorMessage::new()
.cmd(LogMonitorCmd::Log)
.message(line.clone()),
)
.is_err()
{
bail!("Error sending process log message to log monitor");
}
}
}
PipedLine::Eof => {
stream_eof = true;
select.remove(index);
}
},
Err(error) => {
error!("Error streaming process output: {:?}", error);
}
}
} else {
stream_eof = true;
select.remove(index);
}
}
Ok(())
}
pub fn map<'a, F, B>(&'a self, map_fn: F) -> B
where
F: Fn(&'a Self) -> B,
{
map_fn(self)
}
}