style: code style
This commit is contained in:
4
Cargo.lock
generated
4
Cargo.lock
generated
@@ -944,9 +944,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust_util"
|
name = "rust_util"
|
||||||
version = "0.2.5"
|
version = "0.6.3"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "854b942370a20552f9ab927cdc4e88d1d29c59c17f07729930031c26afad16ec"
|
checksum = "9e9cf201657d8553fd7eddf4c20e00b1bdebca40e9fa2ede5c87f6874d02750f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"libc",
|
"libc",
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ tokio = { version = "0.2.6", features = ["full"] }
|
|||||||
serde = { version = "1.0", features = ["derive"] }
|
serde = { version = "1.0", features = ["derive"] }
|
||||||
serde_json = "1.0"
|
serde_json = "1.0"
|
||||||
argparse = "0.2.2"
|
argparse = "0.2.2"
|
||||||
rust_util = "0.2.5"
|
rust_util = "0.6.3"
|
||||||
dingtalk = "1.3.2"
|
dingtalk = "1.3.2"
|
||||||
chrono = "0.4.11"
|
chrono = "0.4.11"
|
||||||
# log = "0.4.8"
|
# log = "0.4.8"
|
||||||
|
|||||||
36
src/main.rs
36
src/main.rs
@@ -1,3 +1,6 @@
|
|||||||
|
#[macro_use]
|
||||||
|
extern crate rust_util;
|
||||||
|
|
||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs,
|
||||||
panic,
|
panic,
|
||||||
@@ -11,7 +14,6 @@ use serde::{ Deserialize, Serialize, };
|
|||||||
use rust_util::{
|
use rust_util::{
|
||||||
util_str::read_str_to_lines,
|
util_str::read_str_to_lines,
|
||||||
util_file::locate_file,
|
util_file::locate_file,
|
||||||
util_msg::{ print_info, print_error, print_debug, }
|
|
||||||
};
|
};
|
||||||
use dingtalk::DingTalk;
|
use dingtalk::DingTalk;
|
||||||
|
|
||||||
@@ -31,7 +33,7 @@ struct KeepRunningConfigItem {
|
|||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
panic::set_hook(Box::new(|panic_info| {
|
panic::set_hook(Box::new(|panic_info| {
|
||||||
print_error(&format!("Panic in running keeprunningd: {:?}", panic_info));
|
failure!("Panic in running keeprunningd: {:?}", panic_info);
|
||||||
}));
|
}));
|
||||||
let keep_running_config = match parse_keep_running_config() {
|
let keep_running_config = match parse_keep_running_config() {
|
||||||
Some(c) => c, None => return,
|
Some(c) => c, None => return,
|
||||||
@@ -39,7 +41,7 @@ fn main() {
|
|||||||
|
|
||||||
let keep_running_config = Arc::new(keep_running_config);
|
let keep_running_config = Arc::new(keep_running_config);
|
||||||
for check_cnt in 0.. {
|
for check_cnt in 0.. {
|
||||||
print_info(&format!("Check index: {} @{:?}", check_cnt, Local::now()));
|
information!("Check index: {} @{:?}", check_cnt, Local::now());
|
||||||
keep_runningd(keep_running_config.clone());
|
keep_runningd(keep_running_config.clone());
|
||||||
thread::sleep(Duration::from_secs(keep_running_config.check_inverval_secs.unwrap_or(60 * 60)));
|
thread::sleep(Duration::from_secs(keep_running_config.check_inverval_secs.unwrap_or(60 * 60)));
|
||||||
}
|
}
|
||||||
@@ -54,32 +56,32 @@ fn parse_keep_running_config() -> Option<KeepRunningConfig> {
|
|||||||
|
|
||||||
let config_file = match config_file_opt {
|
let config_file = match config_file_opt {
|
||||||
None => {
|
None => {
|
||||||
print_error("Cannot find config file!");
|
failure!("Cannot find config file!");
|
||||||
return None;
|
return None;
|
||||||
},
|
},
|
||||||
Some(config_file) => {
|
Some(config_file) => {
|
||||||
print_info(&format!("Find config file: {:?}", &config_file));
|
information!("Find config file: {:?}", &config_file);
|
||||||
config_file
|
config_file
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let config_file_content = match fs::read_to_string(&config_file) {
|
let config_file_content = match fs::read_to_string(&config_file) {
|
||||||
Ok(c) => c, Err(err) => {
|
Ok(c) => c, Err(err) => {
|
||||||
print_error(&format!("Read config file {:?}, error: {}", &config_file, err));
|
failure!("Read config file {:?}, error: {}", &config_file, err);
|
||||||
return None;
|
return None;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
let keep_running_config: KeepRunningConfig = match serde_json::from_str(&config_file_content) {
|
let keep_running_config: KeepRunningConfig = match serde_json::from_str(&config_file_content) {
|
||||||
Ok(c) => c, Err(err) => {
|
Ok(c) => c, Err(err) => {
|
||||||
print_error(&format!("Parse config file: {:?}, error: {}", &config_file, err));
|
failure!("Parse config file: {:?}, error: {}", &config_file, err);
|
||||||
return None;
|
return None;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
if keep_running_config.show_debug_output.unwrap_or(false) {
|
if keep_running_config.show_debug_output.unwrap_or(false) {
|
||||||
if let Ok(json) = serde_json::to_string_pretty(&keep_running_config) {
|
if let Ok(json) = serde_json::to_string_pretty(&keep_running_config) {
|
||||||
print_debug(&format!("Config: {}", json));
|
debugging!("Config: {}", json);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Some(keep_running_config)
|
Some(keep_running_config)
|
||||||
@@ -93,10 +95,10 @@ fn keep_runningd(keep_running_config: Arc<KeepRunningConfig>) {
|
|||||||
keep_running_config_item.grep_tokens.iter().all(|t| ln.contains(t))
|
keep_running_config_item.grep_tokens.iter().all(|t| ln.contains(t))
|
||||||
}).collect::<Vec<_>>();
|
}).collect::<Vec<_>>();
|
||||||
if check_lines.is_empty() { // if check fail!
|
if check_lines.is_empty() { // if check fail!
|
||||||
print_info("Send DingTalk notification!");
|
information!("Send DingTalk notification!");
|
||||||
use tokio::runtime;
|
use tokio::runtime;
|
||||||
match runtime::Builder::new().basic_scheduler().enable_all().build() {
|
match runtime::Builder::new().basic_scheduler().enable_all().build() {
|
||||||
Err(err) => print_error(&format!("Prepare tokio runtime error: {}", err)),
|
Err(err) => failure!("Prepare tokio runtime error: {}", err),
|
||||||
Ok(mut rt) => {
|
Ok(mut rt) => {
|
||||||
let mut sb = String::with_capacity(1024);
|
let mut sb = String::with_capacity(1024);
|
||||||
sb.push_str(&format!("Check failed: {}", &keep_running_config_item.title));
|
sb.push_str(&format!("Check failed: {}", &keep_running_config_item.title));
|
||||||
@@ -105,20 +107,20 @@ fn keep_runningd(keep_running_config: Arc<KeepRunningConfig>) {
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
} else if keep_running_config.show_debug_output.unwrap_or(false) {
|
} else if keep_running_config.show_debug_output.unwrap_or(false) {
|
||||||
print_debug(&format!("Find: {:?}", &check_lines));
|
debugging!("Find: {:?}", &check_lines);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
if let Err(err) = t.join() {
|
if let Err(err) = t.join() {
|
||||||
print_error(&format!("Join check thread error: {:?}", err));
|
failure!("Join check thread error: {:?}", err);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn send_notify(notify_token: &str, text: &str) {
|
async fn send_notify(notify_token: &str, text: &str) {
|
||||||
match DingTalk::from_token(¬ify_token) {
|
match DingTalk::from_token(¬ify_token) {
|
||||||
Err(err) => print_error(&format!("Prepare DingTalk error: {}", err)),
|
Err(err) => failure!("Prepare DingTalk error: {}", err),
|
||||||
Ok(dt) => if let Err(err) = dt.send_text(text).await {
|
Ok(dt) => if let Err(err) = dt.send_text(text).await {
|
||||||
print_error(&format!("Send DingTalk message error: {}", err));
|
failure!("Send DingTalk message error: {}", err);
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -128,18 +130,18 @@ fn ps_aux() -> Option<String> {
|
|||||||
cmd.arg("aux");
|
cmd.arg("aux");
|
||||||
let output = match cmd.output() {
|
let output = match cmd.output() {
|
||||||
Ok(output) => output, Err(err) => {
|
Ok(output) => output, Err(err) => {
|
||||||
print_error(&format!("Run ps error: {}", err));
|
failure!("Run ps error: {}", err);
|
||||||
return None;
|
return None;
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
if !output.status.success() {
|
if !output.status.success() {
|
||||||
print_error(&format!("Run 'ps aux' error: {:?}", output.status));
|
failure!("Run 'ps aux' error: {:?}", output.status);
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
match String::from_utf8(output.stdout) {
|
match String::from_utf8(output.stdout) {
|
||||||
Ok(output_str) => Some(output_str),
|
Ok(output_str) => Some(output_str),
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
print_error(&format!("Get ps output as utf8 error: {}", err));
|
failure!("Get ps output as utf8 error: {}", err);
|
||||||
None
|
None
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user