lib/Ubic/Service/SimpleDaemon.pm

filter: Statementstmt 78.5% 77 / 98 Branchbran 55.0% 22 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.4% 19 / 21 Podpod 100% 11 / 11 totaltotal 75.8% 129 / 170 SCAR 35.5
CC
23
Cov
72%
__ANON__
12.0
start_impl
9.2
new
8.7
CRAP 34.9
« lib/Ubic/UA.pm ↑ Summary blib/lib/Ubic/ServiceLoader/Ext/json.pm »
1 package Ubic::Service::SimpleDaemon;
2 143 $Ubic::Service::SimpleDaemon::VERSION = '1.59';
3 143 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 143 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 # ABSTRACT: service module for daemonizing any binary
7  
8  
9 143 ▶use parent qw(Ubic::Service::Skeleton);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 143 ▶use Ubic::Daemon qw(start_daemon stop_daemon check_daemon);
Subroutine BEGIN: called CC=1 SCAR=0.0
12 143 ▶use Ubic::Result qw(result);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 143 ▶use Ubic::Settings;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 143 ▶use Params::Validate qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 # Beware - this code will ignore any overrides if you're using custom Ubic->new(...) objects
18 143 our $PID_DIR;
19  
20 sub _pid_dir {
21 296 ▶ return $PID_DIR if defined $PID_DIR;
Subroutine _pid_dir: called CC=3 SCAR=11.8
Branch: 1 branch
BranchTrueFalseTotal
if defined $Ubic::Service::SimpleDaemon::PID_DIR 255 41 296
22 41 ▶ if ($ENV{UBIC_DAEMON_PID_DIR}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{'UBIC_DAEMON_PID_DIR'}) { } 0 41 41
23 0 warn "UBIC_DAEMON_PID_DIR env variable is deprecated, use Ubic->set_data_dir or configs instead (see Ubic::Settings for details)";
24 0 $PID_DIR = $ENV{UBIC_DAEMON_PID_DIR};
25 }
26 else {
27 41 $PID_DIR = Ubic::Settings->data_dir."/simple-daemon/pid";
28 }
29 41 return $PID_DIR;
30 }
31  
32 sub _send_signal {
33 11 ▶ my ($self, $signal) = @_;
Subroutine _send_signal: called CC=2 SCAR=6.9
34  
35 11 my $daemon = check_daemon($self->pidfile);
36 11 ▶ unless ($daemon) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($daemon) 4 7 11
37 4 return result('not running');
38 }
39  
40 7 my $pid = $daemon->pid;
41 7 kill $signal => $pid;
42  
43 7 my $guardian_pid = $daemon->guardian_pid;
44 7 kill HUP => $guardian_pid;
45  
46 7 return result('reloaded', "sent $self->{reload_signal} to $pid, sent HUP to $guardian_pid");
47 }
48  
49 sub new {
50 500 ▶ my $class = shift;
Subroutine new: called CC=4 SCAR=21.7
Pod documented
51 500 my $params = validate(@_, {
52 bin => { type => SCALAR | ARRAYREF },
53 user => { type => SCALAR, optional => 1 },
54 group => { type => SCALAR | ARRAYREF, optional => 1 },
55 daemon_user => { type => SCALAR, optional => 1 },
56 daemon_group => { type => SCALAR | ARRAYREF, optional => 1 },
57 name => { type => SCALAR, optional => 1 },
58 pidfile => { type => SCALAR, optional => 1 },
59 stdout => { type => SCALAR, optional => 1 },
60 stderr => { type => SCALAR, optional => 1 },
61 ubic_log => { type => SCALAR, optional => 1 },
62 proxy_logs => { type => BOOLEAN, optional => 1 },
63 cwd => { type => SCALAR, optional => 1 },
64 env => { type => HASHREF, optional => 1 },
65 reload_signal => { type => SCALAR, optional => 1 },
66 term_timeout => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
67 stop_timeout => { type => SCALAR, optional => 1, regex => qr/^\d+$/ },
68 ulimit => { type => HASHREF, optional => 1 },
69 auto_start => { type => BOOLEAN, default => 0 },
70 });
71  
72 500 ▶ if ($params->{ulimit}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($params->{"ulimit"}) 0 500 500
73 # load BSD::Resource lazily, but fail fast if we're asked for it
74 0 eval "require BSD::Resource";
75 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
76 0 die "BSD::Resource is not installed";
77 }
78 0 ▶ if (BSD::Resource->VERSION < 1.29) {
Branch: 1 branch
BranchTrueFalseTotal
if ("BSD::Resource"->VERSION < "1.29") 0 0 0
79 # 1.29 supports string names for resources
80 0 die "BSD::Resource >= 1.29 required";
81 }
82 }
83  
84 500 return bless {%$params} => $class;
85 }
86  
87 sub pidfile {
88 296 ▶ my ($self) = @_;
Subroutine pidfile: called CC=3 SCAR=11.4
Pod documented
89 296 ▶ return $self->{pidfile} if exists($self->{pidfile});
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"pidfile"} 0 296 296
90 296 ▶ my $name = $self->full_name or die "Can't start nameless SimpleDaemon";
Branch: 1 branch
BranchTrueFalseTotal
my $name = $self->full_name or die "Can't start nameless SimpleDaemon" 0 296 296
91 296 return _pid_dir."/$name";
92 }
93  
94 sub start_impl {
95 49 ▶ my ($self) = @_;
Subroutine start_impl: called CC=6 SCAR=22.1
Pod documented
96  
97 my $start_params = {
98 pidfile => $self->pidfile,
99 bin => $self->{bin},
100 49 };
101 49 for (qw/ env cwd stdout stderr ubic_log proxy_logs term_timeout /) {
102 343 ▶ $start_params->{$_} = $self->{$_} if defined $self->{$_};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{$_} 67 276 343
103 }
104 49 ▶ if ($self->{reload_signal}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"reload_signal"}) 6 43 49
105 6 $start_params->{proxy_logs} = 1;
106 }
107 49 ▶ if (defined $self->{daemon_user}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"daemon_user"}) 0 49 49
108 $start_params->{credentials} = Ubic::Credentials->new(
109 user => $self->{daemon_user},
110 group => $self->{daemon_group},
111 0 );
112 }
113 49 ▶ if (defined $self->{ulimit}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"ulimit"}) 0 49 49
114 $start_params->{start_hook} = sub {
115 0 ▶ for my $name (keys %{$self->{ulimit}}) {
Subroutine __ANON__: not called CC=3 SCAR=24.8
116 0 my $value = $self->{ulimit}{$name};
117 0 my $result = BSD::Resource::setrlimit($name, $value, $value);
118 0 ▶ unless ($result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($result) 0 0 0
119 0 die "Failed to set $name=$value ulimit";
120 }
121 }
122 0 };
123 }
124 49 start_daemon($start_params);
125 }
126  
127 sub user {
128 340 ▶ my $self = shift;
Subroutine user: called CC=2 SCAR=6.9
Pod documented
129 340 ▶ return $self->{user} if defined $self->{user};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"user"} 1 339 340
130 339 return $self->SUPER::user();
131 }
132  
133 sub group {
134 337 ▶ my $self = shift;
Subroutine group: called CC=3 SCAR=15.1
Pod documented
135 337 my $groups = $self->{group};
136 337 ▶ return $self->SUPER::group() if not defined $groups;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $groups 337 0 337
137 0 ▶ return @$groups if ref $groups eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $groups eq "ARRAY" 0 0 0
138 0 return $groups;
139 }
140  
141 sub stop_impl {
142 31 ▶ my ($self) = @_;
Subroutine stop_impl: called CC=2 SCAR=7.0
Pod documented
143 31 my $options = {};
144 31 ▶ $options->{timeout} = $self->{stop_timeout} if $self->{stop_timeout};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"stop_timeout"} 0 31 31
145 31 stop_daemon($self->pidfile,$options);
146 }
147  
148 sub status_impl {
149 205 ▶ my ($self) = @_;
Subroutine status_impl: called CC=2 SCAR=6.9
Pod documented
150 205 ▶ if (my $daemon = check_daemon($self->pidfile)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $daemon = check_daemon($self->pidfile)) { } 87 118 205
151 87 return result('running', "pid ".$daemon->pid);
152 }
153 else {
154 118 return result('not running');
155 }
156 }
157  
158 sub reload {
159 21 ▶ my $self = shift;
Subroutine reload: called CC=2 SCAR=6.9
Pod documented
160 21 ▶ unless (defined $self->{reload_signal}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"reload_signal"}) 10 11 21
161 10 return result('unknown', 'not implemented');
162 }
163 11 return $self->_send_signal($self->{reload_signal});
164 }
165  
166 sub auto_start {
167 47 ▶ my $self = shift;
Subroutine auto_start: called CC=1 SCAR=0.0
Pod documented
168 47 return $self->{auto_start};
169 }
170  
171  
172 143 our %SIGNALS = (
173 sigusr1 => 'SIGUSR1',
174 sigusr2 => 'SIGUSR2',
175 );
176  
177 sub custom_commands {
178 26 ▶ my ($self) = @_;
Subroutine custom_commands: called CC=1 SCAR=0.0
Pod documented
179 26 return keys %SIGNALS;
180 }
181  
182 sub do_custom_command {
183 0 ▶ my ($self, $command) = @_;
Subroutine do_custom_command: not called CC=2 SCAR=17.9
Pod documented
184 0 ▶ unless (defined $SIGNALS{$command}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $Ubic::Service::SimpleDaemon::SIGNALS{$command}) 0 0 0
185 0 return result('unknown', 'not implemented');
186 }
187  
188 0 return $self->_send_signal($SIGNALS{$command});
189 }
190  
191  
192 143 1;
193  
194 __END__
« lib/Ubic/UA.pm ↑ Summary blib/lib/Ubic/ServiceLoader/Ext/json.pm »