add simple reload

This commit is contained in:
Dmitry Belyaev 2023-04-27 13:56:22 +03:00
parent c69798ffd6
commit 133c0fa31b
Signed by: b4tman
GPG Key ID: 41A00BF15EA7E5F3
2 changed files with 43 additions and 4 deletions

View File

@ -6,5 +6,5 @@ delay = 2
start = "11:50"
#end = { hour = 12, minute = 55 }
end = "12:55"
#exit_now = true

View File

@ -6,6 +6,7 @@ use itertools::Itertools;
use serde::Deserialize;
use std::path::Path;
use std::str::FromStr;
use std::time::SystemTime;
use std::{thread, time::Duration};
use sysinfo::{System, SystemExt};
@ -70,6 +71,9 @@ impl TryFrom<String> for TimeHM {
/// Application config
#[derive(Deserialize, Default, Debug)]
struct Config {
/// modification time
#[serde(skip)]
modified: Option<SystemTime>,
/// is application active
#[serde(default = "default_true")]
active: bool,
@ -118,15 +122,49 @@ impl Config {
}
let path = path.unwrap();
let cfg = Config::read(path);
let cfg = Config::read(&path);
match cfg {
Err(_e) => {
//println!("{}", _e);
Config::default()
}
Ok(cfg) => cfg,
Ok(mut cfg) => {
let metadata = fs::metadata(&path);
if let Ok(meta) = metadata {
if let Ok(time) = meta.modified() {
cfg.modified = Some(time);
}
}
cfg
}
}
}
fn is_file_modified(&self) -> bool {
if self.modified.is_none() {
return true;
}
let path = Config::file_location();
if let Err(_e) = path {
return false;
}
let path = path.unwrap();
let metadata = fs::metadata(path);
let mut result = false;
if let Ok(meta) = metadata {
if let Ok(time) = meta.modified() {
result = self.modified.unwrap() < time
}
}
result
}
fn reload(&mut self) {
if !self.is_file_modified() {
return;
}
let new_cfg = Self::get();
*self = new_cfg;
}
fn target_name(&self) -> String {
if !self.target.is_empty() {
self.target.clone()
@ -183,11 +221,12 @@ impl Laika {
let _res = cmd.spawn();
}
fn main_loop(self) {
fn main_loop(mut self) {
if !self.config.is_valid() {
return;
}
loop {
self.config.reload();
if self.config.exit_now {
return;
} else if self.is_active() && !self.is_target_alive() {