blib/lib/App/derived.pm

filter: Statementstmt 90.7% 108 / 119 Branchbran 66.6% 20 / 30 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 7 totaltotal 83.4% 146 / 175 CC 17 SCAR 14.9
CC
17
Cov
86%
cap_cmd
8.6
worker
7.1
DESTROY
6.0
CRAP 4.5
« blib/lib/App/derived/Plugin/Memcached.pm ↑ Summary lib/App/derived/Plugin.pm »
1 package App::derived;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use File::Temp qw/tempfile/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Proclet;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use JSON ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Log::Minimal;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 9 our $VERSION = '0.10';
13  
14 9 my $_JSON = JSON->new()
15 ->utf8(1)
16 ->shrink(1)
17 ->space_before(0)
18 ->space_after(0)
19 ->indent(0);
20  
21 8 ▶sub new {
Subroutine new: called CC=3 SCAR=12.0
Pod undocumented
22 8 my $class = shift;
23 0 ▶ my %opt = ref $_[0] ? %{$_[0]} : @_;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 8 8
24 my %args = (
25 8 ▶ proclet => Proclet->new(enable_log_worker => ($ENV{LM_DEBUG} ? 1 : 0)),
Branch: 1 branch
BranchTrueFalseTotal
$ENV{'LM_DEBUG'} ? : 0 8 8
26 interval => 10,
27 host => 0,
28 port => 12306,
29 timeout => 10,
30 services => {},
31 %opt
32 );
33 8 bless \%args, $class;
34 }
35  
36 16 ▶sub add_service {
Subroutine add_service: called CC=1 SCAR=0.0
Pod undocumented
37 16 my $self = shift;
38 16 my ($key, $cmd) = @_;
39 16 my ($tmpfh,$tmpfile) = tempfile(UNLINK=>0, EXLOCK=>0);
40 16 print $tmpfh $_JSON->encode({
41 status=>"INIT",
42 persec => '0E0',
43 latest => '0E0',
44 });
45 16 close $tmpfh;
46 16 $self->{services}->{$key} = {
47 cmd => ['bash', '-c', $cmd],
48 file => $tmpfile,
49 prev => undef,
50 };
51 16 infof("register service: %s", $key);
52 $self->{proclet}->service(
53 6 ▶ code => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
54 6 $0 = "$0 worker $key";
55 6 $self->worker($key);
56 2 exit;
57 },
58 16 tag => $key.'_worker',
59 );
60 }
61  
62 8 ▶sub add_plugin {
Subroutine add_plugin: called CC=1 SCAR=0.0
Pod undocumented
63 8 my $self = shift;
64 8 my ( $plugin, $args) = @_;
65  
66 my %args = (
67 %$args,
68 _services => $self->{services},
69 _proclet => $self->{proclet},
70 8 );
71 8 infof("register plugin: %s", $plugin);
72 8 my $instance = $plugin->new(\%args);
73 8 $instance->init();
74 }
75  
76 8 ▶sub run {
Subroutine run: called CC=1 SCAR=0.0
Pod undocumented
77 8 my $self = shift;
78 8 $self->{proclet}->run;
79 }
80  
81 0 ▶sub DESTROY {
Subroutine DESTROY: not called CC=2 SCAR=17.9
82 0 my $self = shift;
83 0 for my $key ( keys %{$self->{services}} ) {
84 0 unlink $self->{services}->{$key}->{file};
85 }
86 }
87  
88 6 ▶sub worker {
Subroutine worker: called CC=7 SCAR=19.6
Pod undocumented
89 6 my ($self, $service_key) = @_;
90 6 srand();
91 6 my $service = $self->{services}->{$service_key};
92 6 my $n = time;
93 6 $n = $n - ( $n % $self->{interval}) + $self->{interval} + int(rand($self->{interval}));; #next + random
94 6 my $stop = 1;
95 6 ▶ local $SIG{TERM} = sub { $stop = 0 };
Subroutine __ANON__: called CC=1 SCAR=0.0
96  
97 6 ▶ while ( $stop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stop) 10 2 12
98 10 my $current = time();
99 10 ▶ while ( $n < $current ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n < $current) 0 10 10
100 0 $n = $n + $self->{interval};
101 }
102 10 ▶ while ( $stop ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($stop) 34 0 34
103 34 ▶ last if time() >= $n;
Branch: 1 branch
BranchTrueFalseTotal
if time >= $n 10 24 34
104 24 select undef, undef, undef, 0.1 ## no critic;
105 }
106 10 $n = $n + $self->{interval};
107 10 local $Log::Minimal::AUTODUMP = 1;
108 10 debugf("exec command for %s => %s", $service_key, $service);
109 10 my ($result, $exit_code) = cap_cmd($service->{cmd});
110 6 debugf("command [%s]: exit_code:%s result:%s", $service_key, $exit_code, $result);
111 6 ▶ if ( ! defined $result ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $result) 0 6 6
112 atomic_write($service->{file}, {
113 0 status => "ERROR",
114 persec => undef,
115 latest => undef,
116 raw => undef,
117 exit_code => $exit_code,
118 last_update => time,
119 });
120 0 next;
121 }
122
123 6 my $orig = $result;
124 6 $result =~ s!^[^0-9]+!!;
125 {
126 9 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
127 6 $result = int($result);
128 }
129 6 ▶ if ( ! defined $service->{prev} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $service->{"prev"}) 4 2 6
130 4 $service->{prev} = $result;
131 atomic_write( $service->{file}, {
132 4 status => "CALCURATE",
133 persec => "E0E",
134 latest => $result,
135 raw => $orig,
136 exit_code => $exit_code,
137 last_update => time,
138 });
139 4 next;
140 }
141 2 my $derive = ($result - $service->{prev}) / $self->{interval};
142 atomic_write( $service->{file}, {
143 2 status => "OK",
144 persec => $derive,
145 latest => $result,
146 raw => $orig,
147 exit_code => $exit_code,
148 last_update => time,
149 });
150 2 $service->{prev} = $result;
151 }
152 }
153  
154 10 ▶sub cap_cmd {
Subroutine cap_cmd: called CC=8 SCAR=21.6
Pod undocumented
155 10 my ($cmdref) = @_;
156 10 ▶ pipe my $logrh, my $logwh
Branch: 1 branch
BranchTrueFalseTotal
pipe my $logrh, my $logwh or die "Died: failed to create pipe:$!" 10 0 10
157 or die "Died: failed to create pipe:$!";
158 10 my $pid = fork;
159 10 ▶ if ( ! defined $pid ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $pid) { } 0 10 10
elsif ($pid == 0) { } 4 6 10
160 0 die "Died: fork failed: $!";
161 }
162  
163 elsif ( $pid == 0 ) {
164 #child
165 4 close $logrh;
166 4 ▶ open STDOUT, '>&', $logwh
Branch: 1 branch
BranchTrueFalseTotal
open STDOUT, ">&", $logwh or die "Died: failed to redirect STDOUT" 4 0 4
167 or die "Died: failed to redirect STDOUT";
168 4 close $logwh;
169 4 exec @$cmdref;
170 0 die "Died: exec failed: $!";
171 }
172 6 close $logwh;
173 6 my $result;
174 6 ▶ while(<$logrh>){
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $logrh)) 6 6 12
175 6 chomp;chomp;
176 6 $result .= $_;
177 }
178 6 close $logrh;
179 6 ▶ while (wait == -1) {}
Branch: 1 branch
BranchTrueFalseTotal
if (wait == -1) 0 6 6
180 6 my $exit_code = $?;
181 6 $exit_code = $exit_code >> 8;
182 6 ▶ if ( $exit_code != 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exit_code != 0) 0 6 6
183 0 warnf("Error: command exited with code: $exit_code");
184 }
185 6 return ($result, $exit_code);
186 }
187  
188 6 ▶sub atomic_write {
Subroutine atomic_write: called CC=1 SCAR=0.0
Pod undocumented
189 6 my ($writefile, $body) = @_;
190 6 my ($tmpfh,$tmpfile) = tempfile(UNLINK=>0);
191 6 print $tmpfh $_JSON->encode($body);
192 6 close($tmpfh);
193 6 move( $tmpfile, $writefile);
194 }
195  
196  
197 9 1;
198 __END__
« blib/lib/App/derived/Plugin/Memcached.pm ↑ Summary lib/App/derived/Plugin.pm »