Client.pm

filter: Statementstmt 29.3% 32 / 109 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 41.6% 10 / 24 Podpod 55.5% 5 / 9 totaltotal 26.4% 47 / 178 CC 20 SCAR 23.3
CC
20
Cov
23%
set_job_servers
30.0
__ANON__
20.0
spawn
12.0
CRAP 10.2
« Client/Connection.pm ↑ Summary
1 # ===========================================================================
2 # POE::Component::Gearman::Client
3 #
4 # POE-based client for Gearman servers
5 #
6 # Author: Alessandro Ranellucci <aar@cpan.org>
7 #
8 # See below for documentation.
9 #
10  
11 package POE::Component::Gearman::Client;
12  
13 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 2 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 2 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
17 2 ▶use fields (
Subroutine BEGIN: called CC=1 SCAR=0.0
18 2 'job_servers', # arrayref of POE::Component::Gearman::Client::Connection objects
19 't_no_random', # don't randomize job server to use: use first alive one.
20 't_offline_host', # hashref: hostname -> $bool, if host should act as offline, for testing
21 2 );
22 2 ▶use Gearman::Objects;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 2 ▶use Gearman::Task;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 2 ▶use Gearman::JobStatus;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 2 ▶use List::Util qw(first);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 2 ▶use POE::Component::Gearman::Client::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 2 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 2 $VERSION = '0.03';
30  
31 sub DEBUGGING () { 0 }
32  
33 0 ▶sub spawn {
Subroutine spawn: not called CC=3 SCAR=24.8
Pod documented
34 0 my ($class, %opts) = @_;
35 0 my $self = $class;
36 0 ▶ $self = fields::new($class) unless ref $self;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $self 0 0 0
37
38 0 $self->{job_servers} = [];
39 0 $self->{t_offline_host} = {};
40  
41 0 my $js = delete $opts{job_servers};
42 0 my $alias = delete $opts{alias};
43 0 ▶ croak "Unknown parameters: " . join(", ", keys %opts) if %opts;
Branch: 1 branch
BranchTrueFalseTotal
if %opts 0 0 0
44
45 # register session with POE
46 POE::Session->create(
47 inline_states => {
48 0 ▶ _start => sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
49 0 ▶ $_[KERNEL]->alias_set( $alias || 'Gearman' );
Condition0%0/2
$alias || 'Gearman'
l!l
00
MC/DC0%0/1
$alias || 'Gearman'
$alias
Truth table0%0/2
A
A= $alias
Aresult
11
00
50
51 # call instead of yield so that the job_servers method is
52 # instantly available
53 0 ▶ $_[KERNEL]->call($_[SESSION], 'set_job_servers', $js) if $js;
Branch: 1 branch
BranchTrueFalseTotal
if $js 0 0 0
54 },
55 },
56 0 object_states => [
57 $self => [qw(t_set_disable_random t_set_offline_host
58 set_job_servers add_task disconnect_all)]
59 ]
60 );
61
62 0 return $self;
63 }
64  
65 # for testing.
66 0 ▶sub t_set_disable_random {
Subroutine t_set_disable_random: not called CC=1 SCAR=6.9
Pod undocumented
67 0 my $self = $_[OBJECT];
68 0 $self->{t_no_random} = shift;
69 }
70  
71 0 ▶sub t_set_offline_host {
Subroutine t_set_offline_host: not called CC=3 SCAR=24.8
Pod undocumented
72 0 my ($self, $host, $val) = @_[OBJECT, ARG0, ARG1];
73 0 ▶ $val = 1 unless defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 0 0
74 0 $self->{t_offline_host}{$host} = $val;
75  
76 0 ▶ my $conn = first { $_->hostspec eq $host } @{ $self->{job_servers} }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
my $conn = &first(sub { $_->hostspec eq $host; } , @{$$self{"job_servers"};}) or die "No host found with that spec to mark offline" 0 0 0
77 or die "No host found with that spec to mark offline";
78  
79 0 $conn->t_set_offline($val);
80 }
81  
82 # set job servers, without shutting down dups, and shutting down old ones gracefully
83 0 ▶sub set_job_servers {
Subroutine set_job_servers: not called CC=5 SCAR=34.0
Pod documented
84 0 my ($self, $js) = @_[OBJECT, ARG0];
85  
86 0 my %being_set; # hostspec -> 1
87 0 %being_set = map { $_, 1 } @$js;
88  
89 0 my %exist; # hostspec -> existing conn
90 0 foreach my $econn (@{ $self->{job_servers} }) {
91 0 my $spec = $econn->hostspec;
92 0 ▶ if ($being_set{$spec}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($being_set{$spec}) { } 0 0 0
93 0 $exist{$spec} = $econn;
94 } else {
95 0 $econn->close_when_finished;
96 }
97 }
98  
99 0 my @newlist;
100 0 foreach (@$js) {
101 0 ▶ push @newlist, $exist{$_} || POE::Component::Gearman::Client::Connection->new( hostspec => $_ );
Condition0%0/3
$exist{$_} || 'POE::Component::Gearman::Client::Connection'->new('hostspec', $_)
l!l&&r!l&&!r
000
MC/DC0%0/2
$exist{$_} || 'POE::Component::Gearman::Client::Connection'->new('hostspec', $_)
$exist{$_} 'POE::Component::Gearman::Client::Connection'->new('hostspec', $_)
Truth table0%0/3
A || B
A= $exist{$_}B= 'POE::Component::Gearman::Client::Connection'->new('hostspec', $_)
ABresult
1X1
011
000
102 }
103 0 $self->{job_servers} = \@newlist;
104 }
105  
106 # getter
107 0 ▶sub job_servers {
Subroutine job_servers: not called CC=3 SCAR=24.8
Pod documented
108 0 my $self = shift;
109 0 ▶ croak "Not a setter" if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 0 0 0
110 0 my @list = map { $_->hostspec } @{ $self->{job_servers} };
111 0 ▶ return wantarray ? @list : \@list;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
112 }
113  
114 0 ▶sub add_task {
Subroutine add_task: not called CC=2 SCAR=17.9
Pod documented
115 0 my $self = $_[OBJECT];
116 0 ▶ my Gearman::Task $task = $_[ARG0] or return;
Branch: 1 branch
BranchTrueFalseTotal
my Gearman::Task $task = $_[10] or return 0 0 0
117  
118 0 my $try_again;
119 0 ▶ $try_again = sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
120  
121 0 my @job_servers = grep { $_->alive } @{$self->{job_servers}};
122 0 warn "Alive servers: " . @job_servers . " out of " . @{$self->{job_servers}} . "\n" if DEBUGGING;
123 0 ▶ unless (@job_servers) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@job_servers) 0 0 0
124 0 $task->final_fail;
125 0 $try_again = undef;
126 0 return;
127 }
128  
129 0 my $js;
130 0 ▶ if (defined( my $hash = $task->hash )) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $hash = $task->hash)) { } 0 0 0
131 # Task is hashed, use key to fetch job server
132 0 $js = @job_servers[$hash % @job_servers];
133 }
134 else {
135 # Task is not hashed, random job server
136 0 ▶ $js = @job_servers[$self->{t_no_random} ? 0 :
Branch: 1 branch
BranchTrueFalseTotal
$self->{'t_no_random'} ? : 0 0 0
137 int( rand( @job_servers ))];
138 }
139  
140 # TODO Fix this violation of object privacy.
141 0 $task->{taskset} = $self;
142  
143 $js->get_in_ready_state(
144 # on_ready:
145 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
146 0 my $timer;
147 0 ▶ if (my $timeout = $task->{timeout}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timeout = $task->{"timeout"}) 0 0 0
148 # TODO: setup timer
149 #$timer = Danga::Socket->AddTimer($timeout, sub {
150 # $task->final_fail('timeout');
151 #});
152 }
153 0 ▶ $task->set_on_post_hooks(sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
154 0 ▶ $timer->cancel if $timer;
Branch: 1 branch
BranchTrueFalseTotal
if $timer 0 0 0
155  
156 # ALSO clean up our $js (connection's) waiting stuff:
157 0 $js->give_up_on($task);
158 0 });
159 0 $js->add_task( $task );
160 0 $try_again = undef;
161 },
162 # on_error:
163 0 $try_again,
164 );
165 0 };
166 0 $try_again->();
167 }
168  
169 0 ▶sub disconnect_all {
Subroutine disconnect_all: not called CC=2 SCAR=17.9
Pod documented
170 0 my $self = $_[OBJECT];
171 0 warn "Disconnecting all server sockets\n" if DEBUGGING;
172 0 my @job_servers = grep { $_->alive } @{$self->{job_servers}};
173 0 warn "Alive servers: " . @job_servers . " out of " . @{$self->{job_servers}} . "\n" if DEBUGGING;
174
175 # TODO: we should better use close_when_finished
176 0 $_->close for @job_servers;
177 }
178  
179 # POE::Component::Gearman::Client sometimes fakes itself duck-typing style as a
180 # Gearman::Taskset, since a task"set" makes no sense in an async
181 # world, where there's no need to wait on a set of things... since
182 # everything happens at its own pace. so for duck-typing reasons (or,
183 # er, "implementing an interface", say), we need to implement a the
184 # "taskset client method" but in our case, that's just us.
185 0 ▶sub client { $_[0] }
Subroutine client: not called CC=1 SCAR=6.9
Pod undocumented
186  
187 # as a Gearman::Client-like thing, we'll be asked for our prefix, which this module
188 # currently doesn't support, but the base Gearman libraries expect.
189 0 ▶sub prefix { "" }
Subroutine prefix: not called CC=1 SCAR=6.9
Pod undocumented
190  
191  
192 2 1;
193 __END__
« Client/Connection.pm ↑ Summary