#supervisor #process #ipc #unix-socket #sockets #tokio #worker

psup-impl

Process supervisor with inter-process communication over tokio Unix sockets

12 unstable releases (3 breaking)

0.6.4 Mar 12, 2021
0.6.3 Mar 12, 2021
0.5.2 Mar 1, 2021
0.5.0 Feb 28, 2021
0.3.3 Feb 28, 2021

#33 in #supervisor

32 downloads per month
Used in psup

MIT/Apache

23KB
445 lines

Process supervisor with inter-process communication support using tokio.

Currently only supports Unix, later we plan to add support for Windows using named pipes.

Supervisor

Supervisor manages child processes sending socket information using the environment and then switching to Unix domain sockets for inter-process communication. Daemon processes are restarted if they die without being shutdown by the supervisor.

use psup_impl::{Error, Result, Task, SupervisorBuilder};

#[tokio::main]
async fn main() -> Result<()> {
   let worker_cmd = "worker-process";
   let mut supervisor = SupervisorBuilder::new()
       .server(|stream, tx| {
            let (reader, mut writer) = stream.into_split();
            tokio::task::spawn(async move {
                // Handle worker connection here
                // Use the `tx` supervisor control channel
                // to spawn and shutdown workers
                Ok::<(), Error>(())
            });
       })
       .path(std::env::temp_dir().join("supervisor.sock"))
       .add_worker(Task::new(worker_cmd).daemon(true))
       .add_worker(Task::new(worker_cmd).daemon(true))
       .build();
   supervisor.run().await?;
   // Block the process here and do your work.
   Ok(())
}

Worker

Worker reads the socket information from the environment and then connects to the Unix socket.

use psup_impl::{Error, Result, Worker};

#[tokio::main]
async fn main() -> Result<()> {
    // Read supervisor information from the environment
    // and set up the IPC channel with the supervisor
    let worker = Worker::new()
        .client(|stream, id| async {
            let (reader, mut writer) = stream.into_split();
            // Start sending messages to the supervisor
            Ok::<(), Error>(())
        });
    worker.run().await?;
    // Block the process here and do your work.
    Ok(())
}

Dependencies

~4–12MB
~134K SLoC