Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions examples/restarter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
43 changes: 37 additions & 6 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<dyn LifecycleHandler>,
lifecycle_handler: Box<dyn LifecycleHandler>,
/// 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<P: AsRef<Path>>(&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<H: LifecycleHandler + 'static>(&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,
Expand Down