feat: v0.2.0 - add restart command
This commit is contained in:
3
Cargo.lock
generated
3
Cargo.lock
generated
@@ -517,11 +517,12 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "keeprunningd"
|
name = "keeprunningd"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"argparse",
|
"argparse",
|
||||||
"chrono",
|
"chrono",
|
||||||
"dingtalk",
|
"dingtalk",
|
||||||
|
"lazy_static",
|
||||||
"rust_util",
|
"rust_util",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "keeprunningd"
|
name = "keeprunningd"
|
||||||
version = "0.1.0"
|
version = "0.2.0"
|
||||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
|
|
||||||
@@ -14,6 +14,7 @@ argparse = "0.2.2"
|
|||||||
rust_util = "0.6.3"
|
rust_util = "0.6.3"
|
||||||
dingtalk = "1.3.2"
|
dingtalk = "1.3.2"
|
||||||
chrono = "0.4.11"
|
chrono = "0.4.11"
|
||||||
|
lazy_static = "1.4.0"
|
||||||
# log = "0.4.8"
|
# log = "0.4.8"
|
||||||
|
|
||||||
[profile.release]
|
[profile.release]
|
||||||
|
|||||||
@@ -1,2 +1,6 @@
|
|||||||
# keeprunningd
|
# keeprunningd
|
||||||
|
|
||||||
|
* Changelog
|
||||||
|
* v0.2.0 - add restart command
|
||||||
|
* v0.1.0 - first version
|
||||||
|
|
||||||
|
|||||||
58
src/main.rs
58
src/main.rs
@@ -1,20 +1,10 @@
|
|||||||
#[macro_use]
|
#[macro_use] extern crate lazy_static;
|
||||||
extern crate rust_util;
|
#[macro_use] extern crate rust_util;
|
||||||
|
|
||||||
use std::{
|
use std::{ collections::HashMap, fs, panic, thread, time::Duration, process::Command, sync::{ Arc, Mutex } };
|
||||||
fs,
|
|
||||||
panic,
|
|
||||||
thread,
|
|
||||||
time::Duration,
|
|
||||||
process::Command,
|
|
||||||
sync::Arc,
|
|
||||||
};
|
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use serde::{ Deserialize, Serialize };
|
use serde::{ Deserialize, Serialize };
|
||||||
use rust_util::{
|
use rust_util::{ util_str::read_str_to_lines, util_file::locate_file };
|
||||||
util_str::read_str_to_lines,
|
|
||||||
util_file::locate_file,
|
|
||||||
};
|
|
||||||
use dingtalk::DingTalk;
|
use dingtalk::DingTalk;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
@@ -29,6 +19,11 @@ struct KeepRunningConfig {
|
|||||||
struct KeepRunningConfigItem {
|
struct KeepRunningConfigItem {
|
||||||
title: String,
|
title: String,
|
||||||
grep_tokens: Vec<String>,
|
grep_tokens: Vec<String>,
|
||||||
|
restart_command: Option<Vec<String>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
lazy_static!{
|
||||||
|
static ref CHECK_FAIL_MAP: Mutex<HashMap<String, u64>> = Mutex::new(HashMap::new());
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
@@ -94,6 +89,22 @@ fn keep_runningd(keep_running_config: Arc<KeepRunningConfig>) {
|
|||||||
let check_lines = ps_aux_lines.iter().filter(|ln| {
|
let check_lines = ps_aux_lines.iter().filter(|ln| {
|
||||||
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<_>>();
|
||||||
|
|
||||||
|
let mut fail_count = 0;
|
||||||
|
{ // limit `CHECK_FAIL_MAP` lock scope
|
||||||
|
let cloned_title = keep_running_config_item.title.clone();
|
||||||
|
let mut check_fail_map = CHECK_FAIL_MAP.lock().unwrap();
|
||||||
|
let check_success = !check_lines.is_empty();
|
||||||
|
if check_success {
|
||||||
|
check_fail_map.insert(cloned_title, 0);
|
||||||
|
} else {
|
||||||
|
match check_fail_map.get_mut(&cloned_title) {
|
||||||
|
None => { check_fail_map.insert(cloned_title, 1); fail_count = 1; },
|
||||||
|
Some(n) => { *n += 1; fail_count = *n; },
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if check_lines.is_empty() { // if check fail!
|
if check_lines.is_empty() { // if check fail!
|
||||||
information!("Send DingTalk notification!");
|
information!("Send DingTalk notification!");
|
||||||
use tokio::runtime;
|
use tokio::runtime;
|
||||||
@@ -101,11 +112,28 @@ fn keep_runningd(keep_running_config: Arc<KeepRunningConfig>) {
|
|||||||
Err(err) => failure!("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: {}, fail count: {}", &keep_running_config_item.title, fail_count));
|
||||||
sb.push_str(&format!("\ncheck time: {:?}", Local::now()));
|
sb.push_str(&format!("\ncheck time: {:?}", Local::now()));
|
||||||
rt.block_on(send_notify(&keep_running_config.notify_token, &sb));
|
rt.block_on(send_notify(&keep_running_config.notify_token, &sb));
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
match &keep_running_config_item.restart_command {
|
||||||
|
Some(restart_command) if fail_count > 1 => {
|
||||||
|
information!("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);
|
||||||
|
while let Some(arg) = restart_command_iter.next() {
|
||||||
|
cmd.arg(arg);
|
||||||
|
}
|
||||||
|
match cmd.spawn() {
|
||||||
|
Ok(child) => success!("Ran process success, process id: {}", child.id()),
|
||||||
|
Err(e) => failure!("Run restart command failed: {}", e),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
_ => ( /* IGNORE */ ),
|
||||||
|
}
|
||||||
} else if keep_running_config.show_debug_output.unwrap_or(false) {
|
} else if keep_running_config.show_debug_output.unwrap_or(false) {
|
||||||
debugging!("Find: {:?}", &check_lines);
|
debugging!("Find: {:?}", &check_lines);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user