tame_protocol/process_monitor/
flow.rs

1use rill_protocol::flow::core::{Flow, TimedEvent};
2use rill_protocol::flow::location::Location;
3use rill_protocol::io::provider::StreamType;
4use serde::{Deserialize, Serialize};
5
6pub const LOCATION: Location = Location::new("system:process_monitor");
7
8pub type Pid = u32;
9pub type ExitCode = i32;
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
12pub struct Command {
13    pub command: String,
14    pub args: Vec<String>,
15    pub workdir: String,
16}
17
18#[derive(Debug, Clone, Serialize, Deserialize)]
19pub enum ProcessStatus {
20    NotDetected,
21    Alive { pid: Option<Pid> },
22    Terminated { code: Option<ExitCode> },
23}
24
25#[derive(Debug, Clone, Serialize, Deserialize)]
26pub struct ProcessMonitorState {
27    pub command: Command,
28    pub process_status: ProcessStatus,
29}
30
31#[allow(clippy::new_without_default)]
32impl ProcessMonitorState {
33    pub fn new(command: Command) -> Self {
34        Self {
35            command,
36            process_status: ProcessStatus::NotDetected,
37        }
38    }
39}
40
41impl Flow for ProcessMonitorState {
42    type Action = ProcessMonitorAction;
43    type Event = ProcessMonitorEvent;
44
45    fn stream_type() -> StreamType {
46        StreamType::from("rillrate::agent::process_monitor::v0")
47    }
48
49    fn apply(&mut self, event: TimedEvent<Self::Event>) {
50        match event.event {
51            ProcessMonitorEvent::AssignPid { pid } => {
52                self.process_status = ProcessStatus::Alive { pid };
53            }
54            ProcessMonitorEvent::SetExitCode { code } => {
55                self.process_status = ProcessStatus::Terminated { code };
56            }
57        }
58    }
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub enum ProcessMonitorAction {
63    /// This action is available for terminated processes only
64    Respawn,
65    /// This action is available for alive processes only
66    Kill,
67}
68
69#[derive(Debug, Clone, Serialize, Deserialize)]
70pub enum ProcessMonitorEvent {
71    AssignPid { pid: Option<Pid> },
72    SetExitCode { code: Option<ExitCode> },
73}