-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rs
85 lines (70 loc) · 2.69 KB
/
main.rs
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
//! Safe-sync server
extern crate wavesexchange_log as log;
extern crate wavesexchange_warp as wx_warp;
use std::sync::Arc;
use tokio::{
signal::unix::{signal, SignalKind},
sync::{mpsc, oneshot},
};
mod metrics;
mod server;
#[tokio::main]
async fn main() -> Result<(), anyhow::Error> {
env_logger::init();
// Load configs
let config = server::config::load()?;
// Create the web server
use server::builder::ServerBuilder;
let server = ServerBuilder::new()
.port(config.port)
.metrics_port(config.metrics_port)
.build()
.new_server();
let server = Arc::new(server);
// Run the web server
let (shutdown_signal_tx, mut shutdown_signal_rx) = mpsc::channel(1);
let (server_task, server_stop_tx) = server.clone().start(shutdown_signal_tx);
let server_handle = tokio::spawn(server_task);
// Graceful shutdown handling
let (shutdown_start_tx, shutdown_start_rx) = oneshot::channel();
let mut shutdown_start_tx = Some(shutdown_start_tx);
let mut graceful_shutdown_handle = tokio::spawn(async move {
if shutdown_start_rx.await.is_ok() {
log::debug!("Graceful shutdown started: disconnecting all clients");
server.disconnect_all_clients().await;
}
});
let mut sigterm_stream = signal(SignalKind::terminate()).expect("sigterm stream");
loop {
tokio::select! {
// On SIGTERM initiate graceful shutdown (subsequent SIGTERM will terminate server immediately)
_ = sigterm_stream.recv() => {
log::info!("got SIGTERM - shutting down gracefully");
if let Some(tx) = shutdown_start_tx.take() { // first SIGTERM
let _ = tx.send(()); // start graceful shutdown
} else { // subsequent SIGTERM
break; // terminate server immediately
}
}
// On SIGINT terminate server immediately
_ = tokio::signal::ctrl_c() => {
log::info!("got SIGINT - terminating immediately");
break; // terminate server
}
// When graceful shutdown handler finishes terminate the server
_ = &mut graceful_shutdown_handle => {
log::debug!("Graceful shutdown finished");
break; // terminate server
}
}
}
// Send stop signal to all websocket connection handlers
log::trace!("terminating ws connection handlers");
shutdown_signal_rx.close();
// Send stop signal to the web server
log::trace!("terminating ws server");
let _ = server_stop_tx.send(());
server_handle.await?;
log::info!("Server terminated");
Ok(())
}