The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

App::Base::Daemon - A lazy person's tool for writing self-documenting, self-monitoring daemons

SYNOPSIS

    package App::Base::Daemon::example;
    use Moose;
    with 'App::Base::Daemon';
    sub documentation { return 'This is an example daemon.'; }

    sub options {

        # See App::Base::Script::Common
    }

    sub daemon_run {
        my $self = shift;
        while (1) {
            # do something
            sleep(1)
        }

        return 0;    # This will never be reached
    }

    sub handle_shutdown {
        my $self = shift;
        # do something
        return 0;
    }

    no Moose;
    __PACKAGE__->meta->make_immutable;
    1;

    exit App::Base::Daemon::example->new->run;

DESCRIPTION

App::Base::Daemon builds on App::Base::Script::Common and provides common infrastructure for writing daemons, including:

-

Standardized logging techniques via syslog

-

Signal processing and graceful shutdown

BUILT-IN OPTIONS

Every App::Base::Daemon-implementing class gets some daemon-specific options for free, in addition to those provided by App::Base::Script::Common. They are:

--no-fork

Rather than double-forking and detaching from the console, the daemon runs in the foreground (parent) process. Useful for debugging or interactive invocations.

--pid-file

Writes PID of the daemon into specified file, by default writes pid into /var/run/__PACKAGE__.pid

--no-pid-file

Do not write pid file, and do not check if it is exist and locked.

--no-warn

Do not produce warnings, silent mode

REQUIRED SUBCLASS METHODS

daemon_run

The main loop that runs the daemon. Typically this will include while(1) or something similar. If this method returns, daemon exits.

handle_shutdown

Called before the daemon shuts down in response to a shutdown signal. Should clean up any resources in use by the daemon. The return value of handle_shutdown is used as the exit status of the daemon.

ATTRIBUTES

shutdown_signals

An arrayref of signals that should result in termination of the daemon. Defaults are: INT, QUIT, TERM.

user

Run as specified user, note that it is only possible if daemon started as root

group

Run as specified group, note that it is only possible if daemon started as root

pid_file

Pid file name

can_do_hot_reload

Should return true if implementation supports hot reloading

METHODS

error

Handles the output of errors, including shutting down the running daemon by calling handle_shutdown(). If you have a serious problem that should NOT result in shutting down your daemon, use warn() instead.

USAGE

Inheritance

Invocation of a App::Base::Daemon-based daemon is accomplished as follows:

-

Define a class that implements App::Base::Daemon

-

Instantiate an object of that class via new()

-

Run the daemon by calling run(). The return value of run() is the exit status of the daemon, and should typically be passed back to the calling program via exit()

The new() method

(See App::Base::Script::Common::new)

Options handling

(See App::Base::Script::Common, "Options handling")

LICENSE AND COPYRIGHT

Copyright (C) 2010-2014 Binary.com

This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.

See http://dev.perl.org/licenses/ for more information.