zero state

pull/9/head
prplV 2024-07-08 03:42:42 -04:00
parent c90e1938ef
commit 244508509c
2 changed files with 83 additions and 108 deletions

View File

@ -8,7 +8,7 @@
"filename" : "control-file", "filename" : "control-file",
"src" : "/home/vladislav/web/", "src" : "/home/vladislav/web/",
"triggers" : { "triggers" : {
"onDelete" : "hold", "onDelete" : "stop",
"onChange" : "hold" "onChange" : "hold"
} }
}, },
@ -16,7 +16,7 @@
"filename" : "config-file", "filename" : "config-file",
"src" : "/home/vladislav/web/", "src" : "/home/vladislav/web/",
"triggers" : { "triggers" : {
"onDelete" : "hold", "onDelete" : "stop",
"onChange" : "hold" "onChange" : "hold"
} }
} }
@ -27,7 +27,7 @@
"triggers" : { "triggers" : {
"wait" : 6, "wait" : 6,
"delay" : 1, "delay" : 1,
"onLost" : "hold" "onLost" : "stop"
} }
}] }]
} }
@ -41,7 +41,7 @@
"filename" : "control-file", "filename" : "control-file",
"src" : "/home/vladislav/web/", "src" : "/home/vladislav/web/",
"triggers" : { "triggers" : {
"onDelete" : "hold", "onDelete" : "stop",
"onChange" : "hold" "onChange" : "hold"
} }
}], }],
@ -51,7 +51,7 @@
"triggers" : { "triggers" : {
"wait" : 14, "wait" : 14,
"delay" : 1, "delay" : 1,
"onLost" : "hold" "onLost" : "stop"
} }
}, { }, {
"hostname" : "localhost", "hostname" : "localhost",

View File

@ -1,5 +1,7 @@
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json; use serde_json;
use tokio::io::Join;
use std::env::join_paths;
use std::fmt::Debug; use std::fmt::Debug;
use std::fs; use std::fs;
use std::path::Path; use std::path::Path;
@ -7,7 +9,7 @@ use std::process::{Command, Output};
// to use in time-trigger // to use in time-trigger
use tokio::time::{Duration, Instant}; use tokio::time::{Duration, Instant};
// to store condition between asynchronous tasks // to store condition between asynchronous tasks
use tokio::sync::mpsc; use tokio::sync::{futures, mpsc};
enum CustomError { enum CustomError {
@ -49,7 +51,6 @@ struct Services {
} }
// policy // policy
#[derive(Debug, Serialize, Deserialize, Clone)] #[derive(Debug, Serialize, Deserialize, Clone)]
struct ServiceTriggers { struct ServiceTriggers {
wait : u32, wait : u32,
@ -85,24 +86,6 @@ async fn main() {
// creating msg channel // creating msg channel
let (tx, mut rx) = mpsc::channel::<u8>(1); let (tx, mut rx) = mpsc::channel::<u8>(1);
// for file in proc.dependencies.files.iter() {
// if let Err(_) = check_file(&file.filename, &file.src) {
// eprintln!("Error: Process {} cannot run without file {}{}", proc.name, file.src, file.filename);
// error_counter += 1;
// }
// }
// for ser in proc.dependencies.services.iter() {
// if let Err(_) = check_service(&ser.hostname, &ser.port) {
// eprintln!("Error: Process {} cannot run while service {}:{} is down", proc.name, ser.hostname, ser.port);
// error_counter += 1;
// }
// }
// if error_counter > 0 {
// continue;
// }
let proc_clone = proc.clone(); let proc_clone = proc.clone();
let tx_clone = tx.clone(); let tx_clone = tx.clone();
@ -114,6 +97,7 @@ async fn main() {
for i in handler { for i in handler {
i.await.unwrap(); i.await.unwrap();
} }
return; return;
} }
@ -189,6 +173,7 @@ async fn run_daemons(
} }
}, },
} }
// tokio::task::yield_now().await;
} }
} }
@ -262,57 +247,57 @@ async fn start_process(name: &str, path: &str) -> Result<(), CustomError> {
} }
// check process status daemon // check process status daemon
async fn running_handler(name: &str, path: &str, tx: mpsc::Sender<u8>){ async fn running_handler(name: &str, path: &str, tx: mpsc::Sender<u8>){
// println!("running daemon on {}", name); println!("running daemon on {}", name);
let _ = Command::new("pidof") let _ = Command::new("pidof")
.arg(name) .arg(name)
.output() .output()
.expect("Failed to execute command 'pidof'"); .expect("Failed to execute command 'pidof'");
if !is_active(name) && !is_frozen(name){ if !is_active(name) && !is_frozen(name){
match start_process(name, path).await { if start_process(name, path).await.is_err() {
Ok(_) => {}, tx.send(3).await.unwrap();
Err(_) => { // return;
tx.send(3).await.unwrap(); }
}, }
} tokio::time::sleep(Duration::from_millis(100)).await;
}
tokio::time::sleep(Duration::from_millis(100)).await;
} }
async fn file_handler(name: &str, files: &Vec<Files>, tx: mpsc::Sender<u8>) { async fn file_handler(name: &str, files: &Vec<Files>, tx: mpsc::Sender<u8>) {
// println!("file daemon on {}", name); loop {
if is_active(name) { println!("file daemon on {}", name);
for file in files { if is_active(name) && !is_frozen(name){
match check_file(&file.filename, &file.src) { for file in files {
Ok(_) => { if check_file(&file.filename, &file.src).is_err() {
continue; match file.triggers.on_delete.as_str() {
}, "stay" => {
Err(_) => { continue;
match file.triggers.on_delete.as_str() { },
"stay" => { "stop" => {
continue; tx.send(1).await.unwrap();
}, return;
"stop" => { },
tx.send(1).await.unwrap(); "hold" => {
return; tx.send(2).await.unwrap();
}, // continue;
"hold" => { // while check_file(&file.filename, &file.src).is_err() {
tx.send(2).await.unwrap(); // println!("zov");
return; // tokio::time::sleep(Duration::from_millis(100)).await;
}, // }
_ => { // tx.send(10).await.unwrap();
tx.send(101).await.unwrap(); },
return; _ => {
}, tx.send(101).await.unwrap();
return;
},
} }
}, }
} }
// if is_frozen(name) {
// tx.send(10).await.unwrap();
// }
} }
if is_frozen(name) { tokio::time::sleep(Duration::from_millis(100)).await;
tx.send(10).await.unwrap();
}
} }
tokio::time::sleep(Duration::from_millis(100)).await;
} }
fn check_file(filename: &str, path: &str) -> Result<(), CustomError> { fn check_file(filename: &str, path: &str) -> Result<(), CustomError> {
let fileconcat = format!("{}{}", path, filename); let fileconcat = format!("{}{}", path, filename);
@ -326,49 +311,39 @@ fn check_file(filename: &str, path: &str) -> Result<(), CustomError> {
// ?? // ??
async fn service_handler(name: &str, services: &Vec<Services>, tx: mpsc::Sender<u8>) { async fn service_handler(name: &str, services: &Vec<Services>, tx: mpsc::Sender<u8>) {
// println!("service daemon on {}", name); loop {
if is_active(name) { println!("service daemon on {}", name);
for serv in services { if is_active(name) && !is_frozen(name) {
match check_service(&serv.hostname, &serv.port) { for serv in services {
Ok(_) => { if check_service(&serv.hostname, &serv.port).is_err() {
continue; // println!("Service {}:{} is unreachable for process {}", &serv.hostname, &serv.port, &name);
}, match serv.triggers.on_lost.as_str() {
Err(_) => { "stay" => {
println!("Service {}:{} is unreachable for process {}", &serv.hostname, &serv.port, &name); },
match serv.triggers.on_lost.as_str() { "stop" => {
"stay" => { if looped_service_connecting(&name, serv).await.is_err() {
}, tx.send(5).await.unwrap();
"stop" => { return;
match looped_service_connecting(&name, serv).await { }
Ok(_) => {}, },
Err(_) => { "hold" => {
tx.send(5).await.unwrap(); if looped_service_connecting(&name, serv).await.is_err() {
break; tx.send(6).await.unwrap();
}, }
} },
}, _ => {
"hold" => { tx.send(101).await.unwrap();
match looped_service_connecting(&name, serv).await { return;
Ok(_) => {}, },
Err(_) => {
tx.send(6).await.unwrap();
break;
},
}
},
_ => {
tx.send(101).await.unwrap();
return;
},
} }
}, }
} }
} // if is_frozen(name) {
if is_frozen(name) { // tx.send(11).await.unwrap();
tx.send(11).await.unwrap(); // }
} }
tokio::time::sleep(Duration::from_millis(100)).await;
} }
tokio::time::sleep(Duration::from_millis(100)).await;
} }
async fn looped_service_connecting( async fn looped_service_connecting(