Skip to content

Do not send email too frequently #1994

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 25, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion util/logger/src/logger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@

use std::env;
use std::thread;
use std::time::{Duration, Instant};
use time;

use atty;
use colored::Colorize;
use env_logger::filter::{Builder as FilterBuilder, Filter};
use log::{Level, LevelFilter, Log, Metadata, Record};
use parking_lot::Mutex;

use crate::{email::EmailAlarm, structured_logger, SLOGGER};

Expand All @@ -42,6 +44,7 @@ pub struct Logger {
filter: Filter,
stderr_is_tty: bool,
email_alarm: Option<EmailAlarm>,
last_email_sent: Mutex<Option<Instant>>,
}

impl Logger {
Expand All @@ -60,6 +63,7 @@ impl Logger {
filter: builder.build(),
stderr_is_tty,
email_alarm,
last_email_sent: Mutex::new(None),
}
}

Expand Down Expand Up @@ -110,7 +114,15 @@ impl Log for Logger {

if log_level == Level::Error {
if let Some(email_alarm) = &self.email_alarm {
email_alarm.send(&format!("{} {} {}", thread_name, log_target, log_message))
let mut last_email_sent = self.last_email_sent.lock();
let sent_recently = match *last_email_sent {
Some(last_sent) => last_sent.elapsed() < Duration::from_secs(300),
None => false,
};
if !sent_recently {
email_alarm.send(&format!("{} {} {}", thread_name, log_target, log_message));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sure that this doesn't cause another log. It will lead to a deadlock. If not, LGTM.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The send function does not use log.

*last_email_sent = Some(Instant::now());
}
}
}
}
Expand Down