buildDaemon.run

Starts daemon that is described by DaemonInfo. Daemon is implemented as windows service and auto-installed in SC manager. If you want to uninstall the service, you can use uninstall function or system call:

C:\Windows\System32\sc.exe delete <daemonName>

If the service is already installed and is stopped, the function tries to start daemon. Otherwise it fails and returns EXIT_FAILURE code.

logger is a initialized logger for the daemon, you should use absolute names for Windows for logger files.

pidFilePath, lockFilePath, userId and groupId are ignored for Windows platform.

template buildDaemon(alias DaemonInfo)
version(Windows)
static if(isDaemon!DaemonInfo)
int
run
(
shared ILogger logger
,
string pidFilePath = ""
,
string lockFilePath = ""
,
int userId = -1
,
int groupId = -1
)
if (
isDaemon!DaemonInfo ||
isDaemonClient!DaemonInfo
)

Examples

// First you need to describe your daemon via template
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;
    }
);

//...
buildDaemon!daemon.run(new shared StrictLogger(logFilePath));

See Also

uninstall

Meta