small optimisation

This commit is contained in:
primardj 2024-04-15 23:19:02 +01:00
parent d5064ae281
commit b7ad89359b
2 changed files with 8 additions and 27 deletions

View file

@ -3,15 +3,9 @@ mod misc;
use crate::status_obj; use crate::status_obj;
use std::thread::sleep;
use std::time::Duration;
pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_obj::StatusObj) -> i32 { 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. // Return 0 in case everything worked fine, return 1 if timeout or error.
let mut time_wait = 0;
// execute command.
if command == "s" { if command == "s" {
// case command is status // case command is status
return misc::print_status(sv); return misc::print_status(sv);
@ -21,7 +15,7 @@ pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_
// case where command is up // case where command is up
if sv.sent_signal(b"u") { if sv.sent_signal(b"u") {
if verbose == 1 { if verbose == 1 {
return misc::run_verbose(svwait_var, sv); return misc::run_verbose(true, svwait_var, sv);
} else { } else {
return 0; return 0;
} }
@ -34,23 +28,7 @@ pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_
// case where command is up // case where command is up
if sv.sent_signal(b"d") { if sv.sent_signal(b"d") {
if verbose == 1 { if verbose == 1 {
loop { return misc::run_verbose(false, svwait_var, sv);
let status = sv.update_status();
if status == 1 {
return 1;
}
if sv.is_up() == false {
println!("{}", sv.get_status_string());
return 0
}
if time_wait >= svwait_var {
println!("timeout: {}", sv.get_status_string());
return 1
}
time_wait += 1;
sleep(Duration::from_secs(1));
}
} else { } else {
return 0; return 0;
} }
@ -63,7 +41,7 @@ pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_
// case where command is run once. // case where command is run once.
if sv.sent_signal(b"o") { if sv.sent_signal(b"o") {
if verbose == 1 { if verbose == 1 {
return misc::run_verbose(svwait_var, sv); return misc::run_verbose(true, svwait_var, sv);
} else { } else {
return 0; return 0;
} }
@ -72,6 +50,7 @@ pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_
} }
} }
println!("Error, command not implemented."); println!("Error, command not implemented.");
return 1; return 1;
} }

View file

@ -24,14 +24,16 @@ pub fn print_status(mut sv: status_obj::StatusObj) -> i32 {
} }
pub fn run_verbose(svwait_var: i32, mut sv: status_obj::StatusObj) -> i32 { pub fn run_verbose(state: bool, svwait_var: i32, mut sv: status_obj::StatusObj) -> i32 {
// if the state is true, wait for the service to run.
// If the state is false, wait for the service to down.
let mut time_wait = 0; let mut time_wait = 0;
loop { loop {
let status = sv.update_status(); let status = sv.update_status();
if status == 1 { if status == 1 {
return 1; return 1;
} }
if sv.is_up() == true { if sv.is_up() == state {
println!("ok: {}", sv.get_status_string()); println!("ok: {}", sv.get_status_string());
return 0 return 0
} }