Objects.pm

filter: Statementstmt 95.5% 107 / 112 Branchbran 82.0% 41 / 50 Conditioncond 70.0% 7 / 10 MC/DCmcdc 50.0% 3 / 6 Subroutinesub 96.0% 24 / 25 Podpod 80.0% 8 / 10 totaltotal 89.2% 190 / 213 CC 32 SCAR 15.7
CC
32
Cov
90%
socket
11.6
canonicalize_job_servers
8.0
sock_nodelay
6.0
CRAP 4.8
« ResponseParser.pm ↑ Summary Task.pm »
1 package Gearman::Objects;
2 18 ▶use version ();
Subroutine BEGIN: called CC=1 SCAR=0.0
3 18 $Gearman::Objects::VERSION = version->declare("2.004.015");
4  
5 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 =head1 NAME
9  
10 Gearman::Objects - a parent class for L<Gearman::Client> and L<Gearman::Worker>
11  
12 =head1 METHODS
13  
14 =cut
15  
16 18 ▶use constant DEFAULT_PORT => 4730;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 18 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
19 18 ▶use IO::Socket::IP ();
Subroutine BEGIN: called CC=1 SCAR=0.0
20 18 ▶use IO::Socket::SSL ();
Subroutine BEGIN: called CC=1 SCAR=0.0
21 18 ▶use Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 ▶use List::MoreUtils qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
23 first_index
24 18 /;
25  
26 18 ▶use fields qw/
Subroutine BEGIN: called CC=1 SCAR=0.0
27 debug
28 job_servers
29 js_count
30 prefix
31 prefix_separator
32 sock_cache
33 18 /;
34  
35 26 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
36 26 my $self = shift;
37 26 my (%opts) = @_;
38 26 ▶ unless (ref $self) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $self) 12 14 26
39 12 $self = fields::new($self);
40 }
41 26 $self->{job_servers} = [];
42 26 $self->{js_count} = 0;
43  
44 $opts{job_servers}
45 26 ▶ && $self->set_job_servers($opts{job_servers});
Branch: 1 branch
BranchTrueFalseTotal
$opts{"job_servers"} and $self->set_job_servers($opts{"job_servers"}) 7 19 26
46  
47 26 $self->debug($opts{debug});
48 26 $self->prefix($opts{prefix});
49 26 $self->prefix_separator($opts{prefix_separator});
50  
51 26 $self->{sock_cache} = {};
52  
53 26 return $self;
54 } ## end sub new
55  
56 =head2 job_servers([$job_servers])
57  
58 Initialize the list of job servers.
59 C<$job_servers>should be array or array reference of hash references or stringified job servers.
60 If the port number is not provided, C<4730> is used as the default.
61 For example:
62  
63 C<< $client->job_servers('127.0.0.1', { host => "192.168.1.100", port => 4730 }); >>
64  
65  
66 B<return> C<[job_servers]>
67  
68 =cut
69  
70 11 ▶sub job_servers {
Subroutine job_servers: called CC=3 SCAR=11.0
Pod documented
71 11 my ($self) = shift;
72 11 ▶ (@_) && $self->set_job_servers(@_);
Branch: 1 branch
BranchTrueFalseTotal
@_ and $self->set_job_servers(@_) 2 9 11
73  
74 10 ▶ return wantarray ? @{ $self->{job_servers} } : $self->{job_servers};
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 9 1 10
75 } ## end sub job_servers
76  
77 =head2 set_job_servers($js)
78  
79 set job_servers attribute by canonicalized C<$js>
80  
81 =cut
82  
83 9 ▶sub set_job_servers {
Subroutine set_job_servers: called CC=1 SCAR=0.0
Pod documented
84 9 my $self = shift;
85 9 my $list = $self->canonicalize_job_servers(@_);
86  
87 8 $self->{js_count} = scalar @{$list};
88 8 return $self->{job_servers} = $list;
89 } ## end sub set_job_servers
90  
91 =head2 canonicalize_job_servers($js)
92  
93 C<$js> a string, hash reference or array reference of aforementioned.
94  
95 Hash reference should contain at least host key.
96  
97 All keys: host, port (4730 on default), use_ssl, ca_file, cert_file, key_file,
98 socket_cb
99  
100 B<return> [canonicalized list]
101  
102 =cut
103  
104 14 ▶sub canonicalize_job_servers {
Subroutine canonicalize_job_servers: called CC=8 SCAR=20.8
Pod documented
105 14 my ($self) = shift;
106  
107 14 my $ref = ref($_[0]);
108 14 ▶ my @in = $ref && $ref eq "ARRAY" ? @{ $_[0] } : @_;
Branch: 1 branch
BranchTrueFalseTotal
$ref && $ref eq 'ARRAY' ? : 5 9 14
Condition100%3/3
$ref and $ref eq "ARRAY"
!ll&&!rl&&r
455
MC/DC100%2/2
$ref and $ref eq "ARRAY"
$ref $ref eq "ARRAY"
Truth table100%3/3
A and B
A= $refB= $ref eq "ARRAY"
ABresult
0X0
100
111
109  
110 14 my $out = [];
111 14 foreach my $i (@in) {
112 12 my $ref = ref($i);
113 12 ▶ if ($ref) {
Branch: 2 branches
BranchTrueFalseTotal
if ($ref) { } 6 6 12
elsif (not $i =~ /:/) { } 2 4 6
114 6 ▶ $ref eq "HASH" || Carp::croak "unsupported job server value of type ", ref($i);
Branch: 1 branch
BranchTrueFalseTotal
$ref eq "HASH" or Carp::croak("unsupported job server value of type ", ref $i) 5 1 6
115 5 ▶ $i->{port} ||= Gearman::Objects::DEFAULT_PORT;
Condition50%1/2
$i->{"port"} ||= 4730
l!l
50
MC/DC0%0/1
$i->{"port"} ||= 4730
$i->{"port"}
Truth table50%1/2
A
A= $i->{"port"}
Aresult
11
00
116 }
117 elsif ($i !~ /:/) {
118 2 $i .= ':' . Gearman::Objects::DEFAULT_PORT;
119 }
120 11 push @{$out}, $i;
121 } ## end foreach my $i (@in)
122 13 return $out;
123 } ## end sub canonicalize_job_servers
124  
125 34 ▶sub debug {
Subroutine debug: called CC=1 SCAR=0.0
Pod undocumented
126 34 return shift->_property("debug", @_);
127 }
128  
129 =head2 func($func)
130  
131 B<return> C<< join $prefix_separator, $prefix, $func >>
132  
133 =cut
134  
135 11 ▶sub func {
Subroutine func: called CC=2 SCAR=6.9
Pod documented
136 11 my ($self, $func) = @_;
137 11 my $prefix = $self->prefix;
138 return
139 11 ▶ defined($prefix)
Branch: 1 branch
BranchTrueFalseTotal
defined $prefix ? : 4 7 11
140 ? join($self->prefix_separator, $prefix, $func)
141 : $func;
142 } ## end sub func
143  
144 =head2 prefix([$prefix])
145  
146 get/set the namespace / prefix for the function names.
147  
148 =cut
149  
150 47 ▶sub prefix {
Subroutine prefix: called CC=1 SCAR=0.0
Pod documented
151 47 return shift->_property("prefix", @_);
152 }
153  
154 =head2 prefix_separator([$separator])
155  
156 getter/setter
157  
158 default: "\t"
159  
160 I<If gearmand uses memcached persistent queue type, override default separator to insure jobs recovery>
161  
162 =cut
163  
164 32 ▶sub prefix_separator {
Subroutine prefix_separator: called CC=3 SCAR=11.0
Pod documented
165 32 my ($self) = shift;
166 32 ▶ my $r = $self->_property("prefix_separator", scalar(@_) ? $_[0] : ());
Branch: 1 branch
BranchTrueFalseTotal
scalar @_ ? : 27 5 32
167 32 ▶ return $r ? $r : $self->_property("prefix_separator", "\t");
Branch: 1 branch
BranchTrueFalseTotal
$r ? : 6 26 32
168 }
169  
170 =head2 socket($js, [$timeout])
171  
172 depends on C<use_ssl>
173 prepare L<IO::Socket::IP>
174 or L<IO::Socket::SSL>
175  
176 =over
177  
178 =item
179  
180 C<$host_port> peer address
181  
182 =item
183  
184 C<$timeout> default: 1
185  
186 =back
187  
188 B<return> depends on C<use_ssl> IO::Socket::(IP|SSL) on success
189  
190 =cut
191  
192 3 ▶sub socket {
Subroutine socket: called CC=10 SCAR=24.5
Pod documented
193 3 my ($self, $js, $t) = @_;
194 3 ▶ unless (ref($js)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $js) 0 3 3
195 0 my ($h, $p) = ($js =~ /^(.*):(\d+)$/);
196 0 $js = { host => $h, port => $p };
197 }
198  
199 my %opts = (
200 PeerPort => $js->{port},
201 PeerHost => $js->{host},
202 3 ▶ Timeout => $t || 1
Condition100%2/2
$t || 1
l!l
12
MC/DC100%1/1
$t || 1
$t
Truth table100%2/2
A
A= $t
Aresult
11
00
203 );
204  
205 3 my $sc = "IO::Socket::IP";
206 3 ▶ if ($js->{use_ssl}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($js->{"use_ssl"}) 1 2 3
207 1 $sc = "IO::Socket::SSL";
208 1 for (qw/ ca_file cert_file key_file /) {
209 3 ▶ $js->{$_} || next;
Branch: 1 branch
BranchTrueFalseTotal
$js->{$_} or next 0 3 3
210 0 $opts{ join('_', "SSL", $_) } = $js->{$_};
211 }
212 } ## end if ($js->{use_ssl})
213  
214 3 ▶ $js->{socket_cb} && $js->{socket_cb}->(\%opts);
Branch: 1 branch
BranchTrueFalseTotal
$js->{"socket_cb"} and $js->{"socket_cb"}->(\%opts) 2 1 3
215  
216 3 my $s = $sc->new(%opts);
217 3 ▶ unless ($s) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($s) 1 2 3
218 $self->debug() && Carp::carp("connection failed error='$@'",
219 $js->{use_ssl}
220 1 ▶ ? ", ssl_error='$IO::Socket::SSL::SSL_ERROR'"
Branch: 2 branches
BranchTrueFalseTotal
$self->debug and Carp::carp("connection failed error='$@'", $js->{"use_ssl"} ? ", ssl_error='$IO::Socket::SSL::SSL_ERROR'" : "") 0 1 1
$js->{'use_ssl'} ? : 0 0 0
221 : "");
222 } ## end unless ($s)
223  
224 3 return $s;
225 } ## end sub socket
226  
227 =head2 sock_nodelay($sock)
228  
229 set TCP_NODELAY on $sock, die on failure
230  
231 =cut
232  
233 0 ▶sub sock_nodelay {
Subroutine sock_nodelay: not called CC=2 SCAR=17.9
Pod documented
234 0 my ($self, $sock) = @_;
235 0 ▶ setsockopt($sock, Socket::IPPROTO_TCP, Socket::TCP_NODELAY, pack("l", 1))
Branch: 1 branch
BranchTrueFalseTotal
setsockopt $sock, 6, 1, "\1\0\0\0" or Carp::croak("setsockopt: $!") 0 0 0
236 or Carp::croak "setsockopt: $!";
237 }
238  
239 # _sock_cache($js, [$sock, $delete])
240 #
241 # B<return> $sock || undef
242 #
243  
244 5 ▶sub _sock_cache {
Subroutine _sock_cache: called CC=3 SCAR=11.0
245 5 my ($self, $js, $sock, $delete) = @_;
246 5 my $hp = $self->_js_str($js);
247 5 ▶ if ($sock) {
Branch: 1 branch
BranchTrueFalseTotal
if ($sock) 3 2 5
248 3 $self->{sock_cache}->{$hp} = $sock;
249 }
250  
251 return $delete
252 ? delete($self->{sock_cache}->{$hp})
253 5 ▶ : $self->{sock_cache}->{$hp};
Branch: 1 branch
BranchTrueFalseTotal
$delete ? : 1 4 5
254 } ## end sub _sock_cache
255  
256 #
257 # _property($name, [$value])
258 # set/get
259 139 ▶sub _property {
Subroutine _property: called CC=3 SCAR=11.0
260 139 my $self = shift;
261 139 my $name = shift;
262 139 ▶ $name || return;
Branch: 1 branch
BranchTrueFalseTotal
$name or return 139 0 139
263 139 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 110 29 139
264 110 $self->{$name} = shift;
265 }
266  
267 139 return $self->{$name};
268 } ## end sub _property
269  
270 #
271 #_js_str($js)
272 #
273 # return host:port
274 10 ▶sub _js_str {
Subroutine _js_str: called CC=2 SCAR=6.9
275 10 my ($self, $js) = @_;
276 10 ▶ return ref($js) eq "HASH" ? join(':', @{$js}{qw/host port/}) : $js;
Branch: 1 branch
BranchTrueFalseTotal
ref $js eq 'HASH' ? : 6 4 10
277 }
278  
279 #
280 # _js($js_str)
281 #
282 # return job_servers item || undef
283 #
284 1 ▶sub _js {
Subroutine _js: called CC=3 SCAR=11.8
285 1 my ($self, $js_str) = @_;
286 1 my @s = $self->job_servers();
287 1 ▶ my $i = first_index { $js_str eq $self->_js_str($_) } @s;
Subroutine __ANON__: called CC=1 SCAR=0.0
288 1 ▶ return ($i == -1 || $i > $#s) ? undef : $s[$i];
Branch: 1 branch
BranchTrueFalseTotal
$i == -1 || $i > $#s ? : 0 1 1
Condition33%1/3
$i == -1 or $i > $#s
l!l&&r!l&&!r
001
MC/DC0%0/2
$i == -1 or $i > $#s
$i == -1 $i > $#s
Truth table33%1/3
A or B
A= $i == -1B= $i > $#s
ABresult
1X1
011
000
289 } ## end sub _js
290  
291 18 1;
« ResponseParser.pm ↑ Summary Task.pm »