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
use crate::error::*;
use chrono::{DateTime, Utc};
use log::*;
use std::os::unix::process::ExitStatusExt;
use std::process::{Child, ExitStatus};
use std::sync::{Arc, Mutex};
#[derive(Clone, Debug, GraphQLObject)]
pub struct MonitorEntry {
pub name: String,
pub version: String,
pub start_time: DateTime<Utc>,
pub end_time: Option<DateTime<Utc>>,
pub running: bool,
pub pid: Option<i32>,
pub last_rc: Option<i32>,
pub last_signal: Option<i32>,
pub args: Option<Vec<String>>,
pub config: String,
}
pub fn find_running(
registry: &Arc<Mutex<Vec<MonitorEntry>>>,
name: &str,
) -> Result<Option<MonitorEntry>, AppError> {
let entries = registry.lock().map_err(|err| AppError::MonitorError {
err: format!("Failed get entries mutex: {:?}", err),
})?;
Ok(entries.iter().find_map(|entry| {
if entry.name == name && entry.running {
Some(entry.clone())
} else {
None
}
}))
}
pub fn monitor_app(
registry: Arc<Mutex<Vec<MonitorEntry>>>,
mut process_handle: Child,
name: &str,
version: &str,
) -> Result<(), AppError> {
let status = process_handle
.wait()
.map_err(|err| AppError::MonitorError {
err: format!("Failed to wait for {} to finish: {:?}", name, err),
})?;
finish_entry(®istry, &name, &version, status)
}
pub fn start_entry(
registry: &Arc<Mutex<Vec<MonitorEntry>>>,
new_entry: &MonitorEntry,
) -> Result<(), AppError> {
let mut entries = registry.lock().map_err(|err| AppError::MonitorError {
err: format!(
"Failed to add {} to monitoring. Couldn't get entries mutex: {:?}",
new_entry.name, err
),
})?;
if let Some(index) = entries
.iter()
.position(|ref e| e.name == new_entry.name && e.version == new_entry.version)
{
debug!(
"Updating existing entry for {} {}",
new_entry.name, new_entry.version
);
entries[index] = (*new_entry).clone();
} else {
debug!("Adding new entry: {:?}", new_entry);
entries.push((*new_entry).clone());
}
Ok(())
}
pub fn finish_entry(
registry: &Arc<Mutex<Vec<MonitorEntry>>>,
name: &str,
version: &str,
status: ExitStatus,
) -> Result<(), AppError> {
let mut last_rc = None;
let mut last_signal = None;
let end_time = Utc::now();
if status.success() {
info!("App {} completed successfully", name);
last_rc = Some(0);
} else if let Some(code) = status.code() {
error!("App {} failed. RC: {}", name, code);
last_rc = Some(code);
} else if let Some(signal) = status.signal() {
warn!("App {} terminated by signal {}", name, signal);
last_signal = Some(signal);
} else {
warn!("App {} terminated for unknown reasons", name);
}
let mut entries = registry.lock().map_err(|err| AppError::MonitorError {
err: format!(
"Failed to remove {} from monitoring. Couldn't get entries mutex: {:?}",
name, err
),
})?;
if let Some(index) = entries
.iter()
.position(|ref e| e.name == name && e.version == version)
{
entries[index].running = false;
entries[index].last_rc = last_rc;
entries[index].last_signal = last_signal;
entries[index].pid = None;
entries[index].end_time = Some(end_time);
} else {
warn!("Unable to find entry for {} {}", name, version);
}
Ok(())
}
pub fn remove_entry(
registry: &Arc<Mutex<Vec<MonitorEntry>>>,
name: &str,
version: &str,
) -> Result<(), AppError> {
let mut entries = registry.lock().map_err(|err| AppError::MonitorError {
err: format!(
"Failed to remove {} from monitoring. Couldn't get entries mutex: {:?}",
name, err
),
})?;
if let Some(index) = entries
.iter()
.position(|ref e| e.name == name && e.version == version)
{
entries.remove(index);
}
Ok(())
}
pub fn remove_entries(
registry: &Arc<Mutex<Vec<MonitorEntry>>>,
name: &str,
) -> Result<(), AppError> {
let mut entries = registry.lock().map_err(|err| AppError::MonitorError {
err: format!(
"Failed to remove {} from monitoring. Couldn't get entries mutex: {:?}",
name, err
),
})?;
entries.retain(|entry| entry.name != name);
Ok(())
}