From c39054926459bd77c7d2f16d1454d544bd118b98 Mon Sep 17 00:00:00 2001 From: Greg Burri Date: Mon, 2 Oct 2023 23:21:09 +0200 Subject: [PATCH] Check the alive UDP socket and send an email if there is an error. --- src/main.rs | 47 ++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 44 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index 84dd95c..49668d0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,8 @@ */ use std::{ + net::UdpSocket, + sync::{Arc, Mutex}, thread, time::{self, Duration}, }; @@ -17,11 +19,11 @@ use serde::Deserialize; use crate::config::Config; mod config; -// mod error; const FILE_CONF: &str = "config.ron"; const CHECK_PERIOD: Duration = Duration::from_secs(10); // 10 s. -const EMAIL_RESEND_PERIOD: Duration = Duration::from_secs(6 * 60 * 60); // 6 h. +const PING_TIMEOUT: Duration = Duration::from_secs(10); // 10 s. +const EMAIL_RESEND_PERIOD: Duration = Duration::from_secs(2 * 60 * 60); // 2 h. const STATE_PRINT_PERIOD: Duration = Duration::from_secs(15 * 60); // 15 min. const BASE_URI: &str = "http://localhost:5052/eth/v1/"; @@ -38,6 +40,8 @@ fn main() -> Result<()> { } ); + let check_alive_error_mutex = start_check_alive_thread(); + let mut time_last_email_send = time::Instant::now() - EMAIL_RESEND_PERIOD; let mut time_last_state_printed = time::Instant::now() - STATE_PRINT_PERIOD; let mut error_state = false; @@ -45,7 +49,10 @@ fn main() -> Result<()> { loop { let time_beginning_loop = time::Instant::now(); - if let Err(error) = check_validators(&config.pub_keys) { + if let Err(error) = check_validators(&config.pub_keys) + .as_ref() + .and(check_alive_error_mutex.lock().unwrap().as_ref()) + { error_state = true; println!("Error: {:?}", error); if time::Instant::now() - time_last_email_send >= EMAIL_RESEND_PERIOD { @@ -95,6 +102,7 @@ enum CheckError { ReqwestError(reqwest::Error), ValidatorError { pub_key: String, message: String }, ValidatorStatusError { pub_key: String, message: String }, + CheckAlive, } impl From for CheckError { @@ -207,3 +215,36 @@ fn send_email(title: &str, body: &str, login: &str, pass: &str) -> Result<()> { Ok(()) } + +fn start_check_alive_thread() -> Arc>> { + let check_alive_error_mutex: Arc>> = + Arc::new(Mutex::new(Ok(()))); + let check_alive_error_mutex_copy = check_alive_error_mutex.clone(); + + let _thread_check_alive_handle = thread::spawn(move || { + let socket = UdpSocket::bind("0.0.0.0:8739").unwrap(); + socket.set_read_timeout(Some(PING_TIMEOUT)).unwrap(); + + let mut buffer = [0u8; 8]; + + loop { + match socket.recv_from(&mut buffer) { + Ok((size, src)) => { + let mut check_alive_error = check_alive_error_mutex.lock().unwrap(); + if size == 8 { + *check_alive_error = Ok(()); + socket.send_to(&buffer, &src).unwrap(); + } else { + *check_alive_error = Err(CheckError::CheckAlive); + } + } + Err(_error) => { + let mut check_alive_error = check_alive_error_mutex.lock().unwrap(); + *check_alive_error = Err(CheckError::CheckAlive); + } + } + } + }); + + check_alive_error_mutex_copy +} -- 2.45.2