Daemonize v0.1 - simple way to create cross-platform daemons

Philippe Sigaud via Digitalmars-d-announce digitalmars-d-announce at puremagic.com
Sun Aug 31 09:01:02 PDT 2014


Nice!

I have a few questions/remarks, mainly to simplify the API somewhat.
Please bear with me :-)

> // First you need to describe your daemon via template
> alias daemon = Daemon!(
>     "DaemonizeExample1", // unique name

Does the user sees/uses this name in any way afterwards? Because I
think you could also produce a unique string at compile-time (by using
__FILE__ and __LINE__, unless someone has a better idea), if the user
does not provide one. Maybe he just wants an anonymous daemon, or
doesn't care, whatever.


>     // Setting associative map signal -> callbacks
>     KeyValueList!(

If I understand correctly, the Daemon template waits for a list of (at
least one) Signal, then a delegate, then some more Signals, another
delegate, and so on?

If that's so I think you could ditch KeyValueList (or build it
invisibly to the user) and let the user write only the signals and
delegates:

alias daemon = Daemon!(
    Signal.Terminate, Signal.Quit, Signal.Shutdown, Signal.Stop,
    (logger, signal) {
    ...},
    Signal.Hangup,
    (logger) {
    ...}
...
);

Iterate the argument list, collecting Signals. When you hit a
delegate, create a Composition!( ... ) with the previous signals, jump
above the delegate and so on.

Is the idea that, if the delegate has two arguments, then the second
is the signal that will be passed to it, and if it has only one
argument, only the logger will be passed?

What if the user does not want a logger? Is a daemon always associated
to a log file in OSes?


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

Is the main function always the last delegate?

Concerning the DaemonClient template, could you not ask for Daemon to
generate it on demand? Or is DaemonClient always used in another
module?

Because, given a Daemon, extracting the simplified DaemonClient can be
done, I think.


> * Custom signals

enum Signal : string
{ ... }

@nogc Signal customSignal(string name) @safe pure nothrow
{
    return cast(Signal)name;
}

I didn't know you could have an enum and extend it with a cast like this. Wow.


> * Signal composition

What happens when an unhandled signal is passed to a daemon?


> P.S. At the moment library doesn't support Mac and other Posix systems, the
> support is going to be added at next releases.

Do you foresee any difficulty in adapting this to Mac?


More information about the Digitalmars-d-announce mailing list