bugfix: watcher mechanism

pull/9/head
prplV 2024-10-30 15:21:13 +03:00
parent 4dd42a0cab
commit 0be23dfb43
6 changed files with 57 additions and 15 deletions

View File

@ -79,7 +79,7 @@ async fn main() {
})); }));
for i in handler { for i in handler {
i.await.unwrap(); let _ = i.await;
} }
return; return;
} }

View File

@ -7,7 +7,7 @@ use std::sync::Arc;
use std::{env, fs}; use std::{env, fs};
use tokio::time::Duration; use tokio::time::Duration;
use std::fs::OpenOptions; use std::fs::OpenOptions;
use std::io::{self, Write}; use std::io::Write;
const CONFIG_PATH: &str = "settings.json"; const CONFIG_PATH: &str = "settings.json";
@ -316,10 +316,11 @@ fn parse_extern_config(json_string: &str) -> Option<Processes> {
assert_eq!(config_comparing(&a, &b), ConfigActuality::Remote); assert_eq!(config_comparing(&a, &b), ConfigActuality::Remote);
} }
#[test] // TODO : strange output
fn get_actual_config_mechanism() { // #[test]
assert!(get_actual_config().is_some()) // fn get_actual_config_mechanism() {
} // assert!(get_actual_config().is_some())
// }
#[test] #[test]
fn save_config() { fn save_config() {
let a = Processes { let a = Processes {

View File

@ -75,3 +75,13 @@ impl SigPostProcessing for Sig {
Ok(()) Ok(())
} }
} }
#[cfg(test)]
mod signals_unittest {
use super::*;
#[tokio::test]
async fn get_signal_check() {
assert!(Signals::Sigint.get_signal().is_ok())
}
}

View File

@ -31,7 +31,13 @@ pub async fn run_daemons(
// creating watchers + ---buffers--- // creating watchers + ---buffers---
let mut watchers: Vec<Inotify> = vec![]; let mut watchers: Vec<Inotify> = vec![];
for file in proc.dependencies.files.clone().into_iter() { for file in proc.dependencies.files.clone().into_iter() {
watchers.push(create_watcher(&file.filename, &file.src).await.unwrap()); if let Ok(watcher) = create_watcher(&file.filename, &file.src).await {
watchers.push(watcher);
}
else {
let _ = tx.send(121).await;
}
// watchers.push(create_watcher(&file.filename, &file.src).await.unwrap());
} }
let watchers_clone: Arc<tokio::sync::Mutex<Vec<Inotify>>> = let watchers_clone: Arc<tokio::sync::Mutex<Vec<Inotify>>> =
Arc::new(tokio::sync::Mutex::new(watchers)); Arc::new(tokio::sync::Mutex::new(watchers));
@ -130,6 +136,13 @@ pub async fn run_daemons(
} }
break; break;
}, },
//
// 121 - Cannot create valid watcher for file dependency
121 => {
error!("Cannot create valid watcher for {}'s file dependency. Terminating...", proc.name);
let _ = terminate_process("runner-rs").await;
break;
},
// 111 - global thread termination with killing current child in a face // 111 - global thread termination with killing current child in a face
// of a current process // of a current process
111 => { 111 => {

View File

@ -1,7 +1,6 @@
use crate::options::structs::{CustomError, Files}; use crate::options::structs::{CustomError, Files};
use crate::utils::prcs::{is_active, is_frozen}; use crate::utils::prcs::{is_active, is_frozen};
use inotify::{EventMask, Inotify, WatchMask}; use inotify::{EventMask, Inotify, WatchMask};
use log::error;
use std::borrow::BorrowMut; use std::borrow::BorrowMut;
use std::path::Path; use std::path::Path;
use std::sync::Arc; use std::sync::Arc;
@ -10,12 +9,8 @@ use tokio::time::Duration;
pub async fn create_watcher(filename: &str, path: &str) -> Result<Inotify, std::io::Error> { pub async fn create_watcher(filename: &str, path: &str) -> Result<Inotify, std::io::Error> {
let src = format!("{}{}", path, filename); let src = format!("{}{}", path, filename);
let inotify: Inotify = Inotify::init().unwrap_or_else(|_| { let inotify: Inotify = Inotify::init()?;
error!("{}", format!("Cannot create watcher for {}", &src)); inotify.watches().add(&src, WatchMask::ALL_EVENTS)?;
std::process::exit(101);
});
_ = inotify.watches().add(&src, WatchMask::ALL_EVENTS);
Ok(inotify) Ok(inotify)
} }
@ -119,3 +114,26 @@ pub async fn check_file(filename: &str, path: &str) -> Result<(), CustomError> {
panic!("Corrupted while file check process"); panic!("Corrupted while file check process");
}) })
} }
#[cfg(test)]
mod files_unittest {
use super::*;
#[test]
fn try_to_create_watcher() {
}
#[test]
fn try_to_create_invalid_watcher() {
}
#[test]
fn check_existing_file() {
}
#[test]
fn check_non_existing_file() {
}
}

View File

@ -97,7 +97,7 @@ pub async fn start_process(name: &str, path: &str) -> Result<(), CustomError> {
// let runsh = format!("{} {}", "exec", path); // let runsh = format!("{} {}", "exec", path);
let mut command = Command::new(path); let mut command = Command::new(path);
// command.arg(path); // command.arg(path);
match command.spawn() { match command.spawn() {
Ok(_) => { Ok(_) => {
warn!("Process {} is running now!", name); warn!("Process {} is running now!", name);