other smalls optimisations.

This commit is contained in:
primardj 2024-04-15 23:38:24 +01:00
parent 3f57120e53
commit 14b762520f
2 changed files with 26 additions and 45 deletions

View file

@ -13,39 +13,15 @@ pub fn sent_signal( svwait_var: i32, verbose: i8, command: &str, mut sv: status_
}, },
"u" => { "u" => {
// case where command is up // case where command is up
if sv.sent_signal(b"u") { return misc::run_verbose(sv.sent_signal(b"u"), true, verbose, svwait_var, sv);
if verbose == 1 {
return misc::run_verbose(true, svwait_var, sv);
} else {
return 0;
}
} else {
return 1;
}
}, },
"d" => { "d" => {
// case where command is up // case where command is up
if sv.sent_signal(b"d") { return misc::run_verbose(sv.sent_signal(b"d"), false, verbose, svwait_var, sv);
if verbose == 1 {
return misc::run_verbose(false, svwait_var, sv);
} else {
return 0;
}
} else {
return 1;
}
}, },
"o" => { "o" => {
// case where command is run once. // case where command is run once.
if sv.sent_signal(b"o") { return misc::run_verbose(sv.sent_signal(b"o"), true, verbose, svwait_var, sv);
if verbose == 1 {
return misc::run_verbose(true, svwait_var, sv);
} else {
return 0;
}
} else {
return 1;
}
}, },
other => { other => {
println!("Error, command {} not implemented.", other); // TODO : Put the real error println!("Error, command {} not implemented.", other); // TODO : Put the real error

View file

@ -24,26 +24,31 @@ pub fn print_status(mut sv: status_obj::StatusObj) -> i32 {
} }
pub fn run_verbose(state: bool, svwait_var: i32, mut sv: status_obj::StatusObj) -> i32 { pub fn run_verbose(sent_signal: bool, state: bool, verbose: i8, svwait_var: i32, mut sv: status_obj::StatusObj) -> i32 {
// if the state is true, wait for the service to run. // if the state is true, wait for the service to run.
// If the state is false, wait for the service to down. // If the state is false, wait for the service to down.
let mut time_wait = 0; if sent_signal && verbose == 1 {
loop { let mut time_wait = 0;
let status = sv.update_status(); loop {
if status == 1 { let status = sv.update_status();
return 1; if status == 1 {
return 1;
}
if sv.is_up() == state {
println!("ok: {}", 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));
} }
if sv.is_up() == state { } else if sent_signal && verbose == 0 {
println!("ok: {}", sv.get_status_string()); return 0
return 0 } else {
} // Case sent_signal != true.
if time_wait >= svwait_var { return 1
println!("timeout: {}", sv.get_status_string());
return 1
}
time_wait += 1;
sleep(Duration::from_secs(1));
} }
} }