finish basic status command

This commit is contained in:
primardj 2024-04-12 00:17:31 +01:00
parent 37783dffe7
commit 968b9e7e87
2 changed files with 47 additions and 19 deletions

View file

@ -7,11 +7,13 @@ use std::fs;
use std::io::prelude::*; use std::io::prelude::*;
use std::time::SystemTime; use std::time::SystemTime;
pub struct StatusObj { pub struct StatusObj {
svname: String, // Service name svname: String, // Service name
svpath: String, // Service path svpath: String, // Service path
time: u64, // 0 -> 7 time: u64, // 0 -> 7
nano_seconds: u32, // 8 -> 11 //nano_seconds: u32, // 8 -> 11
pid: u32, // 12 -> 15 pid: u32, // 12 -> 15
pause_run: bool, // 16 pause_run: bool, // 16
up_down: char, // 17 up_down: char, // 17
@ -38,6 +40,19 @@ impl StatusObj {
println!("warning: {}: unable to open supervise/ok: file does not exist", sv); println!("warning: {}: unable to open supervise/ok: file does not exist", sv);
return None; return None;
} }
// TODO: check if there is a process reading the pipe.
// piste : https://docs.rs/tokio/latest/tokio/net/unix/pipe/struct.OpenOptions.html
//let mut control_option = fs::File::open("supervise/control");
//let status_option = fs::OpenOptions::new().read(true).open("supervise/control");
//match control_option {
// Ok(_file) => {},
// Err(ee) => {
// println!("fail: {}: runsv not running {}", sv, ee);
// return None;
// },
//};
let status_option = fs::OpenOptions::new().read(true).open("supervise/status"); let status_option = fs::OpenOptions::new().read(true).open("supervise/status");
let mut status = match status_option { let mut status = match status_option {
Ok(file) => file, Ok(file) => file,
@ -58,6 +73,8 @@ impl StatusObj {
}; };
if size != 20 { if size != 20 {
dbg!(size);
dbg!(contents);
println!("warning: {}: bad supervise/status format", sv); println!("warning: {}: bad supervise/status format", sv);
return None return None
} }
@ -77,11 +94,11 @@ impl StatusObj {
} }
let time_buf: Vec<u8> = contents[0..8].to_vec(); let time_buf: Vec<u8> = contents[0..8].to_vec();
let nano_buf: Vec<u8> = contents[8..12].to_vec(); //let nano_buf: Vec<u8> = contents[8..12].to_vec();
let pid_buf: Vec<u8> = contents[12..16].to_vec(); let pid_buf: Vec<u8> = contents[12..16].to_vec();
let seconds = misc::return_u8_in_u64(time_buf); let seconds = misc::return_u8_in_u64(time_buf);
let nano = misc::return_u8_in_u32(nano_buf); //let nano = misc::return_u8_in_u32(nano_buf);
let pid_found = misc::return_reverse_u8_in_u32(pid_buf); let pid_found = misc::return_reverse_u8_in_u32(pid_buf);
let pause_run_raw = misc::make_pause_run(contents[16]); // done let pause_run_raw = misc::make_pause_run(contents[16]); // done
@ -93,7 +110,7 @@ impl StatusObj {
svname: sv, svname: sv,
svpath: path_string, svpath: path_string,
time: seconds, time: seconds,
nano_seconds: nano, //nano_seconds: nano,
pid: pid_found, pid: pid_found,
pause_run: pause_run_raw, pause_run: pause_run_raw,
up_down: up_down_raw, up_down: up_down_raw,
@ -104,22 +121,13 @@ impl StatusObj {
}) })
} }
pub fn get_status_string(&mut self) -> String { pub fn get_status_string(&mut self) -> String {
//dbg!(&self.svname);
dbg!(&self.svpath);
//dbg!(self.time);
dbg!(self.nano_seconds);
//dbg!(self.pid);
dbg!(self.pause_run);
dbg!(self.up_down);
dbg!(self.term_sig);
//dbg!(self.run_finish);
//dbg!(self.down);
dbg!(self.log);
// TODO
let status_sv; let status_sv;
let pid_string: String; let pid_string: String;
let down_string; let down_string;
let pause_string;
let ud_string;
let term_string;
if self.run_finish == false { if self.run_finish == false {
status_sv = "down:"; status_sv = "down:";
pid_string = "".to_owned(); pid_string = "".to_owned();
@ -128,6 +136,11 @@ impl StatusObj {
} else { } else {
down_string = ""; down_string = "";
} }
if self.up_down == 'u' {
ud_string = ", want up";
} else {
ud_string = "";
}
} else { } else {
status_sv = "run:"; status_sv = "run:";
pid_string = "(pid ".to_owned() + &self.pid.to_string() + ") "; pid_string = "(pid ".to_owned() + &self.pid.to_string() + ") ";
@ -136,6 +149,21 @@ impl StatusObj {
} else { } else {
down_string = ""; down_string = "";
} }
if self.up_down == 'd' {
ud_string = ", want down";
} else {
ud_string = "";
}
}
if self.pause_run == true {
pause_string = ", paused";
} else {
pause_string = "";
}
if self.term_sig == true {
term_string = ", got TERM";
} else {
term_string = "";
} }
let time_repaired: u64 = self.time - 4611686018427387914; let time_repaired: u64 = self.time - 4611686018427387914;
@ -146,9 +174,8 @@ impl StatusObj {
}; };
let dif_time = sys_time - time_repaired; let dif_time = sys_time - time_repaired;
// Make the string to return
// Make return string with what made upper there. let return_string: String = status_sv.to_owned() + " " + &self.svname + ": " + &pid_string + &dif_time.to_string() + "s" + down_string + pause_string + ud_string + term_string;
let return_string: String = status_sv.to_owned() + " " + &self.svname + ": " + &pid_string + &dif_time.to_string() + "s" + down_string;
return return_string; return return_string;
} }

View file

@ -45,6 +45,7 @@ pub fn return_reverse_u8_in_u32(mut table: Vec<u8>) -> u32 {
return r return r
} }
#[allow(dead_code)]
pub fn return_u8_in_u32(table: Vec<u8>) -> u32 { pub fn return_u8_in_u32(table: Vec<u8>) -> u32 {
// Table make 4 values u8 in one u32 // Table make 4 values u8 in one u32
if table.len() != 4 { if table.len() != 4 {