Skip to main content

folk_plugin_process/
config.rs

1use std::collections::HashMap;
2use std::path::PathBuf;
3use std::time::Duration;
4
5use serde::{Deserialize, Serialize};
6
7#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
8#[serde(rename_all = "snake_case")]
9pub enum RestartPolicy {
10    Always,
11    OnFailure,
12    Never,
13}
14
15#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
16#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
17pub enum StopSignal {
18    #[default]
19    Term,
20    Int,
21    Quit,
22}
23
24#[cfg(unix)]
25impl StopSignal {
26    pub fn as_libc_signal(&self) -> libc::c_int {
27        match self {
28            Self::Term => libc::SIGTERM,
29            Self::Int => libc::SIGINT,
30            Self::Quit => libc::SIGQUIT,
31        }
32    }
33}
34
35#[derive(Debug, Clone, Default, Serialize, Deserialize, PartialEq, Eq)]
36#[serde(rename_all = "snake_case")]
37pub enum OutputTarget {
38    #[default]
39    Inherit,
40    Null,
41    File(PathBuf),
42}
43
44#[derive(Debug, Clone, Serialize, Deserialize)]
45pub struct LoggingConfig {
46    #[serde(default)]
47    pub stdout: OutputTarget,
48    #[serde(default)]
49    pub stderr: OutputTarget,
50}
51
52impl Default for LoggingConfig {
53    fn default() -> Self {
54        Self {
55            stdout: OutputTarget::Inherit,
56            stderr: OutputTarget::Inherit,
57        }
58    }
59}
60
61#[derive(Debug, Clone, Serialize, Deserialize)]
62pub struct ProcessDef {
63    pub name: String,
64    pub command: String,
65    pub restart: RestartPolicy,
66    pub max_restarts: u32,
67    #[serde(with = "humantime_serde")]
68    pub restart_delay: Duration,
69
70    #[serde(default)]
71    pub directory: Option<PathBuf>,
72
73    #[serde(default = "default_stop_timeout", with = "humantime_serde")]
74    pub stop_timeout: Duration,
75
76    #[serde(default)]
77    pub stop_signal: StopSignal,
78
79    #[serde(default = "default_numprocs")]
80    pub numprocs: u32,
81
82    #[serde(default)]
83    pub env: HashMap<String, String>,
84
85    #[serde(default)]
86    pub logging: LoggingConfig,
87}
88
89fn default_stop_timeout() -> Duration {
90    Duration::from_secs(5)
91}
92
93fn default_numprocs() -> u32 {
94    1
95}
96
97impl Default for ProcessDef {
98    fn default() -> Self {
99        Self {
100            name: "unnamed".into(),
101            command: "true".into(),
102            restart: RestartPolicy::OnFailure,
103            max_restarts: 5,
104            restart_delay: Duration::from_secs(1),
105            directory: None,
106            stop_timeout: default_stop_timeout(),
107            stop_signal: StopSignal::default(),
108            numprocs: 1,
109            env: HashMap::new(),
110            logging: LoggingConfig::default(),
111        }
112    }
113}
114
115#[derive(Debug, Clone, Serialize, Deserialize, Default)]
116pub struct ProcessConfig {
117    pub processes: Vec<ProcessDef>,
118}