[][src]Crate spirit_daemonize

A spirit extension for daemonization.

The configuration in here extends the spirit configuration framework to automatically go into background based on user's configuration and command line options.

Examples

use serde::Deserialize;
use spirit::{Pipeline, Spirit};
use spirit::prelude::*;
use spirit_daemonize::{Daemon, Opts as DaemonOpts};
use structopt::StructOpt;

// From config files
#[derive(Default, Deserialize)]
struct Cfg {
    #[serde(default)]
    daemon: Daemon,
}

// From command line
#[derive(Debug, StructOpt)]
struct Opts {
    #[structopt(flatten)]
    daemon: DaemonOpts,
}

fn main() {
     Spirit::<Opts, Cfg>::new()
        .with(
            Pipeline::new("daemon")
                .extract(|o: &Opts, c: &Cfg| {
                    // Put the two sources together to one Daemon
                    o.daemon.transform(c.daemon.clone())
                })
        )
        .run(|_spirit| {
            // Possibly daemonized program goes here
            Ok(())
        });
}

Added options

The program above gets the -d command line option, which enables daemonization. Furthermore, the configuration now understands a new daemon section, with these options:

Multithreaded applications

As daemonization is done by using fork, you should start any threads after you initialize the spirit. Otherwise you'll lose the threads (and further bad things will happen).

The daemonization happens inside the application of validator actions of config_validator callback. If other config validators need to start any threads, they should be plugged in after the daemonization callback. However, the safer option is to start them inside the run method.

Structs

Daemon

A configuration fragment for configuration of daemonization.

Daemonize

Intermediate plumbing type.

DaemonizeInstaller

An installer for Daemonize.

Opts

Command line options fragment.

UserDaemon

A stripped-down version of Daemon without the user-switching options.

Enums

SecId

Configuration of either user or a group.