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
|
use serde::{Deserialize, Serialize};
use tokio::{
io::{AsyncBufReadExt, AsyncWriteExt, BufReader},
process::{Child, Command},
sync::{
Mutex,
mpsc::{Receiver, channel},
},
task::spawn,
};
use std::{path::PathBuf, process::Stdio, sync::Arc};
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct ServiceConf {
pub name: String,
pub command: String,
pub args: Option<String>,
pub directory: Option<PathBuf>,
pub autostart: bool,
}
impl Default for ServiceConf {
fn default() -> Self {
Self::new()
}
}
impl ServiceConf {
/**
* Returns a new empty `ServiceConf`
*/
pub fn new() -> Self {
Self {
name: String::new(),
command: String::new(),
args: None,
directory: None,
autostart: false,
}
}
/**
* Returns a new `ServiceConf` from parts.
*/
pub fn from_parts(
name: String,
command: String,
args: Option<String>,
directory: Option<PathBuf>,
autostart: bool,
) -> Self {
Self {
name,
command,
args,
directory,
autostart,
}
}
}
#[derive(Debug)]
pub struct Service {
conf: ServiceConf,
proc: Option<Arc<Mutex<Child>>>,
stdout: Option<Arc<Mutex<Receiver<String>>>>,
stderr: Option<Arc<Mutex<Receiver<String>>>>,
}
impl Default for Service {
fn default() -> Self {
Self::new()
}
}
impl Service {
/**
* Returns a new empty `Service`
*/
pub fn new() -> Self {
Self {
conf: ServiceConf::default(),
proc: None,
stdout: None,
stderr: None,
}
}
/**
* Returns a `Service` made from a `ServiceConf`.
*/
pub fn from_conf(conf: &ServiceConf) -> Self {
Self {
conf: conf.clone(),
proc: None,
stdout: None,
stderr: None,
}
}
/**
* Returns the name of the service
*/
pub async fn name(&self) -> &str {
&self.conf.name
}
/**
* Uses `tokio::process::Command` to start the service.
*/
pub async fn start(&mut self) -> Result<(), Box<dyn std::error::Error>> {
if self.proc.is_some() {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::AlreadyExists,
"Process Already Exists",
)));
}
let cmd = &self.conf.command;
let args = if let Some(a) = &self.conf.args {
a.split_whitespace()
} else {
"".split_whitespace()
};
let cwd = if let Some(c) = &self.conf.directory {
c
} else {
&PathBuf::from("/")
};
let child = Command::new(cmd)
.args(args)
.current_dir(cwd)
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.stderr(Stdio::piped())
.spawn()?;
self.proc = Some(Arc::new(Mutex::new(child)));
Ok(())
}
/**
* Returns true when process is started and false when process is stopped.
*/
pub async fn started(&self) -> bool {
self.proc.is_some()
}
/**
* Invokes kill on the service process
*/
pub async fn stop(&mut self) -> Result<(), Box<dyn std::error::Error>> {
if let Some(proc) = self.proc.clone() {
let mut lock = proc.lock().await;
lock.kill().await?;
drop(lock);
self.proc = None;
Ok(())
} else {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No Process Associated with Service",
)))
}
}
/**
* Restarts service process
*/
pub async fn restart(&mut self) -> Result<(), Box<dyn std::error::Error>> {
self.stop().await?;
self.start().await?;
Ok(())
}
/**
* Takes control of service process' stdout file handle and spawns a new task to continuously
* scan it.
*/
pub async fn scan_stdout(&mut self) -> Result<(), Box<dyn std::error::Error>> {
if let Some(proc) = self.proc.clone() {
let mut lock = proc.lock().await;
let stdout = if let Some(stdout) = lock.stdout.take() {
stdout
} else {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No stdout handle associated with process",
)));
};
drop(lock);
let (tx, rx) = channel(100);
let sname = self.conf.name.clone();
spawn(async move {
let mut br = BufReader::new(stdout).lines();
while let Ok(Some(line)) = br.next_line().await {
println!("{} :: {}", &sname, &line);
if let Err(_) = tx.send(line).await {
return;
};
}
});
self.stdout = Some(Arc::new(Mutex::new(rx)));
Ok(())
} else {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No Process Associated with Service",
)))
}
}
/**
* Takes control of service process' stderr file handle and spawns a new task to continuously
* scan it.
*/
pub async fn scan_stderr(&mut self) -> Result<(), Box<dyn std::error::Error>> {
if let Some(proc) = self.proc.clone() {
let mut lock = proc.lock().await;
let stderr = if let Some(stderr) = lock.stderr.take() {
stderr
} else {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No stderr handle associated with process",
)));
};
drop(lock);
let (tx, rx) = channel(100);
let sname = self.conf.name.clone();
spawn(async move {
let mut br = BufReader::new(stderr).lines();
while let Ok(Some(line)) = br.next_line().await {
println!("ERR :: {} :: {}", &sname, &line);
if let Err(_) = tx.send(line).await {
return;
};
}
});
self.stderr = Some(Arc::new(Mutex::new(rx)));
Ok(())
} else {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No Process Associated with Service",
)))
}
}
/**
* Writes to the service process' stdin, if it exists.
*/
pub async fn write_stdin(&mut self, buf: String) -> Result<(), Box<dyn std::error::Error>> {
if let Some(proc) = self.proc.clone() {
let mut lock = proc.lock().await;
let stdin = if let Some(stdin) = lock.stdin.as_mut() {
stdin
} else {
return Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No stdin handle associated with process",
)));
};
stdin.write(&buf.as_bytes()).await?;
Ok(())
} else {
Err(Box::new(std::io::Error::new(
std::io::ErrorKind::NotFound,
"No Process Associated with Service",
)))
}
}
}
|