blib/lib/MYDan/Collector/Jobs.pm untested

filter: Statementstmt 0.0% 0 / 173 Branchbran 0.0% 0 / 46 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 282 CC 26 SCAR 50.4
CC
26
Cov
0%
run
342.0
new
56.0
task
12.0
CRAP 153.8
1 package MYDan::Collector::Jobs;
2 use strict;
3 use warnings;
4  
5 use Carp;
6 use YAML::XS;
7  
8 use Digest::MD5;
9 use Sys::Hostname;
10  
11 use threads;
12 use Thread::Queue;
13  
14 use MYDan::Collector::Push;
15 use MYDan::Collector::Sock::Data;
16 use MYDan::Collector::Sock::Ring;
17 use MYDan::Collector::Stat::Backup;
18 use MYDan::Collector::Util;
19  
20 use POSIX qw( :sys_wait_h );
21 #use Time::HiRes qw( time sleep alarm stat );
22  
23 use Data::Dumper;
24  
25 sub new
26 {
27 my ( $class, %this ) = @_;
28  
29 $this{jobs} = '' unless $this{jobs} && $this{jobs} =~ /^[\w_]+$/;
30 map{ $this{$_} = "$this{$_}_$this{jobs}" if $this{jobs} }qw( data logs );
31 map{
32 confess "no $_\n" unless $this{$_};
33 system "mkdir -p '$this{$_}'" unless -d $this{$_}
34 }
35 qw( conf code logs data );
36
37 $this{conf} = sprintf "$this{conf}/config%s", $this{jobs} ? "_$this{jobs}" : '';
38  
39 MYDan::Collector::Sock::Data->new( path => "$this{data}/output.sock" )->run();
40 MYDan::Collector::Sock::Ring->new( path => "$this{data}/ring.sock" )->run();
41  
42 $MYDan::Collector::Stat::Backup::path = "$this{data}/backup";
43  
44 $this{config} = eval{ YAML::XS::LoadFile $this{conf} };
45 confess "load config fail:$@\n" if $@;
46  
47 $this{md5} = Digest::MD5->new->add( YAML::XS::Dump $this{config} )->hexdigest;
48  
49 return bless \%this, ref $class || $class;
50 }
51  
52 sub run
53 {
54 my ( $this, $index ) = shift;
55  
56 my $queue = Thread::Queue->new;
57 # $SIG{ALRM} = sub{ die "code timeout."; };
58  
59 my $config = $this->{config};
60 my $conf = $config->{conf};
61 my $push = MYDan::Collector::Push->new( %{$config->{'push'}} ) if $config->{'push'};
62  
63 threads::async
64 {
65 while( sleep 10 )
66 {
67 my $conf = eval{ YAML::XS::LoadFile $this->{conf} };
68 if( $@ ){ warn "load config fail. exit(1).\n"; exit 1 };
69  
70 my $curr = Digest::MD5->new->add( YAML::XS::Dump $conf )->hexdigest;
71 if ( $curr ne $this->{md5} )
72 { warn "conf file has changed. exit(1).\n"; exit 1; }
73 }
74 }->detach;
75  
76 threads::async
77 {
78 my $index = MYDan::Collector::Util::qx( "ls -tr $this->{logs}/output.* |tail -n 1" )
79 =~ /\/output\.(\d+)\n$/ ? $1 : 0;
80  
81 while( 1 )
82 {
83 if (( stat "$this->{data}/output" )[9] > time - 90 )
84 {
85 $index = 1 if ++$index > 1024;
86 MYDan::Collector::Util::system "cp '$this->{data}/output' '$this->{logs}/output.$index'";
87 sleep 600;
88 }
89 else { sleep 30; }
90 }
91 }->detach;
92  
93  
94 sub task
95 {
96 my ( $t, $conf, $this, $config, $queue ) = @_;
97 $SIG{'KILL'} = sub { print "$t exit\n";threads->exit(); };
98 $SIG{'CHLD'} = sub {
99 my $kid;
100 do {
101 $kid = waitpid(-1, WNOHANG);
102 } while $kid > 0;
103 };
104 print "init $t\n";
105 #my $conf = $conf->{$t};
106 my ( $interval, $timeout, $code, $param, $i )
107 = @$conf{qw( interval timeout code param )};
108  
109 $interval ||= 60; $timeout ||= $interval;
110  
111 $code = do "$this->{code}/$config->{conf}{$t}{code}";
112 unless( $code && ref $code eq 'CODE' )
113 {
114 warn "load code fail: $t\n";
115 exit 1;
116 }
117  
118 while(1)
119 {
120 printf "do $t (%d)...\n", ++ $i;
121 my $time = time;
122 eval
123 {
124 my $data = &$code( %$param );
125 $queue->enqueue( 'data', $t, YAML::XS::Dump $data );
126 };
127 $queue->enqueue( 'code', $t,
128 YAML::XS::Dump [ time - $time, $time, $@ ||'' ]
129 );
130  
131 my $due = $time + $interval - time;
132 sleep $due if $due > 0;
133 }
134 };
135  
136 my ( %thr, %tid );
137 map{
138 $thr{$_} = threads->create( 'task', ( $_, $conf->{$_}, $this, $config, $queue ) );
139 $tid{$thr{$_}->tid()} = $_;
140 }keys %$conf;
141  
142  
143 my %timeout = map{ $_ => $conf->{$_}{timeout} || 60 }keys %$conf;
144 my %interval = map{ $_ => $conf->{$_}{interval} || 60}keys %$conf;
145 my ( $time, %data, %time ) = time;
146  
147 my $prev = 0;
148 while( 1 )
149 {
150 printf "do(%d)...\n", ++ $index;
151
152 while( $queue->pending )
153 {
154 my ( $type, $name, $data ) = $queue->dequeue( 3 );
155 $data{$type}{$name} = YAML::XS::Load $data;
156 MYDan::Collector::Sock::Ring::push( $data{$type}{$name} ) if $type eq 'data';
157 }
158  
159 my $uptime = $data{'collector'}{uptime} = time - $time;
160 my $curr = int( $uptime / 60 );
161 if ( $curr > $prev )
162 {
163 $prev = $curr;
164  
165 $data{'collector'}{cfgtime} = time - ( stat $this->{conf} )[9];
166 my ( @t, @collector ) = qw( uptime cfgtime);
167 push my @coll, [ 'TASK', @t ];
168 push @coll, [ 'value', map{ $data{'collector'}{$_} }@t ];
169 push my @code, [ qw( TASK usetime last err ) ];
170 map
171 {
172 unless( $data{'code'}{$_} )
173 {
174 push @code, [ $_, '','','no info'];
175 }
176 else
177 {
178 my ( $usetime, $last, $err ) = @{$data{'code'}{$_}};
179 $err = "run timeout" if ! $err && $usetime > $interval{$_};
180 $err = "timeout" if ! $err && $last + $usetime + $timeout{$_} < time;
181 push @code, [ $_, $usetime, $last, $err ];
182 }
183 }sort keys %timeout;
184  
185 for( 1 .. @code -1 )
186 {
187 my ( $name, $error ) = ( $code[$_][0], $code[$_][3] );
188 next unless $error;
189 print "code stat error $name, restart!!!\n";
190 eval{ $thr{$name}->kill('KILL'); } ;##if $curr % 2;
191 print "kill thread err:$@\n" if $@;
192 }
193  
194 for my $t ( threads->list( threads::joinable ) )
195 {
196  
197 my $tid = $t->tid();
198 next unless my $name = delete $tid{$tid};
199 $t->join();
200 $thr{$name} = threads->create(
201 'task', ( $name, $conf->{$name}, $this, $config, $queue )
202 );
203  
204 $tid{$thr{$name}->tid()} = $name;
205 }
206  
207 push @collector, \@coll, \@code;
208 MYDan::Collector::Sock::Ring::push( $data{data}{collector} = \@collector );
209 eval{
210 YAML::XS::DumpFile "$this->{data}/.output", $data{data};
211 $push->push( $data{data} ) if $push;
212 $MYDan::Collector::Sock::Data::DATA = YAML::XS::Dump $data{data};
213 };
214
215 MYDan::Collector::Util::system "mv '$this->{data}/.output' '$this->{data}/output'";
216 }
217 sleep 3;
218 }
219 }
220  
221 1;