App::Base::Daemon - A lazy person's tool for writing self-documenting, self-monitoring daemons
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;
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
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:
Rather than double-forking and detaching from the console, the daemon runs in the foreground (parent) process. Useful for debugging or interactive invocations.
Writes PID of the daemon into specified file, by default writes pid into /var/run/__PACKAGE__.pid
Do not write pid file, and do not check if it is exist and locked.
Do not produce warnings, silent mode
The main loop that runs the daemon. Typically this will include while(1) or something similar. If this method returns, daemon exits.
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.
An arrayref of signals that should result in termination of the daemon. Defaults are: INT, QUIT, TERM.
Run as specified user, note that it is only possible if daemon started as root
Run as specified group, note that it is only possible if daemon started as root
Pid file name
Should return true if implementation supports hot reloading
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.
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()
(See App::Base::Script::Common::new)
(See App::Base::Script::Common, "Options handling")
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.
To install App::Base, copy and paste the appropriate command in to your terminal.
cpanm
cpanm App::Base
CPAN shell
perl -MCPAN -e shell install App::Base
For more information on module installation, please visit the detailed CPAN module installation guide.