Struct process_events_streaming::ProcessRequest
source · pub struct ProcessRequest {
pub request_id: u32,
pub use_shell: bool,
pub non_blocking_mode: bool,
pub cmd_line: Vec<Vec<String>>,
pub callback: Option<Arc<dyn Fn(&ProcessEvent, &ProcessData<'_>) -> Option<bool> + 'static>>,
}Expand description
A request structure to start a process
Fields
request_id: u32Custom unique numeric id to relate the various callbacks for a particular process execution session
use_shell: boolUse shell mode or direct executable path based execution
non_blocking_mode: boolUse blocking or non blocking mode using internal threads
cmd_line: Vec<Vec<String>>(2D Array) Vector of command line along with arguments. For a single command line one vector element is enough. For the pipe line use case where output of one command to provide to the next command, use Vector of command lines.
callback: Option<Arc<dyn Fn(&ProcessEvent, &ProcessData<'_>) -> Option<bool> + 'static>>Register callback to get various events and process output, for no callbacks use None
Implementations
sourceimpl ProcessRequest
impl ProcessRequest
sourcepub fn start(process_request: ProcessRequest) -> Option<Result<JoinHandle<()>>>
pub fn start(process_request: ProcessRequest) -> Option<Result<JoinHandle<()>>>
Run a process based on the provided process request which is events based multi process execution(blocking & non-blocking modes) in parallel and with data streaming
Generates various events ProcessEvent according to the process’s life-cycle, process’s information and data ProcessData associated with that event
Arguments
ProcessRequest : ProcessRequest // A request structure to start a process
Return
For Blocking mode it returns None and for Non-Blocking mode it will return [Some(io::Result<JoinHandle<()>>)], so the caller can join & wait for the process completion if needed!
Examples
// Setup callback for the process events and data streaming
//
// use process_events_streaming::{ProcessRequest, ProcessData, ProcessEvent};
// use std::{thread, time::Duration};
// let callback = |status: &ProcessEvent, data: &ProcessData| -> Option<bool> {
// match status {
// ProcessEvent::Started => {
// println!(
// "Event {:?} | req-id {} | Pids: {:?}",
// status,
// data.request.as_ref().unwrap().request_id,
// data.pids
// );
// }
// ProcessEvent::IOData => {
// println!(
// "Event {:?} | req-id {} | # {} : {}",
// status,
// data.request.as_ref().unwrap().request_id,
// data.line_number,
// data.line
// );
//
// //demo how to kill/stop
// // //using kill api
// //_ = data.kill();
// // //or return false to exit the process, based on the line_number value
// // if data.line_number == 1 {
// // return Some(false);
// // }
// // // or return false to exit the process, if a condition is true based on the output data
// // if data.line.contains("Sandy") {
// // return Some(false);
// // }
// }
// other => {
// if !data.line.is_empty() {
// println!(
// "Event {:?} | req-id {} | additional detail(s): {}",
// other,
// data.request.as_ref().unwrap().request_id,
// data.line
// );
// } else {
// println!(
// "Event {:?} | req-id {}",
// other,
// data.request.as_ref().unwrap().request_id
// );
// }
// }
// }
// Some(true)
// };
//
//
// ProcessRequest::start(ProcessRequest {
// request_id: 161,
// callback: Some(Arc::new(callback)),
// use_shell: true,
// cmd_line: vec![vec![String::from("dir")], vec![String::from("sort")]],
// non_blocking_mode: true,
// });
//
// //check & wait for the non blocking mode
// if result1.is_some() {
// if result1.as_ref().unwrap().is_ok() {
// println!(
// "Start - join waiting over in non blocking mode {:?}",
// result1
// .unwrap()
// .unwrap()
// .join()
// .expect("Couldn't join on the associated thread")
// );
// } else {
// println!(
// "Start - Error in non blocking mode {:?}",
// result1.unwrap().err()
// );
// }
// } else {
// println!("Start - It was a blocking mode, so nothing to wait for!");
// }
//
// println!(
// "test_using_sh_output_streaming, start calc in windows {:?}",
// ProcessRequest::start(ProcessRequest {
// request_id: 191,
// callback: Some(Arc::new(callback)),
// use_shell: true,
// cmd_line: vec![vec![String::from("calc")]],
// non_blocking_mode: false,
// }));