swarm_commander/
event.rs

1use crate::ProcInfo;
2
3/// All the events that a command can send during its lifecycle
4#[derive(Debug)]
5pub enum RunnerEvent<T> {
6    /// Raised when a command start step is done. The process can fail (ex command not found) or success
7    RunnerStartEvent(RunnerStartEvent),
8    /// Raised when a command stop step is done. The process can fail (ex invalid pid) or success
9    RunnerStopEvent(RunnerStopEvent),
10    /// The output (stdout and stderr) is processed by your own parser. Your parser should return this events when
11    /// something interersting happened
12    RunnerStatusEvent(StatusEvent<T>),
13    /// Once the process is done, the last output lines are sent. The reason is basically to debug in case of error
14    RunnerLogEvent(RunnerLogEvent)
15}
16
17/// It references the id of the process and the content is generic. You decide what to store in the field data.
18#[derive(Debug)]
19pub struct StatusEvent<T> {
20    /// Id of the command
21    pub id: String,
22    /// Your custom parsed data
23    pub data: T
24}
25
26/// When a start event finished this data is filled and returned. Pid is 0 in case of error
27#[derive(Debug)]
28pub struct RunnerStartEvent {
29    /// If the start process was ok
30    pub success: bool,
31    /// Pid of the created command
32    pub pid: u32,
33    /// Id of the command
34    pub id: String,
35}
36
37/// When a stop event finished this data is filled and returned.
38#[derive(Debug)]
39pub struct RunnerStopEvent {
40    /// If the stop process was ok
41    pub success: bool,
42    /// Id of the command
43    pub id: String,
44    /// Command exit status code. 0 means OK
45    pub code: Option<i32>,
46    /// Information about the terminated program
47    pub info: ProcInfo,
48    
49}
50
51/// The event source, stderr or stdout
52#[derive(Debug)]
53pub enum StdType {
54    /// The event comes from stderr
55    Err,
56    /// The event comes from stdout
57    Out
58}
59
60/// The last lines of the command. It is useful when debugging because in case of unexpected error, most of times the command just explains you what happened
61#[derive(Debug)]
62pub struct RunnerLogEvent {
63    /// The source of the event
64    pub std: StdType,
65    /// The last lines
66    pub log: std::collections::VecDeque<String>,
67    /// The id of the command
68    pub id: String,
69}