Zing/Daemon.pm

filter: Statementstmt 47.9% 47 / 98 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 61.1% 11 / 18 Podpod 0.0% 0 / 4 totaltotal 42.0% 58 / 138 CC 10 SCAR 22.6
CC
10
Cov
41%
new_pid_dir
20.0
execute
20.0
fork
20.0
CRAP 9.6
« Zing/Fork.pm ↑ Summary Zing/Redis.pm »
1 package Zing::Daemon;
2  
3 49 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 49 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 49 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 49 ▶use registry 'Zing::Types';
Subroutine BEGIN: called CC=1 SCAR=0.0
9 49 ▶use routines;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 49 ▶use Data::Object::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 49 ▶use Data::Object::ClassHas;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 49 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 49 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 49 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 49 ▶use POSIX;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 49 our $VERSION = '0.11'; # VERSION
21  
22 # ATTRIBUTES
23  
24 49 has 'app' => (
25 is => 'ro',
26 isa => 'Zing',
27 req => 1,
28 );
29  
30 49 has 'name' => (
31 is => 'ro',
32 isa => 'Str',
33 req => 1,
34 );
35  
36 49 has 'log' => (
37 is => 'ro',
38 isa => 'Logger',
39 new => 1,
40 );
41  
42 0 ▶fun new_log($self) {
Subroutine new_log: not called CC=1 SCAR=6.9
Pod undocumented
43 0 FlightRecorder->new(level => 'info')
44 49 }
45  
46 49 has 'pid_dir' => (
47 is => 'ro',
48 isa => 'Str',
49 new => 1,
50 );
51  
52 0 ▶fun new_pid_dir($self) {
Subroutine new_pid_dir: not called CC=4 SCAR=30.0
Pod undocumented
53 -w $ENV{ZING_PIDDIR} ? $ENV{ZING_PIDDIR} :
54 -w $ENV{ZING_HOME} ? $ENV{ZING_HOME} :
55 0 ▶ -w File::Spec->curdir ? File::Spec->curdir : File::Spec->tmpdir
Branch: 3 branches
BranchTrueFalseTotal
-w $ENV{'ZING_PIDDIR'} ? : 0 0 0
-w $ENV{'ZING_HOME'} ? : 0 0 0
-w 'File::Spec'->curdir ? : 0 0 0
56 49 }
57  
58 49 has 'pid_file' => (
59 is => 'ro',
60 isa => 'Str',
61 new => 1,
62 );
63  
64 0 ▶fun new_pid_file($self) {
Subroutine new_pid_file: not called CC=1 SCAR=6.9
Pod undocumented
65 0 join '.', $self->name, 'pid'
66 49 }
67  
68 49 has 'pid_path' => (
69 is => 'ro',
70 isa => 'Str',
71 new => 1,
72 );
73  
74 0 ▶fun new_pid_path($self) {
Subroutine new_pid_path: not called CC=1 SCAR=6.9
Pod undocumented
75 0 File::Spec->catfile($self->pid_dir, $self->pid_file)
76 49 }
77  
78 # METHODS
79  
80 0 ▶method execute() {
Subroutine execute: not called CC=4 SCAR=30.0
81 0 my $app = $self->app;
82 0 my $file = $self->pid_path;
83  
84 0 ▶ if (-e $file) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $file) 0 0 0
85 0 $self->log->fatal("pid file exists: $file");
86 0 return 1;
87 }
88  
89 0 ▶ open(my $fh, ">", "$file") or do {
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "$file" or do { $self->log->fatal("pid file error: $!"); return 1 } 0 0 0
90 0 $self->log->fatal("pid file error: $!");
91 0 return 1;
92 };
93  
94 0 my ($cnt, $err) = do {
95 0 local $@;
96 0 (eval{chmod(0644, $file)}, $@)
97 };
98 0 ▶ if ($err) {
Branch: 1 branch
BranchTrueFalseTotal
if ($err) 0 0 0
99 0 $self->log->fatal("pid file error: $err");
100 0 return 1;
101 }
102  
103 # launch app
104 0 my $pid = $self->fork;
105 0 my $name = $self->name;
106  
107 0 print $fh "$pid\n";
108 0 close $fh;
109  
110 0 $self->log->info("app created: $name");
111 0 $self->log->info("pid file created: $file");
112  
113 0 return 0;
114 49 }
115  
116 0 ▶method fork() {
Subroutine fork: not called CC=4 SCAR=30.0
117 0 my $app = $self->app;
118  
119 0 ▶ if ($Config{d_pseudofork}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Zing::Daemon::Config{"d_pseudofork"}) 0 0 0
120 0 Carp::confess "Error on fork: fork emulation not supported";
121 }
122  
123 0 my $pid = fork;
124  
125 0 ▶ if (!defined $pid) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 0 0
elsif ($pid == 0) { } 0 0 0
126 0 Carp::confess "Error on fork: $!";
127 }
128 elsif ($pid == 0) {
129 0 $self->app->start; # child
130 0 unlink $self->pid_path;
131 0 POSIX::_exit(0);
132 }
133  
134 0 return $pid;
135 49 }
136  
137 0 ▶method start() {
Subroutine start: not called CC=1 SCAR=6.9
138 0 exit($self->execute);
139 49 }
140  
141 49 1;
142  
143 =encoding utf8
144  
145 =head1 NAME
146  
147 Zing::Daemon - Process Daemon
148  
149 =cut
150  
151 =head1 ABSTRACT
152  
153 Daemon Process Management
154  
155 =cut
156  
157 =head1 SYNOPSIS
158  
159 use Zing;
160 use Zing::Daemon;
161  
162 my $scheme = ['MyApp', [], 1];
163 my $daemon = Zing::Daemon->new(name => 'app', app => Zing->new(scheme => $scheme));
164  
165 # $daemon->start;
166  
167 =cut
168  
169 =head1 DESCRIPTION
170  
171 This package provides the mechanisms for running a L<Zing> application as a
172 daemon process.
173  
174 =cut
175  
176 =head1 LIBRARIES
177  
178 This package uses type constraints from:
179  
180 L<Zing::Types>
181  
182 =cut
183  
184 =head1 ATTRIBUTES
185  
186 This package has the following attributes:
187  
188 =cut
189  
190 =head2 app
191  
192 app(Zing)
193  
194 This attribute is read-only, accepts C<(Zing)> values, and is required.
195  
196 =cut
197  
198 =head2 log
199  
200 log(Logger)
201  
202 This attribute is read-only, accepts C<(Logger)> values, and is optional.
203  
204 =cut
205  
206 =head2 name
207  
208 name(Str)
209  
210 This attribute is read-only, accepts C<(Str)> values, and is required.
211  
212 =cut
213  
214 =head2 pid_dir
215  
216 pid_dir(Str)
217  
218 This attribute is read-only, accepts C<(Str)> values, and is optional.
219  
220 =cut
221  
222 =head2 pid_file
223  
224 pid_file(Str)
225  
226 This attribute is read-only, accepts C<(Str)> values, and is optional.
227  
228 =cut
229  
230 =head2 pid_path
231  
232 pid_path(Str)
233  
234 This attribute is read-only, accepts C<(Str)> values, and is optional.
235  
236 =cut
237  
238 =head1 METHODS
239  
240 This package implements the following methods:
241  
242 =cut
243  
244 =head2 execute
245  
246 execute() : Int
247  
248 The execute method forks the application and creates a pid file under the
249 L</pid_path>.
250  
251 =over 4
252  
253 =item execute example #1
254  
255 # given: synopsis
256  
257 my $exit = $daemon->execute;
258  
259 =back
260  
261 =cut
262  
263 =head2 fork
264  
265 fork() : Int
266  
267 The fork method forks the application and returns a pid.
268  
269 =over 4
270  
271 =item fork example #1
272  
273 # given: synopsis
274  
275 my $pid = $daemon->fork;
276  
277 =back
278  
279 =cut
280  
281 =head2 start
282  
283 start() : Any
284  
285 The start method executes the application and exits the program with the proper
286 exit code.
287  
288 =over 4
289  
290 =item start example #1
291  
292 # given: synopsis
293  
294 $daemon->start;
295  
296 =back
297  
298 =cut
299  
300 =head1 AUTHOR
301  
302 Al Newkirk, C<awncorp@cpan.org>
303  
304 =head1 LICENSE
305  
306 Copyright (C) 2011-2019, Al Newkirk, et al.
307  
308 This is free software; you can redistribute it and/or modify it under the terms
309 of the The Apache License, Version 2.0, as elucidated in the L<"license
310 file"|https://github.com/iamalnewkirk/zing/blob/master/LICENSE>.
311  
312 =head1 PROJECT
313  
314 L<Wiki|https://github.com/iamalnewkirk/zing/wiki>
315  
316 L<Project|https://github.com/iamalnewkirk/zing>
317  
318 L<Initiatives|https://github.com/iamalnewkirk/zing/projects>
319  
320 L<Milestones|https://github.com/iamalnewkirk/zing/milestones>
321  
322 L<Contributing|https://github.com/iamalnewkirk/zing/blob/master/CONTRIBUTE.md>
323  
324 L<Issues|https://github.com/iamalnewkirk/zing/issues>
325  
326 =cut
« Zing/Fork.pm ↑ Summary Zing/Redis.pm »