reflect: reflect

This commit is contained in:
2020-10-18 22:42:59 +08:00
parent 25cb508334
commit ad28d12f4c

View File

@@ -29,6 +29,7 @@ lazy_static!{
static ref CHECK_FAIL_MAP: Mutex<HashMap<String, u64>> = Mutex::new(HashMap::new());
}
fn main() {
panic::set_hook(Box::new(|panic_info| {
error!("Panic in running keeprunningd: {:?}", panic_info);
@@ -51,10 +52,7 @@ fn main() {
};
}
if simple_logging::log_to_file("/var/log/keeprunningd.log", LevelFilter::Info).is_err()
&& simple_logging::log_to_file("/tmp/keeprunningd.log", LevelFilter::Info).is_err() {
simple_logging::log_to_stderr(LevelFilter::Info);
}
init_logger();
let keep_running_config = Arc::new(keep_running_config);
for check_cnt in 0.. {
@@ -131,30 +129,11 @@ fn keep_runningd(keep_running_config: Arc<KeepRunningConfig>) {
if check_lines.is_empty() { // if check fail!
info!("Send DingTalk notification!");
use tokio::runtime;
match runtime::Builder::new().basic_scheduler().enable_all().build() {
Err(err) => error!("Prepare tokio runtime error: {}", err),
Ok(mut rt) => {
let mut sb = String::with_capacity(1024);
sb.push_str(&format!("Check failed: {}, fail count: {}", &keep_running_config_item.title, fail_count));
sb.push_str(&format!("\ncheck time: {:?}", Local::now()));
rt.block_on(send_notify(&keep_running_config.notify_token, &sb));
},
}
block_send_notify(&keep_running_config.notify_token, &keep_running_config_item.title, fail_count);
match &keep_running_config_item.restart_command {
Some(restart_command) if fail_count > 1 => {
info!("Fail count is {} > 1, try restart command: {:?}", fail_count, restart_command);
let mut restart_command_iter = restart_command.iter();
if let Some(program) = restart_command_iter.next() {
let mut cmd = Command::new(program);
for arg in restart_command_iter {
cmd.arg(arg);
}
match cmd.spawn() {
Ok(child) => info!("Ran process success, process id: {}", child.id()),
Err(e) => error!("Run restart command failed: {}", e),
}
}
restart_cmd(restart_command);
},
_ => ( /* IGNORE */ ),
}
@@ -168,6 +147,19 @@ fn keep_runningd(keep_running_config: Arc<KeepRunningConfig>) {
}
}
fn block_send_notify(notify_token: &str, title: &str, fail_count: u64) {
use tokio::runtime;
match runtime::Builder::new().basic_scheduler().enable_all().build() {
Err(err) => error!("Prepare tokio runtime error: {}", err),
Ok(mut rt) => {
let mut sb = String::with_capacity(1024);
sb.push_str(&format!("Check failed: {}, fail count: {}", title, fail_count));
sb.push_str(&format!("\ncheck time: {:?}", Local::now()));
rt.block_on(send_notify(notify_token, &sb));
},
}
}
async fn send_notify(notify_token: &str, text: &str) {
if notify_token.is_empty() {
info!("Notify token is empty, do not send DingTalk notification");
@@ -181,6 +173,27 @@ async fn send_notify(notify_token: &str, text: &str) {
}
}
fn restart_cmd(restart_command: &[String]) {
let mut restart_command_iter = restart_command.iter();
if let Some(program) = restart_command_iter.next() {
let mut cmd = Command::new(program);
for arg in restart_command_iter {
cmd.arg(arg);
}
match cmd.spawn() {
Ok(child) => info!("Ran process success, process id: {}", child.id()),
Err(e) => error!("Run restart command failed: {}", e),
}
}
}
fn init_logger() {
if simple_logging::log_to_file("/var/log/keeprunningd.log", LevelFilter::Info).is_err()
&& simple_logging::log_to_file("/tmp/keeprunningd.log", LevelFilter::Info).is_err() {
simple_logging::log_to_stderr(LevelFilter::Info);
}
}
fn ps_aux() -> Option<String> {
let mut cmd = Command::new("ps");
cmd.arg("aux");