Skip to content

Commit a643c6c

Browse files
majectysgkim126
authored andcommitted
Send an email when a pnic occurred
1 parent 6ac28d5 commit a643c6c

File tree

7 files changed

+29
-10
lines changed

7 files changed

+29
-10
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

codechain/run_node.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use ckey::{Address, NetworkId, PlatformAddress};
2828
use ckeystore::accounts_dir::RootDiskDirectory;
2929
use ckeystore::KeyStore;
3030
use clap::ArgMatches;
31-
use clogger::{self, EmailAlarmConfig, LoggerConfig};
31+
use clogger::{self, EmailAlarm, EmailAlarmConfig, LoggerConfig};
3232
use cnetwork::{Filters, NetworkConfig, NetworkControl, NetworkService, RoutingTable, SocketAddr};
3333
use csync::{BlockSyncExtension, BlockSyncSender, SnapshotService, TransactionSyncExtension};
3434
use ctimer::TimerLoop;
@@ -236,17 +236,21 @@ pub fn run_node(matches: &ArgMatches) -> Result<(), String> {
236236
.expect("Current time should be later than unix epoch")
237237
.subsec_nanos() as usize,
238238
);
239-
let email_alarm_config = if !config.email_alarm.disable.unwrap() {
240-
match (&config.email_alarm.to, &config.email_alarm.sendgrid_key) {
239+
let email_alarm = if !config.email_alarm.disable.unwrap() {
240+
let config = match (&config.email_alarm.to, &config.email_alarm.sendgrid_key) {
241241
(Some(to), Some(sendgrid_key)) => Some(EmailAlarmConfig::new(to.to_string(), sendgrid_key.to_string())),
242242
(None, _) => return Err("email-alarm-to is not specified".to_string()),
243243
(_, None) => return Err("email-alarm-sendgrid-key is not specified".to_string()),
244-
}
244+
};
245+
config.as_ref().map(EmailAlarm::new)
245246
} else {
246247
None
247248
};
248-
clogger::init(&LoggerConfig::new(instance_id), &email_alarm_config)
249+
clogger::init(&LoggerConfig::new(instance_id), email_alarm.clone())
249250
.expect("Logger must be successfully initialized");
251+
if let Some(email_alarm) = email_alarm {
252+
panic_hook::set_with_email_alarm(email_alarm);
253+
}
250254

251255
let pf = load_password_file(&config.operating.password_path)?;
252256
let base_path = config.operating.base_path.as_ref().unwrap().clone();

codechain/subcommand/account_command.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ pub fn run_account_command(matches: &ArgMatches) -> Result<(), String> {
3636
return Ok(())
3737
}
3838

39-
clogger::init(&LoggerConfig::new(0), &None).expect("Logger must be successfully initialized");
39+
clogger::init(&LoggerConfig::new(0), None).expect("Logger must be successfully initialized");
4040

4141
let keys_path = get_global_argument(matches, "keys-path").unwrap_or_else(|| DEFAULT_KEYS_PATH.into());
4242
let dir = RootDiskDirectory::create(keys_path).expect("Cannot read key path directory");

util/logger/src/email.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ impl EmailAlarmConfig {
1414
}
1515
}
1616

17+
#[derive(Clone)]
1718
pub struct EmailAlarm {
1819
pub to: String,
1920
pub sendgrid_key: String,

util/logger/src/lib.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ use logger::Logger;
3838

3939
pub use log::Level;
4040

41-
pub fn init(config: &LoggerConfig, email_alarm_config: &Option<EmailAlarmConfig>) -> Result<(), SetLoggerError> {
42-
let email_alarm = email_alarm_config.as_ref().map(EmailAlarm::new);
41+
pub fn init(config: &LoggerConfig, email_alarm: Option<EmailAlarm>) -> Result<(), SetLoggerError> {
4342
let logger = Logger::new(config, email_alarm);
4443
log::set_max_level(logger.filter());
4544
log::set_boxed_logger(Box::new(logger))
@@ -53,5 +52,4 @@ lazy_static! {
5352
pub static ref SLOGGER: StructuredLogger = StructuredLogger::create();
5453
}
5554

56-
use email::EmailAlarm;
57-
pub use email::EmailAlarmConfig;
55+
pub use email::{EmailAlarm, EmailAlarmConfig};

util/panic_hook/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ authors = ["Parity Technologies <admin@parity.io>", "CodeChain Team <hi@codechai
88

99
[dependencies]
1010
backtrace = "0.3.2"
11+
codechain-logger = { path = "../logger" }

util/panic_hook/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@
1717
//! Custom panic hook with bug report link
1818
1919
extern crate backtrace;
20+
extern crate codechain_logger as clogger;
2021

2122
use backtrace::Backtrace;
23+
use clogger::EmailAlarm;
2224
use std::panic::{self, PanicInfo};
2325
use std::thread;
2426

@@ -27,6 +29,10 @@ pub fn set() {
2729
panic::set_hook(Box::new(panic_hook));
2830
}
2931

32+
pub fn set_with_email_alarm(email_alarm: clogger::EmailAlarm) {
33+
panic::set_hook(Box::new(move |info| panic_hook_with_email_alarm(&email_alarm, info)));
34+
}
35+
3036
static ABOUT_PANIC: &str = "
3137
This is a bug. Please report it at:
3238
@@ -39,6 +45,14 @@ fn panic_hook(info: &PanicInfo) {
3945
exit_on_debug_mode();
4046
}
4147

48+
fn panic_hook_with_email_alarm(email_alarm: &EmailAlarm, info: &PanicInfo) {
49+
let message = panic_message(info);
50+
eprintln!("{}", message);
51+
let message_for_email = message.replace("\n", "<br>");
52+
email_alarm.send(&message_for_email);
53+
exit_on_debug_mode();
54+
}
55+
4256
fn panic_message(info: &PanicInfo) -> String {
4357
let location = info.location();
4458
let file = location.as_ref().map(|l| l.file()).unwrap_or("<unknown>");

0 commit comments

Comments
 (0)