buildDaemon

Main template in the module that actually creates daemon process. DaemonInfo is a Daemon instance that holds name of the daemon and hooks for numerous Signals.

Daemon is detached from terminal, therefore it needs a preinitialized logger.

As soon as daemon is ready the function executes main delegate that returns application return code.

Daemon uses pid and lock files. Pid file holds process id for communications with other applications. If pidFilePath isn't set, the default path to pid file is '~/.daemonize/<daemonName>.pid'. Lock file prevents from execution of numerous copies of daemons. If lockFilePath isn't set, the default path to lock file is '~/.daemonize/<daemonName>.lock'. If you want several instances of one daemon, redefine pid and lock files paths.

Sometimes lock and pid files are located at /var/run directory and needs a root access. If userId and groupId parameters are set, daemon tries to create lock and pid files and drops root privileges.

Members

Aliases

daemon
alias daemon = readDaemonInfo!DaemonInfo
Undocumented in source.

Classes

LoggedException
class LoggedException

Saves info about exception into daemon logger

Functions

run
int run(ILogger logger, string pidFilePath, string lockFilePath, int userId, int groupId)
Undocumented in source. Be warned that the author may not have intended to support it.
sendSignal
void sendSignal(ILogger logger, Signal signal, string pidFilePath)

As custom signals are mapped to realtime signals at runtime, it is complicated to calculate signal number by hands. The function simplifies sending signals to daemons that were created by the package.

sendSignalDynamic
void sendSignalDynamic(ILogger logger, string daemonName, Signal signal, string pidFilePath)

As custom signals are mapped to realtime signals at runtime, it is complicated to calculate signal number by hands. The function simplifies sending signals to daemons that were created by the package.

uninstall
void uninstall()

In GNU/Linux daemon doesn't require deinstallation.

Examples

alias daemon = Daemon!(
    "DaemonizeExample1", // unique name

    // Setting associative map signal -> callbacks
    KeyValueList!(
        Composition!(Signal.Terminate, Signal.Quit, Signal.Shutdown, Signal.Stop), (logger, signal)
        {
            logger.logInfo("Exiting...");
            return false; // returning false will terminate daemon
        },
        Signal.HangUp, (logger)
        {
            logger.logInfo("Hello World!");
            return true; // continue execution
        }
    ),

    // Main function where your code is
    (logger, shouldExit) {
        // will stop the daemon in 5 minutes
        auto time = Clock.currSystemTick + cast(TickDuration)5.dur!"minutes";
        bool timeout = false;
        while(!shouldExit() && time > Clock.currSystemTick) {  }

        logger.logInfo("Exiting main function!");

        return 0;
    }
);

return buildDaemon!daemon.run(logger);

Meta