pub struct ProcessSupervisor { /* private fields */ }Expand description
Process supervisor for managing upstream application servers
The ProcessSupervisor spawns and monitors a child process, forwarding signals and handling graceful shutdown. It’s designed to work with application servers that listen on a specific port.
Implementations§
Source§impl ProcessSupervisor
impl ProcessSupervisor
Sourcepub async fn start(&self, port: u16) -> Result<()>
pub async fn start(&self, port: u16) -> Result<()>
Start the child process with the specified port
Spawns the child process with the PORT environment variable set. The process inherits stdin, stdout, and stderr from the parent.
§Arguments
port- Port number to pass via PORT environment variable
§Errors
Returns an error if:
- The command cannot be found or executed
- The process is already running
§Example
let mut supervisor = ProcessSupervisor::new("rails".to_string(), vec!["server".to_string()]);
supervisor.start(3000).await?;Sourcepub async fn wait(&self) -> Result<i32>
pub async fn wait(&self) -> Result<i32>
Wait for the child process to exit
This method blocks until the child process terminates and returns the exit code. A non-zero exit code indicates the process was terminated by a signal or crashed.
§Errors
Returns an error if:
- No process is running
- Failed to wait for the process
§Returns
The exit code of the process (0 for success, non-zero for error/signal)
§Example
let exit_code = supervisor.wait().await?;
println!("Process exited with code: {}", exit_code);Sourcepub async fn stop(&self) -> Result<()>
pub async fn stop(&self) -> Result<()>
Stop the child process gracefully
Sends SIGTERM to the process and waits for it to exit within the timeout period. If the process doesn’t exit within the timeout, sends SIGKILL to force termination.
§Errors
Returns an error if:
- No process is running
- Failed to send signals to the process
§Example
supervisor.stop().await?;Sourcepub async fn handle_signals(&self)
pub async fn handle_signals(&self)
Handle signals and forward them to the child process
This method listens for SIGTERM and SIGINT signals and forwards them to the child process. It should be spawned in a background task.
The method will block until a signal is received or the shutdown notification is triggered.
§Example
// Spawn signal handler in background
let signal_handle = tokio::spawn({
let supervisor = supervisor.clone();
async move {
supervisor.handle_signals().await;
}
});
// Later, clean up
signal_handle.abort();Sourcepub async fn wait_for_start(&self)
pub async fn wait_for_start(&self)
Wait for the process to start
This method blocks until the start() method has been called and
the process has been spawned.
§Example
// Start in background
let supervisor_clone = supervisor.clone();
tokio::spawn(async move {
supervisor_clone.start(3000).await.unwrap();
});
// Wait for startup
supervisor.wait_for_start().await;
println!("Process has started");Sourcepub fn trigger_shutdown(&self)
pub fn trigger_shutdown(&self)
Trigger shutdown notification
This notifies the signal handler to stop listening for signals.
Sourcepub async fn is_running(&self) -> bool
pub async fn is_running(&self) -> bool
Check if the process is currently running
Returns true if a child process is running, false otherwise.
§Example
if supervisor.is_running().await {
println!("Process is running");
}Trait Implementations§
Source§impl Clone for ProcessSupervisor
impl Clone for ProcessSupervisor
Source§fn clone(&self) -> ProcessSupervisor
fn clone(&self) -> ProcessSupervisor
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more