blib/lib/MYDan/Agent/Client/Proxy.pm untested

filter: Statementstmt 0.0% 0 / 100 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 8 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 18 Podpod 0.0% 0 / 2 totaltotal 0.0% 0 / 140 CC 7 SCAR 28.8
CC
7
Cov
0%
run
56.0
new
2.0
BEGIN
2.0
CRAP 17.8
1 package MYDan::Agent::Client::Proxy;
2  
3 =head1 NAME
4  
5 MYDan::Agent::Client
6  
7 =head1 SYNOPSIS
8  
9 use MYDan::Agent::Client;
10 my $client = MYDan::Agent::Client->new( [ 'node1', 'node2' ] );
11 my %result = $client->run( timeout => 300, input => '' );
12  
13 =cut
14 use strict;
15 use warnings;
16  
17 use Carp;
18 use YAML::XS;
19 use File::Spec;
20 use File::Basename;
21 use FindBin qw( $RealBin );
22 use YAML::XS;
23  
24 use MYDan::Agent::Query;
25 use AnyEvent;
26 use AnyEvent::Handle;
27 use AnyEvent::Socket;
28 use Time::HiRes qw(time);
29  
30 use MYDan::API::Agent;
31 use MYDan::Util::Percent;
32 use MYDan::Agent::Proxy;
33  
34 our %RUN = ( user => 'root', max => 128, timeout => 300 );
35  
36 sub new
37 {
38 my $class = shift;
39 bless +{ node => \@_ }, ref $class || $class;
40 }
41  
42  
43 sub run
44 {
45 my ( $this, %run, %result ) = ( shift, %RUN, @_ );
46  
47 return unless my @node = @{$this->{node}};
48  
49 my $query = $run{query};
50  
51 my $cv = AE::cv;
52 my ( @work, $stop );
53  
54 $SIG{TERM} = $SIG{INT} = my $tocb = sub
55 {
56 $stop = 1;
57 for my $w ( @work )
58 {
59 next unless $$w && $$w->fh;
60 $$w->destroy;
61 $cv->end;
62 }
63  
64 map{ $cv->end; } 1 .. $cv->{_ae_counter}||0;
65 };
66  
67 my $percent = MYDan::Util::Percent->new( scalar @node, 'run ..' );
68 my $work;$work = sub{
69 return unless my $node = shift @node;
70 $result{$node} = '';
71
72 $cv->begin;
73  
74 tcp_connect $node, $run{port}, sub {
75 my ( $fh ) = @_;
76 unless( $fh ){
77 $cv->end;
78 $result{$node} = "tcp_connect: $!";
79 $work->();
80 return;
81 }
82 if( $stop )
83 {
84 close $fh;
85 return;
86 }
87  
88 my $hdl;
89 push @work, \$hdl;
90 $hdl = new AnyEvent::Handle(
91 fh => $fh,
92 on_read => sub {
93 my $self = shift;
94 $self->unshift_read (
95 chunk => length $self->{rbuf},
96 sub { $result{$node} .= $_[1];}
97 );
98 },
99 on_eof => sub{
100 undef $hdl;
101 $cv->end;
102 $work->();
103 }
104 );
105 $hdl->push_write($query);
106 $hdl->push_shutdown;
107 };
108 };
109  
110 my $max = scalar @node > $run{max} ? $run{max} : scalar @node;
111 $work->() for 1 .. $max ;
112  
113  
114 my $w = AnyEvent->timer ( after => $run{timeout}, cb => $tocb );
115 $cv->recv;
116 undef $w;
117  
118 map{ $_ =~ s/^\**#\*MYDan_\d+\*#//;}values %result;
119 return %result;
120 }
121  
122 1;