proc_heim/process/model/
process.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
use std::process::ExitStatus;

use tokio::process::Child;

use crate::process::{
    log_reader::LogReaderHandle, reader::MessageReaderHandle, writer::MessageWriterHandle,
};

#[derive(Debug)]
pub(crate) struct Process {
    pub child: Child,
    pub message_writer: Option<MessageWriterHandle>,
    pub message_reader: Option<MessageReaderHandle>,
    pub log_reader: Option<LogReaderHandle>,
}

#[derive(Debug, Default)]
pub(crate) struct ProcessBuilder {
    pub message_writer: Option<MessageWriterHandle>,
    pub message_reader: Option<MessageReaderHandle>,
    pub log_reader: Option<LogReaderHandle>,
}

impl ProcessBuilder {
    pub fn build(self, child: Child) -> Process {
        Process {
            child,
            message_writer: self.message_writer,
            message_reader: self.message_reader,
            log_reader: self.log_reader,
        }
    }
}

/// Type representing information about spawned process.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ProcessInfo {
    pid: Option<u32>,
    exit_status: Option<ExitStatus>,
}

impl ProcessInfo {
    pub(crate) fn new(pid: Option<u32>, exit_status: Option<ExitStatus>) -> Self {
        Self { pid, exit_status }
    }

    /// Retrieve information about OS-assigned process identifier. For more information see [`Child::id`](tokio::process::Child::id) docs.
    pub fn pid(&self) -> Option<u32> {
        self.pid
    }

    /// Retrieve information about process exit status.
    /// If the process has exited, then `Some(status)` is returned.
    /// If the exit status is not available at this time then `None` is returned.
    pub fn exit_status(&self) -> Option<ExitStatus> {
        self.exit_status
    }

    /// Check if process is still running. This is equivalent of `self.exit_status().is_none()`.
    pub fn is_running(&self) -> bool {
        self.exit_status.is_none()
    }
}