docs: prcs
parent
174779bfcb
commit
c14a6f943b
|
|
@ -1,10 +1,22 @@
|
||||||
use crate::options::structs::CustomError;
|
use crate::options::structs::CustomError;
|
||||||
use log::{error, warn};
|
use log::{error, warn};
|
||||||
use std::io;
|
|
||||||
use std::process::{Command, Output};
|
use std::process::{Command, Output};
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
use tokio::time::Duration;
|
use tokio::time::Duration;
|
||||||
|
|
||||||
|
/// # Fn `get_pid`
|
||||||
|
/// ## for initializing process of unstoppable grubbing metrics.
|
||||||
|
///
|
||||||
|
/// *input* : `&str`
|
||||||
|
///
|
||||||
|
/// *output* : `None` if cant get process PID | `Some(Output)` on success
|
||||||
|
///
|
||||||
|
/// *initiator* : fn `is_frozen`, fn `utils::files::file_handler`, fn `utils::files::service_handler`
|
||||||
|
///
|
||||||
|
/// *managing* : process name
|
||||||
|
///
|
||||||
|
/// *depends on* : -
|
||||||
|
///
|
||||||
pub async fn get_pid(name: &str) -> Option<Output> {
|
pub async fn get_pid(name: &str) -> Option<Output> {
|
||||||
let name = Arc::new(name.to_string());
|
let name = Arc::new(name.to_string());
|
||||||
let res =
|
let res =
|
||||||
|
|
@ -20,8 +32,20 @@ pub async fn get_pid(name: &str) -> Option<Output> {
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ! can be with bug !!!
|
|
||||||
// * APPROVED
|
/// # Fn `is_active`
|
||||||
|
/// ## for checking process's activity state
|
||||||
|
///
|
||||||
|
/// *input* : `&str`
|
||||||
|
///
|
||||||
|
/// *output* : `true` if process running | `false` if not
|
||||||
|
///
|
||||||
|
/// *initiator* : fn `utils::files::file_handler`, fn `utils::files::service_handler`
|
||||||
|
///
|
||||||
|
/// *managing* : process name
|
||||||
|
///
|
||||||
|
/// *depends on* : -
|
||||||
|
///
|
||||||
pub async fn is_active(name: &str) -> bool {
|
pub async fn is_active(name: &str) -> bool {
|
||||||
let arc_name = Arc::new(name.to_string());
|
let arc_name = Arc::new(name.to_string());
|
||||||
tokio::task::spawn_blocking(move || {
|
tokio::task::spawn_blocking(move || {
|
||||||
|
|
@ -38,7 +62,19 @@ pub async fn is_active(name: &str) -> bool {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
// T is for stopped processes
|
/// # Fn `is_frozen`
|
||||||
|
/// ## for checking process's hibernation state
|
||||||
|
///
|
||||||
|
/// *input* : `&str`
|
||||||
|
///
|
||||||
|
/// *output* : `true` if process is frozen | `false` if not
|
||||||
|
///
|
||||||
|
/// *initiator* : fn `utils::files::file_handler`, fn `utils::files::service_handler`
|
||||||
|
///
|
||||||
|
/// *managing* : process name
|
||||||
|
///
|
||||||
|
/// *depends on* : fn `get_pid`
|
||||||
|
///
|
||||||
pub async fn is_frozen(name: &str) -> bool {
|
pub async fn is_frozen(name: &str) -> bool {
|
||||||
let temp: Output;
|
let temp: Output;
|
||||||
if let Some(output) = get_pid(name).await {
|
if let Some(output) = get_pid(name).await {
|
||||||
|
|
@ -66,6 +102,20 @@ pub async fn is_frozen(name: &str) -> bool {
|
||||||
.unwrap()
|
.unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # Fn `terminate_process`
|
||||||
|
/// ## for stop current process
|
||||||
|
///
|
||||||
|
/// *input* : `&str`
|
||||||
|
///
|
||||||
|
/// *output* : -
|
||||||
|
///
|
||||||
|
/// *initiator* : fn `utils::files::file_handler`, fn `utils::files::service_handler`, fn `utils::run_daemons`
|
||||||
|
///
|
||||||
|
/// *managing* : process name
|
||||||
|
///
|
||||||
|
/// *depends on* : -
|
||||||
|
///
|
||||||
pub async fn terminate_process(name: &str) {
|
pub async fn terminate_process(name: &str) {
|
||||||
let _ = Command::new("pkill")
|
let _ = Command::new("pkill")
|
||||||
.arg(name)
|
.arg(name)
|
||||||
|
|
@ -76,6 +126,19 @@ pub async fn terminate_process(name: &str) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # Fn `terminate_process`
|
||||||
|
/// ## for freeze/hibernate current process
|
||||||
|
///
|
||||||
|
/// *input* : `&str`
|
||||||
|
///
|
||||||
|
/// *output* : -
|
||||||
|
///
|
||||||
|
/// *initiator* : fn `utils::run_daemons`
|
||||||
|
///
|
||||||
|
/// *managing* : process name
|
||||||
|
///
|
||||||
|
/// *depends on* : -
|
||||||
|
///
|
||||||
pub async fn freeze_process(name: &str) {
|
pub async fn freeze_process(name: &str) {
|
||||||
let _ = Command::new("pkill")
|
let _ = Command::new("pkill")
|
||||||
.args(["-STOP", name])
|
.args(["-STOP", name])
|
||||||
|
|
@ -85,6 +148,20 @@ pub async fn freeze_process(name: &str) {
|
||||||
std::process::exit(101);
|
std::process::exit(101);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # Fn `unfreeze_process`
|
||||||
|
/// ## for unfreeze/hibernate current process
|
||||||
|
///
|
||||||
|
/// *input* : `&str`
|
||||||
|
///
|
||||||
|
/// *output* : -
|
||||||
|
///
|
||||||
|
/// *initiator* : fn `utils::run_daemons`
|
||||||
|
///
|
||||||
|
/// *managing* : process name
|
||||||
|
///
|
||||||
|
/// *depends on* : -
|
||||||
|
///
|
||||||
pub async fn unfreeze_process(name: &str) {
|
pub async fn unfreeze_process(name: &str) {
|
||||||
let _ = Command::new("pkill")
|
let _ = Command::new("pkill")
|
||||||
.args(["-CONT", name])
|
.args(["-CONT", name])
|
||||||
|
|
@ -94,12 +171,39 @@ pub async fn unfreeze_process(name: &str) {
|
||||||
std::process::exit(101);
|
std::process::exit(101);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # Fn `restart_process`
|
||||||
|
/// ## for restarting current process
|
||||||
|
///
|
||||||
|
/// *input* : `&str`, &str
|
||||||
|
///
|
||||||
|
/// *output* : -
|
||||||
|
///
|
||||||
|
/// *initiator* : fn `utils::run_daemons`
|
||||||
|
///
|
||||||
|
/// *managing* : process name and path to its exec file
|
||||||
|
///
|
||||||
|
/// *depends on* : fn `start_process`, fn `terminate_process`
|
||||||
|
///
|
||||||
pub async fn restart_process(name: &str, path: &str) -> Result<(), CustomError> {
|
pub async fn restart_process(name: &str, path: &str) -> Result<(), CustomError> {
|
||||||
terminate_process(name).await;
|
terminate_process(name).await;
|
||||||
tokio::time::sleep(Duration::from_millis(100)).await;
|
tokio::time::sleep(Duration::from_millis(100)).await;
|
||||||
start_process(name, path).await
|
start_process(name, path).await
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// # Fn `start_process`
|
||||||
|
/// ## for starting current process
|
||||||
|
///
|
||||||
|
/// *input* : `&str`, &str
|
||||||
|
///
|
||||||
|
/// *output* : -
|
||||||
|
///
|
||||||
|
/// *initiator* : fn `restart_process`
|
||||||
|
///
|
||||||
|
/// *managing* : process name and path to its exec file
|
||||||
|
///
|
||||||
|
/// *depends on* : -
|
||||||
|
///
|
||||||
pub async fn start_process(name: &str, path: &str) -> Result<(), CustomError> {
|
pub async fn start_process(name: &str, path: &str) -> Result<(), CustomError> {
|
||||||
// let runsh = format!("{} {}", "exec", path);
|
// let runsh = format!("{} {}", "exec", path);
|
||||||
let mut command = Command::new(path);
|
let mut command = Command::new(path);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue