add method status_update and do optimisation on up and down verbose command.

This commit is contained in:
primardj 2024-04-13 23:41:11 +01:00
parent 94f8aebd3a
commit cb8768b6f5
2 changed files with 65 additions and 21 deletions

View file

@ -9,9 +9,6 @@ use std::time::Duration;
pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_obj::StatusObj) -> i32 {
// Return 0 in case everything worked fine, return 1 if timeout or error.
dbg!(svwait_var);
dbg!(verbose);
let mut time_wait = 0;
// execute command.
@ -25,18 +22,16 @@ pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_
if sv.sent_signal(b"u") {
if verbose == 1 {
loop {
// TODO, Make a method update_sv
let sv_option = status_obj::StatusObj::new(sv.get_path(), sv.get_name());
let mut sv_new = match sv_option {
Some(service) => service,
None => return 1,
};
if sv_new.is_up() == true {
misc::print_status(sv_new);
let status = sv.update_status();
if status == 1 {
return 1;
}
if sv.is_up() == true {
misc::print_status(sv);
return 0
}
if time_wait >= svwait_var {
println!("fail: {}: timeout", sv_new.get_name());
println!("fail: {}: timeout", sv.get_name());
return 1
}
@ -56,18 +51,16 @@ pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_
if sv.sent_signal(b"d") {
if verbose == 1 {
loop {
// TODO: Use the update_service method instead of creating another sv.
let sv_option = status_obj::StatusObj::new(sv.get_path(), sv.get_name());
let mut sv_new = match sv_option {
Some(service) => service,
None => return 1,
};
if sv_new.is_up() == false {
misc::print_status(sv_new);
let status = sv.update_status();
if status == 1 {
return 1;
}
if sv.is_up() == false {
misc::print_status(sv);
return 0
}
if time_wait >= svwait_var {
println!("fail: {}: timeout", sv_new.get_name());
println!("fail: {}: timeout", sv.get_name());
return 1
}

View file

@ -113,6 +113,57 @@ impl StatusObj {
})
}
pub fn update_status(&mut self) -> u32 {
// Update only status related fields.
// return 1 in case there is a problem.
// return 0 if everything is altight.
let status_option = fs::OpenOptions::new().read(true).open("supervise/status");
let mut status = match status_option {
Ok(file) => file,
Err(..) => {
println!("warning: {}: unable to open supervise/status: file does not exist", self.svname);
return 1;
},
};
let mut contents: Vec<u8> = Vec::new();
let option_size = status.read_to_end(&mut contents);
let size = match option_size {
Ok(size) => size,
Err(..) => {
println!("warning: {}: couldn't read supervise/status", self.svname);
return 1
},
};
if size != 20 {
println!("warning: {}: bad supervise/status format", self.svname);
return 1
}
let time_buf: Vec<u8> = contents[0..8].to_vec();
//let nano_buf: Vec<u8> = contents[8..12].to_vec();
let pid_buf: Vec<u8> = contents[12..16].to_vec();
let seconds = misc::return_u8_in_u64(time_buf);
//let nano = misc::return_u8_in_u32(nano_buf);
let pid_found = misc::return_reverse_u8_in_u32(pid_buf);
let pause_run_raw = misc::make_pause_run(contents[16]); // done
let up_down_raw = misc::make_up_down(contents[17]); // done
let term_sig_raw = misc::make_term_sig(contents[18]); // done
let run_finish_raw = misc::make_run_finish(contents[19]); // done
self.time = seconds;
self.pid = pid_found;
self.pause_run = pause_run_raw;
self.up_down = up_down_raw;
self.term_sig = term_sig_raw;
self.run_finish = run_finish_raw;
return 0;
}
pub fn get_status_string(&mut self) -> String {
// return the status for this service in particular.