ProcessRequest

Struct 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<'_>) -> ProcessResult + 'static>>,
}
Expand description

A request structure to start a process

Fields§

§request_id: u32

Custom unique numeric id to relate the various callbacks for a particular process execution session

§use_shell: bool

Use shell mode or direct executable path based execution

§non_blocking_mode: bool

Use 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<'_>) -> ProcessResult + 'static>>

Register callback to get various events and process output, for no callbacks use None

Implementations§

Source§

impl ProcessRequest

Source

pub fn start(process_request: ProcessRequest) -> ProcessResult

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

ProcessResult : ProcessResult // Contains join handle for non-blocking and data variables For Blocking mode use join_handle Option<io::Result<JoinHandle<ProcessResult>>>, so the caller can join & wait for the process completion if needed! In the callback set the custom data to be retrieved once process execution is over, which will be returned in response of the join call.

§Examples
// Setup callback for the process events and data streaming
//
// use process_events_streaming::{ProcessRequest, ProcessResult, ProcessData, ProcessEvent};
// use std::{thread, time::Duration};
//      let callback = |status: &ProcessEvent, data: &ProcessData| -> ProcessResult {
//          match status {
//              ProcessEvent::Started => {
//                  println!(
//                      "Event {:?} | req-id {}  | Pids: {:?}",
//                      status,
//                      data.request.as_ref().unwrap().request_id,
//                      data.child_pids()
//                  );
//              }
//              ProcessEvent::IOData => {
//                  println!(
//                      "Event {:?} | req-id {} | # {} : {}",
//                      status,
//                      data.request.as_ref().unwrap().request_id,
//                      data.line_number,
//                      data.line
//                  );
//                  //now assume we want to exit the process with some data
//                  let mut result = ProcessResult::new();
//                  result.set_exit_flag_and_success(true, Ok(true));
//                  result.data_num = Some(8111981);
//                  result.data_vec_str = Some(vec![String::from("I found my hidden data!")]);
//                  return result;
//
//                  //demo how to kill/stop
//                  //_ = data.kill();
//              }
//              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
//                      );
//                  }
//              }
//          }
//          ProcessResult::new()
//      };
//
//
//
//    let request2 = ProcessRequest {
//        request_id: 151,
//        callback: Some(Arc::new(callback)),
//        use_shell: true,
//        cmd_line: vec![vec![
//            String::from("echo"),
//            String::from("stdout"),
//            String::from("&"),
//            String::from("echo"),
//            String::from("stderr"),
//            String::from(">&2"),
//        ]],
//        non_blocking_mode: true,
//    };
//
//    // non Blocking mode
//    let process_result = ProcessRequest::start(request2);
//    println!("Returned from Start! of non blocking");
//
//    let mut internal_data = ProcessResult::new();
//    //check & wait for the non blocking mode
//    if process_result.join_handle.is_some() {
//        if process_result.join_handle.as_ref().unwrap().is_ok() {
//            internal_data = process_result.join_handle.unwrap().unwrap().join().unwrap();
//            println!(
//                "Start - join waiting over in non blocking mode {:?}",
//                internal_data
//            );
//        } else {
//            internal_data.success = Err(process_result.join_handle.unwrap().err().unwrap());
//            println!(
//                "Start - Error in non blocking mode {:?}",
//                internal_data.success
//            );
//        }
//    } else {
//        internal_data = process_result;
//    }
//    println!("result dump : {:?}", internal_data);
//
//    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,
//     }));

Trait Implementations§

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.