diff --git a/examples/restarter.rs b/examples/restarter.rs index 9f7cc05..e566bd2 100644 --- a/examples/restarter.rs +++ b/examples/restarter.rs @@ -68,12 +68,9 @@ async fn main() -> Result<(), Error> { let restart_generation = app_data.restart_generation; // Configure the essential requirements for implementing graceful restart. - let restart_conf = RestartConfig { - enabled: true, - coordination_socket_path: args.socket.into(), - lifecycle_handler: Box::new(app_data), - ..Default::default() - }; + let mut restart_conf = RestartConfig::new(); + restart_conf.coordination_socket(args.socket); + restart_conf.lifecycle_handler(app_data); match args.command { // Restart an already-running process diff --git a/src/lib.rs b/src/lib.rs index 06b6f25..90493c6 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -86,22 +86,53 @@ const ENV_SYSTEMD_PID: &str = "LISTEN_PID"; const REBIND_SYSTEMD_PID: &str = "auto"; /// Settings for graceful restarts +#[non_exhaustive] pub struct RestartConfig { /// Enables the restart coordination socket for graceful restarts as an alternative to a Unix signal. - pub enabled: bool, + enabled: bool, /// Socket path - pub coordination_socket_path: PathBuf, + coordination_socket_path: PathBuf, /// Sets environment variables on the newly-started process - pub environment: Vec<(OsString, OsString)>, + environment: Vec<(OsString, OsString)>, /// Receive fine-grained events on the lifecycle of the new process and support data transfer. - pub lifecycle_handler: Box, + lifecycle_handler: Box, /// Exits early when child process fail to start - pub exit_on_error: bool, + exit_on_error: bool, /// Sets the signal to listen to on restart. This defaults to SIGUSR1. - pub restart_signal: SignalKind, + restart_signal: SignalKind, } impl RestartConfig { + pub fn new() -> Self { + Default::default() + } + + pub fn coordination_socket>(&mut self, path: P) -> &mut Self { + self.enabled = true; + self.coordination_socket_path = path.as_ref().into(); + self + } + + pub fn environment(&mut self, vars: Vec<(OsString, OsString)>) -> &mut Self { + self.environment = vars; + self + } + + pub fn lifecycle_handler(&mut self, handler: H) -> &mut Self { + self.lifecycle_handler = Box::new(handler); + self + } + + pub fn should_exit_on_error(&mut self, exit: bool) -> &mut Self { + self.exit_on_error = exit; + self + } + + pub fn restart_signal(&mut self, signal: SignalKind) -> &mut Self { + self.restart_signal = signal; + self + } + /// Prepare the current process to handle restarts, if enabled. pub fn try_into_restart_task( self,