Send email only if there are two successive errors
authorGreg Burri <greg.burri@gmail.com>
Fri, 24 Nov 2023 08:17:48 +0000 (09:17 +0100)
committerGreg Burri <greg.burri@gmail.com>
Fri, 24 Nov 2023 08:17:48 +0000 (09:17 +0100)
src/main.rs

index 08039fb..60d0724 100644 (file)
@@ -24,6 +24,7 @@ mod config;
 const FILE_CONF: &str = "config.ron";
 const CHECK_PERIOD: Duration = Duration::from_secs(10); // 10 s.
 const PING_TIMEOUT: Duration = Duration::from_secs(10); // 10 s.
+const NUMBER_SUCCESSIVE_ERRORS_SEND_EMAIL: i32 = 2; // Send an email after 2 successive errors.
 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/";
@@ -45,7 +46,7 @@ fn main() -> Result<()> {
 
     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;
+    let mut error_count = 0; // Number of successive errors.
 
     loop {
         let time_beginning_loop = time::Instant::now();
@@ -54,9 +55,11 @@ fn main() -> Result<()> {
             .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 {
+            error_count += 1;
+            println!("Error (error_count={}): {:?}", error_count, error);
+            if error_count >= NUMBER_SUCCESSIVE_ERRORS_SEND_EMAIL
+                && time::Instant::now() - time_last_email_send >= EMAIL_RESEND_PERIOD
+            {
                 // Send e-mail.
                 println!("Sending email...");
                 match send_email(
@@ -74,8 +77,8 @@ fn main() -> Result<()> {
                 }
             }
         } else {
-            if error_state {
-                error_state = false;
+            if error_count > 0 {
+                error_count = 0;
                 println!("End of erroneous state");
             }