blib/lib/XAS/Lib/App/Daemon.pm

filter: Statementstmt 41.0% 23 / 56 Branchbran 0.0% 0 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 50.0% 7 / 14 Podpod 100% 4 / 4 totaltotal 42.5% 34 / 80 CC 4 SCAR 13.4
CC
4
Cov
37%
define_pidfile
12.0
define_daemon
6.0
define_signals
2.0
CRAP 3.8
« blib/lib/XAS/Lib/Batch.pm ↑ Summary blib/lib/XAS/Lib/Net/Server.pm »
1 package XAS::Lib::App::Daemon;
2  
3 1 our $VERSION = '0.02';
4  
5 1 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Pod::Usage;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use POSIX 'setsid';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use XAS::Lib::Pidfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 use XAS::Class
13 1 debug => 0,
14 version => $VERSION,
15 import => 'class CLASS',
16 base => 'XAS::Lib::App',
17 utils => ':process',
18 constants => 'TRUE FALSE',
19 accessors => 'daemon pid',
20 1 ▶;
Subroutine BEGIN: called CC=1 SCAR=0.0
21  
22 # ----------------------------------------------------------------------
23 # Public Methods
24 # ----------------------------------------------------------------------
25  
26 sub define_signals {
27 0 ▶ my $self = shift;
Subroutine define_signals: not called CC=1 SCAR=6.9
Pod documented
28  
29 0 $SIG{'INT'} = \&signal_handler;
30 0 $SIG{'QUIT'} = \&signal_handler;
31 0 $SIG{'TERM'} = \&signal_handler;
32 0 $SIG{'HUP'} = \&signal_handler;
33  
34 }
35  
36 sub define_pidfile {
37 0 ▶ my $self = shift;
Subroutine define_pidfile: not called CC=3 SCAR=24.8
Pod documented
38  
39 0 my $script = $self->env->script;
40  
41 0 $self->log->debug('entering define_pidfile()');
42  
43 0 $self->{pid} = XAS::Lib::Pidfile->new(-pid => $$);
44  
45 0 ▶ if (my $num = $self->pid->is_running()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $num = $self->pid->is_running) 0 0 0
46  
47 0 $self->throw_msg(
48 dotid($self->class). '.define_pidfile.runerr',
49 'pid_run_error',
50 $script, $num
51 );
52  
53 }
54  
55 0 ▶ $self->pid->write() or
Branch: 1 branch
BranchTrueFalseTotal
$self->pid->write or $self->throw_msg(dotid($self->class) . '.define_pidfile.wrterr', "pid_write_error", $self->pid->file) 0 0 0
56 $self->throw_msg(
57 dotid($self->class) . '.define_pidfile.wrterr',
58 'pid_write_error',
59 $self->pid->file
60 );
61  
62 0 $self->log->debug('leaving define_pidfile()');
63  
64 }
65  
66 sub define_daemon {
67 0 ▶ my $self = shift;
Subroutine define_daemon: not called CC=2 SCAR=17.9
Pod documented
68  
69 # become a daemon...
70 # interesting, "daemonize() if ($self->daemon);" doesn't work as expected
71  
72 0 $self->log->debug("before pid = " . $$);
73  
74 0 ▶ if ($self->daemon) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->daemon) 0 0 0
75  
76 0 daemonize();
77  
78 }
79  
80 0 $self->log->debug("after pid = " . $$);
81  
82 }
83  
84 sub run {
85 0 ▶ my $self = shift;
Subroutine run: not called CC=1 SCAR=6.9
Pod documented
86  
87 0 my $rc = $self->SUPER::run();
88  
89 0 $self->pid->remove();
90  
91 0 return $rc;
92  
93 }
94  
95 # ----------------------------------------------------------------------
96 # Private Methods
97 # ----------------------------------------------------------------------
98  
99 sub _default_options {
100 0 ▶ my $self = shift;
Subroutine _default_options: not called CC=1 SCAR=6.9
101  
102 0 my $options = $self->SUPER::_default_options();
103  
104 0 $self->{daemon} = FALSE;
105  
106 0 $options->{'daemon'} = \$self->{daemon};
107  
108 $options->{'cfgfile=s'} = sub {
109 0 ▶ my $cfgfile = File($_[1]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
110 0 $self->env->cfgfile($cfgfile);
111 0 };
112  
113 $options->{'pidfile=s'} = sub {
114 0 ▶ my $pidfile = File($_[1]);
Subroutine __ANON__: not called CC=1 SCAR=6.9
115 0 $self->env->pidfile($pidfile);
116 0 };
117  
118 0 return $options;
119  
120 }
121  
122 1 1;
123  
124 __END__
125  
126 =head1 NAME
127  
128 XAS::Lib::App::Daemon - The base class to write daemons within the XAS environment
129  
130 =head1 SYNOPSIS
131  
132 use XAS::Lib::App::Daemon;
133  
134 my $app = XAS::Lib::App::Daemon->new();
135  
136 $app->run();
137  
138 =head1 DESCRIPTION
139  
140 This module defines an operating environment for daemons. A daemon is a
141 Unix background process without a controlling terminal. Windows really
142 doesn't have a concept for this behavior. For running background jobs
143 on Windows please see L<XAS::Lib::App::Services|XAS::Lib::App::Services>.
144  
145 This module is also single threaded, it doesn't use POE to provide an
146 async environment. If you need that, then see the above module. This inherits
147 from L<XAS::Lib::App|XAS::Lib::App>. Please see that module for additional
148 documentation.
149  
150 =head1 METHODS
151  
152 =head2 define_pidfile
153  
154 This method sets up the pid file for the process. By default, this file
155 is named $XAS_RUN/<$0>.pid. This can be overridden by the --pidfile option.
156  
157 =head2 define_signals
158  
159 This method sets up basic signal handling. By default this is only for the INT,
160 TERM, HUP and QUIT signals.
161  
162 =head2 define_daemon
163  
164 This method will cause the process to become a daemon.
165  
166 =head1 OPTIONS
167  
168 This module handles these additional options.
169  
170 =head2 --cfgfile
171  
172 This defines an optional configuration file.
173  
174 =head2 --pidfile
175  
176 This defines the pid file for recording the pid.
177  
178 =head2 --daemon
179  
180 Become a daemon.
181  
182 =head1 SEE ALSO
183  
184 =over 4
185  
186 =item L<XAS|XAS>
187  
188 =back
189  
190 =head1 AUTHOR
191  
192 Kevin L. Esteb, E<lt>kevin@kesteb.usE<gt>
193  
194 =head1 COPYRIGHT AND LICENSE
195  
196 Copyright (C) 2014 Kevin L. Esteb
197  
198 This is free software; you can redistribute it and/or modify it under
199 the terms of the Artistic License 2.0. For details, see the full text
200 of the license at http://www.perlfoundation.org/artistic_license_2_0.
201  
202 =cut
« blib/lib/XAS/Lib/Batch.pm ↑ Summary blib/lib/XAS/Lib/Net/Server.pm »