| 1 | package XAS::Lib::App::Daemon; | ||
| 2 | |||
| 3 | our $VERSION = '0.02'; | ||
| 4 | |||
| 5 | use Try::Tiny; | ||
| 6 | use File::Pid; | ||
| 7 | use Pod::Usage; | ||
| 8 | use Hash::Merge; | ||
| 9 | use Getopt::Long; | ||
| 10 | use POSIX 'setsid'; | ||
| 11 | |||
| 12 | use XAS::Class | ||
| 13 | debug => 0, | ||
| 14 | version => $VERSION, | ||
| 15 | import => 'class CLASS', | ||
| 16 | base => 'XAS::Lib::App', | ||
| 17 | utils => ':process', | ||
| 18 | constants => 'TRUE FALSE', | ||
| 19 | accessors => 'daemon', | ||
| 20 | ; | ||
| 21 | |||
| 22 | # ---------------------------------------------------------------------- | ||
| 23 | # Public Methods | ||
| 24 | # ---------------------------------------------------------------------- | ||
| 25 | |||
| 26 | sub define_signals { | ||
| 27 | my $self = shift; | ||
| 28 | |||
| 29 | $SIG{'INT'} = \&signal_handler; | ||
| 30 | $SIG{'QUIT'} = \&signal_handler; | ||
| 31 | $SIG{'TERM'} = \&signal_handler; | ||
| 32 | $SIG{'HUP'} = \&signal_handler; | ||
| 33 | |||
| 34 | } | ||
| 35 | |||
| 36 | sub define_pidfile { | ||
| 37 | my $self = shift; | ||
| 38 | |||
| 39 | my $script = $self->class->any_var('SCRIPT'); | ||
| 40 | |||
| 41 | # create a pid file, use it as a semaphore lock file | ||
| 42 | |||
| 43 | $self->log->debug("entering define_pidfile()"); | ||
| 44 | $self->log->debug("pid file = " . $self->env->pidfile); | ||
| 45 | |||
| 46 | try { | ||
| 47 | |||
| 48 | $self->{pid} = File::Pid->new({file => $self->env->pidfile->path}); | ||
| 49 | |||
| 50 | if ((my $num = $self->pid->running()) || | ||
| 51 | ($self->env->pidfile->exists)) { | ||
| 52 | |||
| 53 | if ($num) { | ||
| 54 | |||
| 55 | $self->throw_msg( | ||
| 56 | 'xas.lib.app.daemon.pidfile.runerr', | ||
| 57 | 'runerr', | ||
| 58 | $script, $num | ||
| 59 | ); | ||
| 60 | |||
| 61 | } else { | ||
| 62 | |||
| 63 | $self->throw_msg( | ||
| 64 | 'xas.lib.app.daemon.pidfile.piderr', | ||
| 65 | 'piderr', | ||
| 66 | $script | ||
| 67 | ); | ||
| 68 | |||
| 69 | } | ||
| 70 | |||
| 71 | } | ||
| 72 | |||
| 73 | $self->pid->write() or | ||
| 74 | $self->throw_msg( | ||
| 75 | 'xas.lib.app.daemon.pidfile.writerr', | ||
| 76 | 'wrterr', | ||
| 77 | $self->pid->file | ||
| 78 | ); | ||
| 79 | |||
| 80 | } catch { | ||
| 81 | |||
| 82 | my $ex = $_; | ||
| 83 | |||
| 84 | print STDERR "$ex\n"; | ||
| 85 | |||
| 86 | exit 2; | ||
| 87 | |||
| 88 | }; | ||
| 89 | |||
| 90 | $self->log->debug("leaving define_pidfile()"); | ||
| 91 | |||
| 92 | } | ||
| 93 | |||
| 94 | sub define_daemon { | ||
| 95 | my $self = shift; | ||
| 96 | |||
| 97 | # become a daemon... | ||
| 98 | # interesting, "daemonize() if ($self->daemon);" doesn't work as expected | ||
| 99 | |||
| 100 | $self->log->debug("before pid = " . $$); | ||
| 101 | |||
| 102 | if ($self->daemon) { | ||
| 103 | |||
| 104 | daemonize(); | ||
| 105 | |||
| 106 | } | ||
| 107 | |||
| 108 | $self->log->debug("after pid = " . $$); | ||
| 109 | |||
| 110 | } | ||
| 111 | |||
| 112 | sub run { | ||
| 113 | my $self = shift; | ||
| 114 | |||
| 115 | my $rc = $self->SUPER::run(); | ||
| 116 | |||
| 117 | $self->pid->remove(); | ||
| 118 | |||
| 119 | return $rc; | ||
| 120 | |||
| 121 | } | ||
| 122 | |||
| 123 | # ---------------------------------------------------------------------- | ||
| 124 | # Private Methods | ||
| 125 | # ---------------------------------------------------------------------- | ||
| 126 | |||
| 127 | sub _default_options { | ||
| 128 | my $self = shift; | ||
| 129 | |||
| 130 | my $options = $self->SUPER::_default_options(); | ||
| 131 | |||
| 132 | $self->{daemon} = FALSE; | ||
| 133 | |||
| 134 | $options->{'daemon'} = \$self->{daemon}; | ||
| 135 | |||
| 136 | $options->{'cfgfile=s'} = sub { | ||
| 137 | my $cfgfile = File($_[1]); | ||
| 138 | $self->env->cfgfile($cfgfile); | ||
| 139 | }; | ||
| 140 | |||
| 141 | $options->{'pidfile=s'} = sub { | ||
| 142 | my $pidfile = File($_[1]); | ||
| 143 | $self->env->pidfile($pidfile); | ||
| 144 | }; | ||
| 145 | |||
| 146 | return $options; | ||
| 147 | |||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | __END__ | ||
| 153 | |||
| 154 | =head1 NAME | ||
| 155 | |||
| 156 | XAS::Lib::App::Daemon - The base class to write daemons within the XAS environment | ||
| 157 | |||
| 158 | =head1 SYNOPSIS | ||
| 159 | |||
| 160 | use XAS::Lib::App::Daemon; | ||
| 161 | |||
| 162 | my $app = XAS::Lib::App::Daemon->new(); | ||
| 163 | |||
| 164 | $app->run(); | ||
| 165 | |||
| 166 | =head1 DESCRIPTION | ||
| 167 | |||
| 168 | This module defines an operating environment for daemons. A daemon is a | ||
| 169 | Unix background process without a controlling terminal. Windows really | ||
| 170 | doesn't have a concept for this behavior. For running background jobs | ||
| 171 | on Windows please see L<XAS::Lib::App::Services|XAS::Lib::App::Services>. | ||
| 172 | |||
| 173 | This module is also single threaded, it doesn't use POE to provide an | ||
| 174 | async environment. If you need that, the see the above module. This inherits | ||
| 175 | from L<XAS::Lib::App|XAS::Lib::App>. Please see that module for additional | ||
| 176 | documentation. | ||
| 177 | |||
| 178 | =head1 METHODS | ||
| 179 | |||
| 180 | =head2 define_pidfile | ||
| 181 | |||
| 182 | This method sets up the pid file for the process. By default, this file | ||
| 183 | is named $XAS_RUN/<$0>.pid. This can be overridden by the --pidfile option. | ||
| 184 | |||
| 185 | =head2 define_signals | ||
| 186 | |||
| 187 | This method sets up basic signal handling. By default this is only for the INT, | ||
| 188 | TERM, HUP and QUIT signals. | ||
| 189 | |||
| 190 | =head2 define_daemon | ||
| 191 | |||
| 192 | This method will cause the process to become a daemon. | ||
| 193 | |||
| 194 | =head1 OPTIONS | ||
| 195 | |||
| 196 | This module handles these additional options. | ||
| 197 | |||
| 198 | =head2 --cfgfile | ||
| 199 | |||
| 200 | This defines an optional configuration file. | ||
| 201 | |||
| 202 | =head2 --pidfile | ||
| 203 | |||
| 204 | This defines the pid file for recording the pid. | ||
| 205 | |||
| 206 | =head2 --daemon | ||
| 207 | |||
| 208 | Become a daemon. | ||
| 209 | |||
| 210 | =head1 SEE ALSO | ||
| 211 | |||
| 212 | =over 4 | ||
| 213 | |||
| 214 | =item L<XAS|XAS> | ||
| 215 | |||
| 216 | =back | ||
| 217 | |||
| 218 | =head1 AUTHOR | ||
| 219 | |||
| 220 | Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt> | ||
| 221 | |||
| 222 | =head1 COPYRIGHT AND LICENSE | ||
| 223 | |||
| 224 | Copyright (C) 2014 Kevin L. Esteb | ||
| 225 | |||
| 226 | This library is free software; you can redistribute it and/or modify | ||
| 227 | it under the same terms as Perl itself, either Perl version 5.8.8 or, | ||
| 228 | at your option, any later version of Perl 5 you may have available. | ||
| 229 | |||
| 230 | See L<http://dev.perl.org/licenses/> for more information. | ||
| 231 | |||
| 232 | =cut |