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
use log::*;
use std::fs::{File, OpenOptions};
use std::mem;
use std::process::{Child, Command, Stdio};
use crate::config::SubprocessCommand;
pub struct SubprocessHandle(Vec<(SubprocessCommand, Option<Child>)>);
impl Drop for SubprocessHandle {
fn drop(&mut self) {
let mut remaining = Vec::new();
for (sc, subopt) in self.0.drain(..) {
let mut sub = match subopt {
None => continue,
Some(s) => s
};
info!("Killing process: {}", sc.name);
match sub.kill() {
Ok(_) => {},
Err(_) => error!("Failed to kill {}", sc.name),
}
remaining.push((sc.name, sub));
}
let mut attempts = 10;
while attempts > 0 && !remaining.is_empty() {
let mut temp = mem::take(&mut remaining);
for (name, mut sub) in temp.drain(..) {
match sub.try_wait() {
Ok(Some(st)) => {
info!("{} exited with status: {}", name, st)
},
_ => {
debug!("Still waiting on {}", name);
remaining.push((name, sub));
}
}
}
attempts -= 1;
}
if !remaining.is_empty() {
for (name, _) in &remaining {
error!("Failed to kill {}", name);
}
}
}
}
fn launch_child(sub: &SubprocessCommand) -> Child {
let stdin = match sub.stdin.as_ref() {
None => { Stdio::null() },
Some(f) => { File::open(f)
.expect(&format!("Failed to open {}", f))
.into() }
};
let stdout = match sub.stdout.as_ref() {
None => { Stdio::inherit() },
Some(f) =>
OpenOptions::new()
.create(true)
.append(true)
.open(f)
.expect(&format!("Failed to open {}", f))
.into(),
};
let stderr = match sub.stderr.as_ref() {
None => { Stdio::inherit() },
Some(f) =>
OpenOptions::new()
.create(true)
.append(true)
.open(f)
.expect(&format!("Failed to open {}", f))
.into(),
};
Command::new(&sub.command[0])
.args(&sub.command[1..])
.stdin(stdin)
.stdout(stdout)
.stderr(stderr)
.spawn()
.expect(&format!("Failed to start {}", sub.name))
}
impl SubprocessHandle {
pub fn new(procs: Vec<SubprocessCommand>) -> Self {
let mut res = Vec::new();
for sub in procs {
let ch = launch_child(&sub);
res.push((sub, Some(ch)))
}
SubprocessHandle(res)
}
pub fn check_children(&mut self) {
let mut children = mem::take(&mut self.0);
for (sc, child) in children.drain(..) {
match child {
None => self.0.push((sc, None)),
Some(mut c) => {
match c.try_wait() {
Ok(Some(status)) => {
warn!("Child {} exited with status {}",
sc.name, status);
if sc.reload {
warn!("Restarting {}", sc.name);
let ch = launch_child(&sc);
self.0.push((sc, Some(ch)))
} else {
self.0.push((sc, None))
}
},
Ok(None) => {
self.0.push((sc, Some(c)));
},
Err(e) => {
warn!("Could not wait on child {}: {}",
sc.name, e);
self.0.push((sc, Some(c)));
}
}
}
}
}
}
}