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
use std::{io::prelude::*, os::unix::net::UnixStream, time::Duration};
use serde::{Deserialize, Serialize};
use crate::config;
#[derive(Serialize, Deserialize, Debug)]
pub(crate) enum IPCCommand {
Restart {
process_name: Option<String>,
directory: String,
},
Connect {
process_name: Option<String>,
directory: String,
},
Stop {
app_name: Option<String>,
directory: String,
},
Ping,
}
impl IPCCommand {
pub fn restart_command(process_name: Option<String>, directory: String) -> Self {
Self::Restart {
process_name,
directory,
}
}
pub fn connect_command(process_name: Option<String>, directory: String) -> Self {
Self::Connect {
process_name,
directory,
}
}
pub fn stop_command(app_name: Option<String>, directory: String) -> Self {
Self::Stop {
app_name,
directory,
}
}
pub fn heartbeat_command() -> Self {
Self::Ping
}
}
pub fn ping_server() -> color_eyre::Result<String> {
let mut socket = UnixStream::connect(config::socket_path())?;
let timeout = Some(Duration::from_secs(1));
socket.set_read_timeout(timeout)?;
socket.set_write_timeout(timeout)?;
let command = IPCCommand::heartbeat_command();
serde_json::to_writer(&socket, &command)?;
socket.write_all(b"\n")?;
socket.flush()?;
let mut response = String::new();
socket.read_to_string(&mut response)?;
Ok(response)
}