logo
Free, unlimited AI code reviews that run on commit
git-lrc git-lrc GitHub Install Now We'd appreciate a star git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt git-lrc - Free, unlimited AI code reviews that run on commit | Product Hunt

MooseX::Daemonize - Role for daemonizing your Moose based application

Attributes

       This list includes attributes brought in from other roles as well we include them here for ease of
       documentation. All of these attributes are settable though MooseX::Getopt's command line handling, with
       the exception of "is_daemon".

       prognamePath::Class::Dir|Str
           The name of our daemon, defaults to "$package_name =~ s/::/_/";

       pidbasePath::Class::Dir|Str
           The base for our PID, defaults to "/var/run/"

       basedirPath::Class::Dir|Str
           The directory we chdir to; defaults to "/".

       pidfileMooseX::Daemonize::Pid::File|Str
           The file we store our PID in, defaults to "$pidbase/$progname.pid"

       foregroundBool
           If true, the process won't background. Useful for debugging. This option can be set via Getopt's -f.

       no_double_forkBool
           If  true,  the process will not perform the typical double-fork, which is extra added protection from
           your process accidentally acquiring a  controlling  terminal.   More  information  can  be  found  by
           Googling "double fork daemonize".

       ignore_zombiesBool
           If true, the process will not clean up zombie processes.  Normally you don't want this.

       dont_close_all_filesBool
           If  true,  the  objects open filehandles will not be closed when daemonized.  Normally you don't want
           this.

       is_daemonBool
           If true, the process is the backgrounded daemon process, if false it is the parent process.  This  is
           useful for example in an "after 'start' =" sub { }> block.

           NOTE: This option is explicitly not available through MooseX::Getopt.

       stop_timeout
           Number  of  seconds  to  wait for the process to stop, before trying harder to kill it. Defaults to 2
           seconds.

       These are the internal attributes, which are not available through MooseX::Getopt.

       exit_codeIntstatus_messageStr

Authors

       •   Stevan Little <stevan.little@iinteractive.com>

       •   Chris Prather <chris@prather.org>

Caveats

       When going into background MooseX::Daemonize closes all open file handles. This may interfere with you
       logging because it may also close the log file handle you want to write to. To prevent this you can
       either defer opening the log file until after start. Alternatively, use can use the
       'dont_close_all_files' option either from the command line or in your .sh script.

       Assuming you want to use Log::Log4perl for example you could expand the MooseX::Daemonize example above
       like this.

           after start => sub {
               my $self = shift;
               return unless $self->is_daemon;
               Log::Log4perl->init(\$log4perl_config);
               my $logger = Log::Log4perl->get_logger();
               $logger->info("Daemon started");
               # your daemon code here ...
           };

Contributors

       •   Karen Etheridge <ether@cpan.org>

       •   Michael Reddick <michael.reddick@gmail.com>

       •   Yuval Kogman <nothingmuch@woobling.org>

       •   Ash Berlin <ash@cpan.org>

       •   Brandon L Black <blblack@gmail.com>

       •   Jonathan Sailor <jsailor@cpan.org>

       •   David Steinbrunner <dsteinbrunner@pobox.com>

       •   Michael Schwern <mschwern@cpan.org>

       •   Shoichi Kaji <skaji@cpan.org>

       •   Dave Rolsky <autarch@urth.org>

       •   Chisel Wright <chisel@chizography.net>

Dependencies

       Moose, MooseX::Getopt, MooseX::Types::Path::Class and POSIX

Description

       Often you want to write a persistent daemon that has a pid file, and responds appropriately to Signals.
       This module provides a set of basic roles as an infrastructure to do that.

Incompatibilities

       Obviously this will not work on Windows.

Methods

DaemonControlMethods
       These methods can be used to control the daemon behavior. Every  effort  has  been  made  to  have  these
       methods DWIM (Do What I Mean), so that you can focus on just writing the code for your daemon.

       Extending  these  methods  is  best  done  with the Moose method modifiers, such as "before", "after" and
       "around".

       start
           Setup a pidfile, fork, then setup the signal handlers.

       stop
           Stop the process matching the pidfile, and unlinks the pidfile.

       restart
           Literally this is:

               $self->stop();
               $self->start();

       statusshutdownPidfileHandlingMethodsinit_pidfile
           This method will create a MooseX::Daemonize::Pid::File object and tell it to store  the  PID  in  the
           file "$pidbase/$progname.pid".

       check
           This checks to see if the daemon process is currently running by checking the pidfile.

       get_pid
           Returns the PID of the daemon process.

       save_pid
           Write the pidfile.

       remove_pid
           Removes the pidfile.

   SignalHandlingMethodssetup_signals
           Setup  the signal handlers, by default it only sets up handlers for SIGINT and SIGHUP. If you wish to
           add more signals just use the "after" method modifier and add them.

       handle_sigint
           Handle a INT signal, by default calls "$self-"stop()>

       handle_sighup
           Handle a HUP signal. By default calls "$self-"restart()>

   ExitCodeMethods
       These are overridable constant methods used for setting the exit code.

       OK  Returns 0.

       ERROR
           Returns 1.

   Introspectionmeta()
           The "meta()" method from Class::MOP::Class

Name

       MooseX::Daemonize - Role for daemonizing your Moose based application

See Also

       Daemon::Control, Proc::Daemon, Daemon::Generic

Support

       Bugs        may        be        submitted        through        the        RT        bug         tracker
       <https://rt.cpan.org/Public/Dist/Display.html?Name=MooseX-Daemonize> (or bug-MooseX-Daemonize@rt.cpan.org
       <mailto:bug-MooseX-Daemonize@rt.cpan.org>).

       There    is    also    a    mailing    list    available    for    users   of   this   distribution,   at
       <http://lists.perl.org/list/moose.html>.

       There is also an irc channel available for users of this  distribution,  at  "#moose"  on  "irc.perl.org"
       <irc://irc.perl.org/#moose>.

Synopsis

           package My::Daemon;
           use Moose;

           with qw(MooseX::Daemonize);

           # ... define your class ....

           after start => sub {
               my $self = shift;
               return unless $self->is_daemon;
               # your daemon code here ...
           };

           # then in your script ...

           my $daemon = My::Daemon->new_with_options();

           my ($command) = @{$daemon->extra_argv}
           defined $command || die "No command specified";

           $daemon->start   if $command eq 'start';
           $daemon->status  if $command eq 'status';
           $daemon->restart if $command eq 'restart';
           $daemon->stop    if $command eq 'stop';

           warn($daemon->status_message);
           exit($daemon->exit_code);

Thanks

       Mike Boyko, Matt S. Trout, Stevan Little, Brandon Black, Ash Berlin and the #moose denizens

       Some bug fixes sponsored by Takkle Inc.

Version

       version 0.22

Warning

       The maintainers of this module now recommend using Daemon::Control instead.

See Also