fn wait_children(children_list: Vec<ChildData>) {
    // just wait a little for all commands to finish. Otherwise, the last process will not be considered to be finished.
    if !children_list.is_empty() {
        let wait_timeout = std::time::Duration::from_millis(1000);
        thread::sleep(wait_timeout);
    }

    // as child can be None in case of Tcp or Domain socket, need to get rid of these
    for (i, started_child) in children_list
        .iter()
        .filter(|x| x.child.is_some())
        .enumerate()
    {
        // get a mutable reference
        let mut child = started_child.child.as_ref().unwrap().borrow_mut();

        // save pid & path
        let pid = child.id();
        let path = &started_child.path;

        debug!(
            "managing end of process #{}, pid:{}, path:{}",
            i,
            pid,
            path.display()
        );

        // use try_wait() to check if command has exited
        match child.try_wait() {
            // child has already exited. So check output status code if any
            Ok(Some(status)) => debug!(
                "command with path: {}, pid: {} exited with: {}",
                path.display(),
                pid,
                status
            ),

            // child has not exited. Spawn a new thread to wait at most the timeout defined
            Ok(None) => {
                debug!("command has not exited yet, try to wait a little!");

                // now if timeout has not yet occured, start a new thread to wait and kill process ??
                let elapsed = started_child.start_time.unwrap().elapsed().as_secs();

                // if timeout occured, try to kill anyway ;-)
                if elapsed > started_child.timeout {
                    match child.kill() {
                        Ok(_) => info!("process {} killed", child.id()),
                        Err(e) => {
                            if e.kind() == ErrorKind::InvalidInput {
                                info!("process {} already killed", child.id());
                            } else {
                                info!(
                                    "error:{} trying to kill process pid:{}, path: {}",
                                    e,
                                    pid,
                                    path.display()
                                );
                            }
                        }
                    }
                } else {
                    // we'll wait at least the remaining seconds
                    let secs_to_wait = Duration::from_secs(started_child.timeout - elapsed);

                    let _status_code = match child.wait_timeout(secs_to_wait).unwrap() {
                        Some(status) => status.code(),
                        None => {
                            // child hasn't exited yet
                            child.kill().unwrap();
                            child.wait().unwrap().code()
                        }
                    };
                }
            }

            // unlikely error
            Err(e) => eprintln!("error attempting to try_wait: {} for pid:{}", e, pid),
        };
    }
}
/// Manage end of all started processes from clf.
Manage end of all started processes from clf.
Manage end of all started processes from clf .
fn wait_children(children_list: Vec<ChildData>) {
    // just wait a little for all commands to finish. Otherwise, the last process will not be considered to be finished.
    if !children_list.is_empty() {
        let wait_timeout = std::time::Duration::from_millis(1000);
        thread::sleep(wait_timeout);
    }
    // as child can be None in case of Tcp or Domain socket, need to get rid of these
    for (i, started_child) in children_list
        .iter()
        .filter(|x| x.child.is_some())
        .enumerate()
    {
        // get a mutable reference
        let mut child = started_child.child.as_ref().unwrap().borrow_mut();
        // save pid & path
        let pid = child.id();
        let path = &started_child.path;
        debug!(
            "managing end of process #{}, pid:{}, path:{}",
            i,
            pid,
            path.display()
        );
        // use try_wait() to check if command has exited
        match child.try_wait() {
            // child has already exited. So check output status code if any
            Ok(Some(status)) => debug!(
                "command with path: {}, pid: {} exited with: {}",
                path.display(),
                pid,
                status
            ),
            // child has not exited. Spawn a new thread to wait at most the timeout defined
            Ok(None) => {
                debug!("command has not exited yet, try to wait a little!");
                // now if timeout has not yet occured, start a new thread to wait and kill process ??
                let elapsed = started_child.start_time.unwrap().elapsed().as_secs();
                // if timeout occured, try to kill anyway ;-)
                if elapsed > started_child.timeout {
                    match child.kill() {
                        Ok(_) => info!("process {} killed", child.id()),
                        Err(e) => {
                            if e.kind() == ErrorKind::InvalidInput {
                                info!("process {} already killed", child.id());
                            } else {
                                info!(
                                    "error:{} trying to kill process pid:{}, path: {}",
                                    e,
                                    pid,
                                    path.display()
                                );
                            }
                        }
                    }
                } else {
                    // we'll wait at least the remaining seconds
                    let secs_to_wait = Duration::from_secs(started_child.timeout - elapsed);
                    let _status_code = match child.wait_timeout(secs_to_wait).unwrap() {
                        Some(status) => status.code(),
                        None => {
                            // child hasn't exited yet
                            child.kill().unwrap();
                            child.wait().unwrap().code()
                        }
                    };
                }
            }
            // unlikely error
            Err(e) => eprintln!("error attempting to try_wait: {} for pid:{}", e, pid),
        };
    }
}
fn wait_children ( children_list : Vec < ChildData > ) { // just wait a little for all commands to finish. Otherwise, the last process will not be considered to be finished. if ! children_list . is_empty ( ) { let wait_timeout = std :: time :: Duration :: from_millis ( 1000 ) ; thread :: sleep ( wait_timeout ) ; } // as child can be None in case of Tcp or Domain socket, need to get rid of these for ( i , started_child ) in children_list . iter ( ) . filter ( | x | x . child . is_some ( ) ) . enumerate ( ) { // get a mutable reference let mut child = started_child . child . as_ref ( ) . unwrap ( ) . borrow_mut ( ) ; // save pid & path let pid = child . id ( ) ; let path = & started_child . path ; debug ! ( " " i pid path display ( ) ) ; // use try_wait() to check if command has exited match child . try_wait ( ) { // child has already exited. So check output status code if any Ok ( Some ( status ) ) => debug ! ( " " path display ( ) pid status ) , // child has not exited. Spawn a new thread to wait at most the timeout defined Ok ( None ) => { debug ! ( " " ) ; // now if timeout has not yet occured, start a new thread to wait and kill process ?? let elapsed = started_child . start_time . unwrap ( ) . elapsed ( ) . as_secs ( ) ; // if timeout occured, try to kill anyway ;-) if elapsed > started_child . timeout { match child . kill ( ) { Ok ( _ ) => info ! ( " " child id ( ) ) , Err ( e ) => { if e . kind ( ) == ErrorKind :: InvalidInput { info ! ( " " child id ( ) ) ; } else { info ! ( " " e pid path display ( ) ) ; } } } } else { // we'll wait at least the remaining seconds let secs_to_wait = Duration :: from_secs ( started_child . timeout - elapsed ) ; let _status_code = match child . wait_timeout ( secs_to_wait ) . unwrap ( ) { Some ( status ) => status . code ( ) , None => { // child hasn't exited yet child . kill ( ) . unwrap ( ) ; child . wait ( ) . unwrap ( ) . code ( ) } } ; } } // unlikely error Err ( e ) => eprintln ! ( " " e pid ) , } ; } }
Manage end of all started processes from clf.
Manage end of all started processes from clf .
children_list
Vec<ChildData>
children_list
Vec<ChildData>