lib/Daemon/Generic/While1.pm

filter: Statementstmt 68.9% 40 / 58 Branchbran 20.0% 4 / 20 Conditioncond 50.0% 1 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 76.9% 10 / 13 Podpod 85.7% 6 / 7 totaltotal 60.4% 61 / 101 CC 12 SCAR 32.4
CC
12
Cov
56%
gd_sleep
72.0
__ANON__
6.0
gd_run
3.0
CRAP 25.5
« lib/Daemon/Generic.pm ↑ Summary lib/Daemon/Generic/Event.pm »
1 package Daemon::Generic::While1;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 11 require Daemon::Generic;
7 11 require POSIX;
8 11 require Exporter;
9  
10 11 our @ISA = qw(Daemon::Generic Exporter);
11 11 our @EXPORT = @Daemon::Generic::EXPORT;
12 11 our $VERSION = 0.84;
13  
14 sub newdaemon
15 11 ▶{
Subroutine newdaemon: called CC=2 SCAR=7.1
Pod undocumented
16 11 ▶ local($Daemon::Generic::caller) = caller() || 'main';
Condition50%1/2
caller() || 'main'
l!l
110
MC/DC0%0/1
caller() || 'main'
caller()
Truth table50%1/2
A
A= caller()
Aresult
11
00
17 11 local($Daemon::Generic::package) = __PACKAGE__;
18 11 Daemon::Generic::newdaemon(@_);
19 }
20  
21 sub gd_setup_signals
22 4 ▶{
Subroutine gd_setup_signals: called CC=1 SCAR=0.5
Pod documented
23 4 my ($self) = @_;
24 1 ▶ $SIG{HUP} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
25 1 $self->{gd_sighup} = time;
26 4 };
27 4 my $child;
28 4 ▶ $SIG{INT} = sub {
Subroutine __ANON__: called CC=1 SCAR=2.6
29 4 $self->{gd_sigint} = time;
30 #
31 # We'll be getting a SIGTERM in a bit if we're not dead, so let's use it.
32 #
33 0 ▶ $SIG{TERM} = sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
34 0 $self->gd_quit_event();
35 0 ▶ kill(15, $child) if $child; # if we're still alive, let's stay that way
Branch: 1 branch
BranchTrueFalseTotal
if $child 0 0 0
36 4 };
37 4 };
38 }
39  
40 sub gd_sleep
41 0 ▶{
Subroutine gd_sleep: not called CC=8 SCAR=42.8
Pod documented
42 0 my ($self, $period) = @_;
43 0 ▶ croak "Sleep period must be defined" unless defined $period;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $period 0 0 0
44 0 my $hires;
45 0 ▶ if ($period*1000 != int($period*1000)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($period * 1000 != int $period * 1000) 0 0 0
46 0 $hires = 1;
47 0 require Time::HiRes;
48 0 import Time::HiRes qw(time sleep);
49 }
50 0 my $t = time;
51 0 ▶ while (time - $t < $period) {
Branch: 1 branch
BranchTrueFalseTotal
if (time - $t < $period) 0 0 0
52 0 ▶ return if $self->{gd_sigint};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"gd_sigint"} 0 0 0
53 0 ▶ return if $self->{gd_sighup};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"gd_sighup"} 0 0 0
54 0 ▶ if ($hires) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hires) { } 0 0 0
55 0 ▶ my $p = (time - $t < 1)
Branch: 1 branch
BranchTrueFalseTotal
time - $t < 1 ? : 0 0 0
56 ? time - $t
57 : 1;
58 0 sleep($p);
59 } else {
60 0 sleep(1);
61 }
62 }
63 }
64  
65 sub gd_run
66 4 ▶{
Subroutine gd_run: called CC=3 SCAR=11.0
Pod documented
67 4 my ($self) = @_;
68 4 while(1) {
69 153 ▶ if ($self->{gd_sigint}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"gd_sigint"}) 4 149 153
70 4 $self->{gd_sigint} = 0;
71 4 $self->gd_quit_event();
72 }
73  
74 149 ▶ if ($self->{gd_sighup}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"gd_sighup"}) 1 148 149
75 1 $self->{gd_sighup} = 0;
76 1 $self->gd_reconfig_event();
77 }
78  
79 149 $self->gd_run_body();
80 }
81 }
82  
83 sub gd_reconfig_event
84 1 ▶{
Subroutine gd_reconfig_event: called CC=1 SCAR=0.0
Pod documented
85 1 my $self = shift;
86 1 print STDERR "Reconfiguration requested\n";
87 1 $self->gd_postconfig($self->gd_preconfig());
88 }
89  
90 sub gd_quit_event
91 4 ▶{
Subroutine gd_quit_event: called CC=1 SCAR=0.0
Pod documented
92 4 print STDERR "Quitting...\n";
93 4 exit(0);
94 }
95  
96  
97 0 ▶sub gd_run_body { die "must override gd_run_body()" }
Subroutine gd_run_body: not called CC=1 SCAR=6.9
Pod documented
98  
99 11 1;
100  
101 =head1 NAME
102  
103 Daemon::Generic::While1 - Daemon framework with default while(1) loop
104  
105 =head1 SYNOPSIS
106  
107 @ISA = qw(Daemon::Generic::While1);
108  
109 sub gd_run_body {
110 # stuff
111 }
112  
113 =head1 DESCRIPTION
114  
115 This is a slight variation on L<Daemon::Generic>: a default
116 C<gd_run()> provided. It has a while(1) loop that calls
117 C<gd_run_body()> over and over. It checks for reconifg and
118 and terminate events and only actions them between calls
119 to C<gd_run_body()>.
120  
121 Terminate events will be forced through after
122 C<$Daemon::Generic::force_quit_delay> seconds if
123 C<gd_run_body()> doesn't return quickly enough.
124  
125 =head1 SUBCLASS METHODS REQUIRD
126  
127 The following method is required to be overridden to subclass
128 Daemon::Generic::While1:
129  
130 =over 15
131  
132 =item gd_run_body()
133  
134 This method will be called over and over. This method should
135 include a call to C<sleep(1)> (or a bit more). Reconfig events
136 will not interrupt it. Quit events will only interrupt it
137 after 15 seconds.
138  
139 =back
140  
141 =head1 ADDITIONAL METHODS
142  
143 The following additional methods are available for your use
144 (as compared to L<Daemon::Generic>):
145  
146 =over 15
147  
148 =item gd_sleep($period)
149  
150 This will sleep for C<$period> seconds but in one-second
151 intervals so that if a SIGINT or SIGHUP arrives the sleep
152 period can end more quickly.
153  
154 Using this makes it safe for C<gd_run_body()> to sleep for
155 longer than C<$Daemon::Generic::force_quit_delay> seconds
156 at a time.
157  
158 =back
159  
160 =head1 ADDITIONAL MEMBER DATA
161  
162 The following additional bits of member data are defined:
163  
164 =over 15
165  
166 =item gd_sigint
167  
168 The time at which an (unprocessed) SIGINT was received.
169  
170 =item gd_sighup
171  
172 The time at which an (unprocessed) SIGHUP was received.
173  
174 =back
175  
176 =head1 LICENSE
177  
178 Copyright (C) 2006-2010 David Muir Sharnoff <muir@idiom.com>.
179 Copyright (C) 2011 Google, Inc.
180 This module may be used and distributed on the same terms
181 as Perl itself.
182  
« lib/Daemon/Generic.pm ↑ Summary lib/Daemon/Generic/Event.pm »