Skip to content

Commit b5d5c03

Browse files
committed
Non-exhaustive RestartConfig
We added fields twice. We may add them again. As a bonus, we can hide some of the implementation details during construction.
1 parent 6ae1ee8 commit b5d5c03

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

examples/restarter.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,9 @@ async fn main() -> Result<(), Error> {
6868
let restart_generation = app_data.restart_generation;
6969

7070
// Configure the essential requirements for implementing graceful restart.
71-
let restart_conf = RestartConfig {
72-
enabled: true,
73-
coordination_socket_path: args.socket.into(),
74-
lifecycle_handler: Box::new(app_data),
75-
..Default::default()
76-
};
71+
let mut restart_conf = RestartConfig::new();
72+
restart_conf.coordination_socket(args.socket);
73+
restart_conf.lifecycle_handler(app_data);
7774

7875
match args.command {
7976
// Restart an already-running process

src/lib.rs

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,22 +86,53 @@ const ENV_SYSTEMD_PID: &str = "LISTEN_PID";
8686
const REBIND_SYSTEMD_PID: &str = "auto";
8787

8888
/// Settings for graceful restarts
89+
#[non_exhaustive]
8990
pub struct RestartConfig {
9091
/// Enables the restart coordination socket for graceful restarts as an alternative to a Unix signal.
91-
pub enabled: bool,
92+
enabled: bool,
9293
/// Socket path
93-
pub coordination_socket_path: PathBuf,
94+
coordination_socket_path: PathBuf,
9495
/// Sets environment variables on the newly-started process
95-
pub environment: Vec<(OsString, OsString)>,
96+
environment: Vec<(OsString, OsString)>,
9697
/// Receive fine-grained events on the lifecycle of the new process and support data transfer.
97-
pub lifecycle_handler: Box<dyn LifecycleHandler>,
98+
lifecycle_handler: Box<dyn LifecycleHandler>,
9899
/// Exits early when child process fail to start
99-
pub exit_on_error: bool,
100+
exit_on_error: bool,
100101
/// Sets the signal to listen to on restart. This defaults to SIGUSR1.
101-
pub restart_signal: SignalKind,
102+
restart_signal: SignalKind,
102103
}
103104

104105
impl RestartConfig {
106+
pub fn new() -> Self {
107+
Default::default()
108+
}
109+
110+
pub fn coordination_socket<P: AsRef<Path>>(&mut self, path: P) -> &mut Self {
111+
self.enabled = true;
112+
self.coordination_socket_path = path.as_ref().into();
113+
self
114+
}
115+
116+
pub fn environment(&mut self, vars: Vec<(OsString, OsString)>) -> &mut Self {
117+
self.environment = vars;
118+
self
119+
}
120+
121+
pub fn lifecycle_handler<H: LifecycleHandler + 'static>(&mut self, handler: H) -> &mut Self {
122+
self.lifecycle_handler = Box::new(handler);
123+
self
124+
}
125+
126+
pub fn should_exit_on_error(&mut self, exit: bool) -> &mut Self {
127+
self.exit_on_error = exit;
128+
self
129+
}
130+
131+
pub fn restart_signal(&mut self, signal: SignalKind) -> &mut Self {
132+
self.restart_signal = signal;
133+
self
134+
}
135+
105136
/// Prepare the current process to handle restarts, if enabled.
106137
pub fn try_into_restart_task(
107138
self,

0 commit comments

Comments
 (0)