lib/App/MadEye/Plugin/Worker/Gearman.pm untested

filter: Statementstmt 0.0% 0 / 113 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 7 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 0.0% 0 / 23 Podpod 0.0% 0 / 10 totaltotal 0.0% 0 / 171 CC 8 SCAR 17.8
CC
8
Cov
0%
_run_workers
20.0
run_job
12.0
kill_workers
6.0
CRAP 5.9
1 package App::MadEye::Plugin::Worker::Gearman;
2 use strict;
3 use warnings;
4 use base qw/App::MadEye::Plugin::Base/;
5 use Gearman::Worker;
6 use Gearman::Client;
7 use App::MadEye::Util;
8 use Params::Validate;
9 use English;
10 use App::MadEye::Util;
11 use POSIX ":sys_wait_h";
12 use Storable qw/freeze thaw/;
13 use YAML;
14 use Scalar::Util qw/weaken/;
15  
16 __PACKAGE__->mk_accessors(qw/task_set child_pids gearman_client/);
17  
18 our $CHILD_TIMEOUT = 60; # TODO: configurable
19  
20 sub new {
21 my $class = shift;
22 my $self = $class->SUPER::new(@_);
23  
24 my $gearman_client = $self->get_gearman_client;
25 $self->gearman_client( $gearman_client );
26 my $task_set = $gearman_client->new_task_set;
27 $self->task_set( $task_set );
28  
29 $self;
30 }
31  
32 sub before_run_jobs : Hook {
33 my ($self, $context) = @_;
34  
35 my @child_pids = $self->_run_workers($context);
36 $self->child_pids(\@child_pids);
37  
38 $SIG{__DIE__} = sub {
39 $self->kill_workers();
40 };
41 }
42  
43 sub _run_workers {
44 my ($self, $context) = @_;
45  
46 my $parent_pid = $PID;
47 my @child_pids;
48 for my $i ( 0 .. $self->config->{config}->{fork_num}- 1 ) {
49 my $pid = fork();
50 if ($pid) {
51 # parent process
52 push @child_pids, $pid;
53 } elsif ( defined $pid ) {
54 # child process
55 $context->log('debug', "start worker $i($parent_pid)");
56 $self->run_worker($context, $parent_pid);
57 } else {
58 die "Cannot fork: $!";
59 }
60 }
61 return wantarray ? @child_pids : \@child_pids;
62 }
63  
64 sub run_job :Method {
65 my ($self, $context, $args) = @_;
66  
67 $self->task_set->add_task(
68 'watch',
69 freeze($args), +{
70 timeout => $self->task_timeout(),
71 on_fail => sub {
72 warn "GEARMAN ERROR: " . Dump($args);
73 },
74 on_complete => sub {
75 my $args = thaw( ${ $_[0] } );
76  
77 if ( ref $args eq 'HASH' ) {
78 # this server was dead.
79 $context->add_result(
80 plugin => $args->{plugin},
81 target => $args->{target},
82 message => $args->{message},
83 );
84 }
85 elsif ( ref $args eq 'SCALAR' && not defined $$args ) {
86 # success case
87 }
88 else {
89 die "invalid value: " . Dump($args);
90 }
91 },
92 }
93 );
94 }
95  
96 sub after_run_jobs : Hook {
97 my ($self, $context, $args) = @_;
98  
99 $context->log(debug => 'wait children!');
100 $self->task_set->wait;
101  
102 $context->log(debug => 'kill children!');
103 $self->kill_workers();
104  
105 # DESTROYYYYYYYYY
106 delete $self->{task_set};
107 delete $self->{gearman_client};
108 }
109  
110 sub kill_workers {
111 my ( $self, ) = @_;
112  
113 my $INT = 2;
114 my $killed = kill $INT, @{ $self->child_pids };
115 if ($killed != scalar @{ $self->child_pids }) {
116 die "Cannot kill the child process. $killed";
117 }
118 }
119  
120 sub get_gearman_client {
121 my $self = shift;
122  
123 my $client = Gearman::Client->new;
124 $client->job_servers( @{ $self->config->{config}->{gearman_servers} } );
125 $client->prefix($PID);
126 $client;
127 }
128  
129 sub run_worker {
130 my ($self, $context, $parent_pid) = @_;
131  
132 my $worker = Gearman::Worker->new;
133 $worker->job_servers( @{ $self->config->{config}->{gearman_servers} } );
134 $worker->prefix($parent_pid);
135 $worker->register_function(
136 'watch',
137 sub {
138 my $args = thaw( $_[0]->arg );
139  
140 $context->log( debug => "watching $args->{target} by $args->{plugin}" );
141 my $result = \undef;
142 timeout $self->task_timeout(), "watching $args->{target} $args->{plugin}", sub {
143 if ( my $message = $args->{plugin}->is_dead( $args->{target} ) ) {
144 $result = +{
145 message => $message,
146 plugin => $args->{plugin},
147 target => $args->{target},
148 };
149 }
150 };
151 return freeze($result);
152 }
153 );
154  
155 timeout $self->child_timeout(), 'work child', sub {
156 $worker->work while 1;
157 };
158 }
159  
160 sub task_timeout {
161 my $self = shift;
162 $self->config->{config}->{task_timeout} || 10;
163 }
164  
165 sub child_timeout {
166 my $self = shift;
167 $self->config->{config}->{child_timeout} || 60;
168 }
169  
170 1;
171 __END__