blib/lib/Daemonise.pm

filter: Statementstmt 37.7% 46 / 122 Branchbran 9.0% 4 / 44 Conditioncond 7.1% 1 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 41.3% 12 / 29 Podpod 100% 8 / 8 totaltotal 31.4% 71 / 226 CC 28 SCAR 29.0
CC
28
Cov
28%
__ANON__
45.7
dump
42.0
configure
20.0
CRAP 18.2
« lib/Daemonise/Plugin/Syslog.pm ↑ Summary blib/lib/Daemonise/Plugin/HipChat.pm »
1 package Daemonise;
2  
3 2 ▶use Modern::Perl;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Mouse;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use FindBin qw($Bin);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use lib "$Bin/../lib";
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 # ABSTRACT: Daemonise - a general daemoniser for anything...
10  
11 2 our $VERSION = '2.13'; # VERSION
12  
13 2 ▶use Config::Any;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use POSIX qw(strftime SIGTERM SIG_BLOCK SIG_UNBLOCK);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16  
17 has 'name' => (
18 is => 'rw',
19 2 ▶ default => sub { (my $name = basename($0)) =~ s/\.[^.]+$//; $name },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 2 predicate => 'has_name',
21 );
22  
23  
24 2 has 'hostname' => (
25 is => 'ro',
26 isa => 'Str',
27 lazy => 1,
28 builder => 'build_hostname',
29 );
30  
31  
32 2 has 'config_file' => (
33 is => 'rw',
34 isa => "Str",
35 predicate => 'has_config_file',
36 );
37  
38  
39 has 'config' => (
40 is => 'rw',
41 isa => 'HashRef',
42 lazy => 1,
43 0 ▶ default => sub { {} },
Subroutine __ANON__: not called CC=1 SCAR=6.9
44 2 );
45  
46  
47 has 'debug' => (
48 is => 'rw',
49 isa => 'Bool',
50 lazy => 1,
51 1 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
52 2 );
53  
54  
55 has 'start_time' => (
56 is => 'rw',
57 isa => 'Int',
58 2 ▶ default => sub { time },
Subroutine __ANON__: called CC=1 SCAR=0.0
59 2 );
60  
61  
62 has 'is_cron' => (
63 is => 'rw',
64 isa => 'Bool',
65 lazy => 1,
66 0 ▶ default => sub { 0 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 2 );
68  
69  
70 has 'cache_plugin' => (
71 is => 'rw',
72 isa => 'Str',
73 lazy => 1,
74 0 ▶ default => sub { 'KyotoTycoon' },
Subroutine __ANON__: not called CC=1 SCAR=6.9
75 2 );
76  
77  
78 has 'print_log' => (
79 is => 'rw',
80 isa => 'Bool',
81 lazy => 1,
82 0 ▶ default => sub { 1 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
83 2 );
84  
85  
86 2 ▶after 'new' => sub {
Subroutine __ANON__: called CC=10 SCAR=38.2
87 2 my ($class, %args) = @_;
88  
89 # backwards compatibility
90 with('Daemonise::Plugin::Daemon')
91 2 ▶ unless ($args{no_daemon} or $args{is_cron});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"no_daemon"} or $args{"is_cron"} 0 2 2
Condition33%1/3
$args{"no_daemon"} or $args{"is_cron"}
l!l&&r!l&&!r
200
MC/DC0%0/2
$args{"no_daemon"} or $args{"is_cron"}
$args{"no_daemon"} $args{"is_cron"}
Truth table33%1/3
A or B
A= $args{"no_daemon"}B= $args{"is_cron"}
ABresult
1X1
011
000
92 with('Daemonise::Plugin::Syslog')
93 2 ▶ unless ($args{no_syslog});
Branch: 1 branch
BranchTrueFalseTotal
unless $args{"no_syslog"} 2 0 2
94  
95 # load cache plugin required for cron locking
96 2 ▶ if ($args{is_cron}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"is_cron"}) 0 2 2
97 0 ▶ my $cache = ($args{cache_plugin} || 'KyotoTycoon');
Condition0%0/2
$args{'cache_plugin'} || 'KyotoTycoon'
l!l
00
MC/DC0%0/1
$args{'cache_plugin'} || 'KyotoTycoon'
$args{'cache_plugin'}
Truth table0%0/2
A
A= $args{'cache_plugin'}
Aresult
11
00
98  
99 0 ▶ return if %Daemonise::Plugin::KyotoTycoon:: and $cache eq 'KyotoTycoon';
Branch: 1 branch
BranchTrueFalseTotal
if %Daemonise::Plugin::KyotoTycoon:: and $cache eq "KyotoTycoon" 0 0 0
Condition0%0/3
%Daemonise::Plugin::KyotoTycoon:: and $cache eq "KyotoTycoon"
!ll&&!rl&&r
000
MC/DC0%0/2
%Daemonise::Plugin::KyotoTycoon:: and $cache eq "KyotoTycoon"
%Daemonise::Plugin::KyotoTycoon:: $cache eq "KyotoTycoon"
Truth table0%0/3
A and B
A= %Daemonise::Plugin::KyotoTycoon::B= $cache eq "KyotoTycoon"
ABresult
0X0
100
111
100 0 ▶ return if %Daemonise::Plugin::Redis:: and $cache eq 'Redis';
Branch: 1 branch
BranchTrueFalseTotal
if %Daemonise::Plugin::Redis:: and $cache eq "Redis" 0 0 0
Condition0%0/3
%Daemonise::Plugin::Redis:: and $cache eq "Redis"
!ll&&!rl&&r
000
MC/DC0%0/2
%Daemonise::Plugin::Redis:: and $cache eq "Redis"
%Daemonise::Plugin::Redis:: $cache eq "Redis"
Truth table0%0/3
A and B
A= %Daemonise::Plugin::Redis::B= $cache eq "Redis"
ABresult
0X0
100
111
101  
102 0 my $cache_plugin = "Daemonise::Plugin::" . $cache;
103 0 with($cache_plugin);
104 }
105  
106 2 return;
107 2 };
108  
109  
110 1 ▶sub load_plugin {
Subroutine load_plugin: called CC=2 SCAR=7.4
Pod documented
111 1 my ($self, $plugin) = @_;
112  
113 1 my $plug = 'Daemonise::Plugin::' . $plugin;
114 1 ▶ $self->log("loading $plugin plugin") if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 1 1
115 1 with($plug);
116  
117 0 return;
118 }
119  
120  
121 0 ▶sub configure {
Subroutine configure: not called CC=4 SCAR=30.0
122 0 my ($self, $reconfig) = @_;
123  
124 ### install a signal handler as anchor for clean shutdowns in plugins
125 0 ▶ $SIG{QUIT} = sub { $self->stop }; ## no critic
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 ▶ $SIG{TERM} = sub { $self->stop }; ## no critic
Subroutine __ANON__: not called CC=1 SCAR=6.9
127 0 ▶ $SIG{INT} = sub { $self->stop }; ## no critic
Subroutine __ANON__: not called CC=1 SCAR=6.9
128  
129 0 ▶ unless ($self->has_config_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->has_config_file) 0 0 0
130 0 $self->log("config_file unset, nothing to configure");
131 0 return;
132 }
133  
134 0 ▶ unless (-e $self->config_file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-e $self->config_file) 0 0 0
135 0 $self->log(
136 "ERROR: config file '" . $self->config_file . "' does not exist!");
137 0 return;
138 }
139  
140 0 my $conf = Config::Any->load_files({
141 files => [ $self->config_file ],
142 use_ext => 1,
143 driver_args => { General => { -InterPolateEnv => 1 } },
144 });
145 0 ▶ $conf = $conf->[0]->{ $self->config_file } if $conf;
Branch: 1 branch
BranchTrueFalseTotal
if $conf 0 0 0
146  
147 0 $self->config($conf);
148  
149 0 return;
150 }
151  
152  
153 0 ▶sub async {
Subroutine async: not called CC=4 SCAR=30.0
Pod documented
154 0 my ($self) = @_;
155  
156 ### block signal for fork
157 0 my $sigset = POSIX::SigSet->new(SIGTERM);
158 0 ▶ POSIX::sigprocmask(SIG_BLOCK, $sigset)
Branch: 1 branch
BranchTrueFalseTotal
POSIX::sigprocmask(0, $sigset) or die "Can't block SIGTERM for fork: [$!]\n" 0 0 0
159 or die "Can't block SIGTERM for fork: [$!]\n";
160  
161 ### fork off a child
162 0 my $pid = fork;
163 0 ▶ unless (defined $pid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $pid) 0 0 0
164 0 die "Couldn't fork: [$!]\n";
165 }
166  
167 ### make SIGTERM kill us as it did before
168 0 local $SIG{TERM} = 'DEFAULT';
169  
170 ### put back to normal
171 0 ▶ POSIX::sigprocmask(SIG_UNBLOCK, $sigset)
Branch: 1 branch
BranchTrueFalseTotal
POSIX::sigprocmask(1, $sigset) or die "Can't unblock SIGTERM for fork: [$!]\n" 0 0 0
172 or die "Can't unblock SIGTERM for fork: [$!]\n";
173  
174 0 return $pid;
175 }
176  
177  
178 0 ▶sub log { ## no critic (ProhibitBuiltinHomonyms)
Subroutine log: not called CC=2 SCAR=17.9
179 0 my ($self, $msg) = @_;
180  
181 0 ▶ return unless $self->print_log;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->print_log 0 0 0
182  
183 0 chomp($msg);
184 0 my $now = strftime "%F %T", localtime;
185 0 say("${now} " . $self->hostname . ' ' . $self->name . "[$$]: $msg");
186  
187 0 return;
188 }
189  
190  
191 0 ▶sub notify {
Subroutine notify: not called CC=1 SCAR=6.9
Pod documented
192 0 my ($self, $msg, $room, $severity, $notify_users, $message_format) = @_;
193  
194 0 $self->log($msg);
195  
196 0 return;
197 }
198  
199  
200 0 ▶sub start {
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
201 0 my ($self) = @_;
202  
203 0 $self->log("rabbit starting");
204  
205 0 return;
206 }
207  
208  
209 0 ▶sub stop {
Subroutine stop: not called CC=2 SCAR=17.9
Pod documented
210 0 my ($self) = @_;
211  
212 0 $self->log("good bye cruel world!");
213 0 ▶ $self->graph('hase.' . $self->name, 'stopped', 1) if $self->can('graph');
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("graph") 0 0 0
214  
215 0 exit;
216 }
217  
218  
219 0 ▶sub round {
Subroutine round: not called CC=2 SCAR=17.9
Pod documented
220 0 my ($self, $float) = @_;
221  
222 # some stupid perl versions on some platforms can't round correctly and i
223 # don't want to use more modules
224 0 ▶ $float += 0.001 if ($float =~ m/\.[0-9]{2}5/);
Branch: 1 branch
BranchTrueFalseTotal
if $float =~ /\.[0-9]{2}5/u 0 0 0
225  
226 0 return sprintf('%.2f', sprintf('%.10f', $float + 0)) + 0;
227 }
228  
229  
230 0 ▶sub dump { ## no critic (ProhibitBuiltinHomonyms)
Subroutine dump: not called CC=6 SCAR=37.4
Pod documented
231 0 my ($self, $obj, $nocolor_multiline) = @_;
232  
233 0 my %options;
234 0 ▶ if ($self->debug) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->debug) { } 0 0 0
235 0 ▶ $options{colored} = $nocolor_multiline ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$nocolor_multiline ? : 0 0 0
236 }
237 else {
238 0 $options{colored} = 0;
239 0 ▶ $options{multiline} = $nocolor_multiline ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$nocolor_multiline ? : 0 0 0
240 }
241  
242 0 require Data::Printer;
243 0 ▶ Data::Printer->import(%options) unless __PACKAGE__->can('np');
Branch: 1 branch
BranchTrueFalseTotal
unless __PACKAGE__->can("np") 0 0 0
244  
245 0 my $dump;
246 0 ▶ if (ref $obj) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $obj) { } 0 0 0
247 0 $dump = np($obj, %options);
248 }
249 else {
250 0 $dump = np(\$obj, %options);
251 }
252  
253 0 return $dump;
254 }
255  
256  
257 0 ▶sub stdout_redirect { }
Subroutine stdout_redirect: not called CC=1 SCAR=0.0
258  
259  
260 0 ▶sub build_hostname {
Subroutine build_hostname: not called CC=4 SCAR=30.0
Pod documented
261 0 my ($self) = @_;
262  
263 0 my $hostname = `hostname -s`;
264 0 chomp $hostname;
265  
266 # support freebsd jails with custom rc.conf variable
267 0 ▶ if ($^O eq 'freebsd' and -x '/usr/sbin/sysrc') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "freebsd" and -x "/usr/sbin/sysrc") 0 0 0
Condition0%0/3
$^O eq "freebsd" and -x "/usr/sbin/sysrc"
!ll&&!rl&&r
000
MC/DC0%0/2
$^O eq "freebsd" and -x "/usr/sbin/sysrc"
$^O eq "freebsd" -x "/usr/sbin/sysrc"
Truth table0%0/3
A and B
A= $^O eq "freebsd"B= -x "/usr/sbin/sysrc"
ABresult
0X0
100
111
268 0 my $h = `/usr/sbin/sysrc -n jail_host`;
269 0 ▶ $hostname = "$hostname\@$h" unless $h =~ m/unknown variable/;
Branch: 1 branch
BranchTrueFalseTotal
unless $h =~ /unknown variable/u 0 0 0
270 0 chomp $hostname;
271 }
272  
273 0 return $hostname;
274 }
275  
276  
277 2 1; # End of Daemonise
278  
279 __END__
« lib/Daemonise/Plugin/Syslog.pm ↑ Summary blib/lib/Daemonise/Plugin/HipChat.pm »