| 1 | package Gearman::Util; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 20 | ▶ | use version (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 20 | $Gearman::Util::VERSION = version->declare("2.004.015"); | |||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 20 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 20 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | # for sake of _read_sock | ||||||||||||||||||||||||||||||||||||||||||
| 9 | 20 | ▶ | no warnings "recursion"; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||||||
| 11 | # man errno | ||||||||||||||||||||||||||||||||||||||||||
| 12 | # Resource temporarily unavailable | ||||||||||||||||||||||||||||||||||||||||||
| 13 | # (may be the same value as EWOULDBLOCK) (POSIX.1) | ||||||||||||||||||||||||||||||||||||||||||
| 14 | 20 | ▶ | use IO::Select; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 20 | ▶ | use POSIX qw(:errno_h); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 20 | ▶ | use Scalar::Util qw(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 20 | ▶ | use Time::HiRes qw(); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | Gearman::Util - Utility functions for gearman distributed job system | ||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||
| 23 | =head1 METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||
| 25 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||
| 27 | sub DEBUG () {0} | ||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | # I: to jobserver | ||||||||||||||||||||||||||||||||||||||||||
| 30 | # O: out of job server | ||||||||||||||||||||||||||||||||||||||||||
| 31 | # W: worker | ||||||||||||||||||||||||||||||||||||||||||
| 32 | # C: client of job server | ||||||||||||||||||||||||||||||||||||||||||
| 33 | # J: jobserver | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 20 | our %cmd = ( | |||||||||||||||||||||||||||||||||||||||||
| 35 | 1 => ['I', "can_do"], # from W: [FUNC] | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 2 => ['I', "cant_do"], # from W: [FUNC] | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 3 => ['I', "reset_abilities"], # from W: --- | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 4 => ['I', "pre_sleep"], # from W: --- | ||||||||||||||||||||||||||||||||||||||||||
| 39 | 6 => ['O', "noop"], # J->W --- | ||||||||||||||||||||||||||||||||||||||||||
| 40 | 7 => ['I', "submit_job"], # C->J FUNC[0]UNIQ[0]ARGS | ||||||||||||||||||||||||||||||||||||||||||
| 41 | 8 => ['O', "job_created"], # J->C HANDLE | ||||||||||||||||||||||||||||||||||||||||||
| 42 | 9 => ['I', "grab_job"], # W->J -- | ||||||||||||||||||||||||||||||||||||||||||
| 43 | 10 => ['O', "no_job"], # J->W -- | ||||||||||||||||||||||||||||||||||||||||||
| 44 | 11 => ['O', "job_assign"], # J->W HANDLE[0]FUNC[0]ARG | ||||||||||||||||||||||||||||||||||||||||||
| 45 | 12 => ['IO', "work_status"], # W->J/C: HANDLE[0]NUMERATOR[0]DENOMINATOR | ||||||||||||||||||||||||||||||||||||||||||
| 46 | 13 => ['IO', "work_complete"], # W->J/C: HANDLE[0]RES | ||||||||||||||||||||||||||||||||||||||||||
| 47 | 14 => ['IO', "work_fail"], # W->J/C: HANDLE | ||||||||||||||||||||||||||||||||||||||||||
| 48 | 15 => ['I', "get_status"], # C->J: HANDLE | ||||||||||||||||||||||||||||||||||||||||||
| 49 | 16 => ['I', "echo_req"], # ?->J TEXT | ||||||||||||||||||||||||||||||||||||||||||
| 50 | 17 => ['O', "echo_res"], # J->? TEXT | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 18 => ['I', "submit_job_bg"], # C->J " " " " " | ||||||||||||||||||||||||||||||||||||||||||
| 52 | 19 => ['O', "error"], # J->? ERRCODE[0]ERR_TEXT | ||||||||||||||||||||||||||||||||||||||||||
| 53 | 20 => ['O', "status_res"], # C->J: HANDLE[0]KNOWN[0]RUNNING[0]NUM[0]DENOM | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 21 => ['I', "submit_job_high"], # C->J FUNC[0]UNIQ[0]ARGS | ||||||||||||||||||||||||||||||||||||||||||
| 55 | 22 => ['I', "set_client_id"], # W->J: [RANDOM_STRING_NO_WHITESPACE] | ||||||||||||||||||||||||||||||||||||||||||
| 56 | 23 => ['I', "can_do_timeout"], # from W: FUNC[0]TIMEOUT | ||||||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||||||
| 58 | # for worker to declare to the jobserver that this worker is only connected | ||||||||||||||||||||||||||||||||||||||||||
| 59 | # to one jobserver, so no polls/grabs will take place, and server is free | ||||||||||||||||||||||||||||||||||||||||||
| 60 | # to push "job_assign" packets back down. | ||||||||||||||||||||||||||||||||||||||||||
| 61 | 24 => ['I', "all_yours"], # W->J --- | ||||||||||||||||||||||||||||||||||||||||||
| 62 | 25 => ['IO', "work_exception"], # W->J/C: HANDLE[0]EXCEPTION | ||||||||||||||||||||||||||||||||||||||||||
| 63 | 26 => ['I', "option_req"], # C->J: [OPT] | ||||||||||||||||||||||||||||||||||||||||||
| 64 | 27 => ['O', "option_res"], # J->C: [OPT] | ||||||||||||||||||||||||||||||||||||||||||
| 65 | 28 => ['IO', "work_data"], # W->J/C: HANDLE[0]RES | ||||||||||||||||||||||||||||||||||||||||||
| 66 | 29 => ['IO', "work_warning"], # W->J/C: HANDLE[0]RES | ||||||||||||||||||||||||||||||||||||||||||
| 67 | 32 => ['I', "submit_job_high_bg"], # C->J FUNC[0]UNIQ[0]ARGS | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 33 => ['I', "submit_job_low"], # C->J FUNC[0]UNIQ[0]ARGS | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 34 => ['I', "submit_job_low_bg"], # C->J FUNC[0]UNIQ[0]ARGS | ||||||||||||||||||||||||||||||||||||||||||
| 70 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 20 | our %num; # name -> num | |||||||||||||||||||||||||||||||||||||||||
| 73 | 20 | ▶ | while (my ($num, $ary) = each %cmd) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 74 | 620 | ▶ | die if $num{ $ary->[1] }; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 75 | 620 | $num{ $ary->[1] } = $num; | |||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | =head2 cmd_name($num) | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | B<return> cmd | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | 39 | ▶ | sub cmd_name { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
cmd_name: called CC=2 SCAR=7.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 39 | my $num = shift; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 39 | my $c = $cmd{$num}; | |||||||||||||||||||||||||||||||||||||||||
| 87 | 39 | ▶ | return $c ? $c->[1] : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | } | ||||||||||||||||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||||||||||||||||
| 90 | =head2 pack_req_command($key, $arg) | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | B<return> request string | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | 75 | ▶ | sub pack_req_command { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pack_req_command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 97 | 75 | return _pack_command("REQ", @_); | |||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||
| 100 | =head2 pack_res_command($cmd, $arg) | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | B<return> response string | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||||||||||||||||
| 106 | 64 | ▶ | sub pack_res_command { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
pack_res_command: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | 64 | return _pack_command("RES", @_); | |||||||||||||||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | =head2 read_res_packet($sock, $err_ref, $timeout) | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | B<return> undef on closed socket or malformed packet | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | sub read_res_packet { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_res_packet: called CC=20 SCAR=57.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 117 | 1 | warn " Entering read_res_packet" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||
| 118 | 1 | my $sock = shift; | |||||||||||||||||||||||||||||||||||||||||
| 119 | 1 | my $err_ref = shift; | |||||||||||||||||||||||||||||||||||||||||
| 120 | 1 | my $timeout = shift; | |||||||||||||||||||||||||||||||||||||||||
| 121 | 1 | my $time_start = Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||||||||
| 122 | 1 | ▶ | unless (Scalar::Util::blessed($sock)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 123 | # for the sake of Gearman::Client::Async | ||||||||||||||||||||||||||||||||||||||||||
| 124 | # see https://github.com/p-alik/perl-Gearman/issues/37 | ||||||||||||||||||||||||||||||||||||||||||
| 125 | 1 | ▶ | (ref($sock) eq "GLOB") || die "provided value is not a blessed object"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | ($$sock && $$sock eq '*Gearman::Worker::$sock') | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$$sock and $$sock eq "*Gearman::Worker::\$sock"
MC/DC0%0/2
$$sock and $$sock eq "*Gearman::Worker::\$sock"
$$sock
$$sock eq "*Gearman::Worker::\$sock"
Truth table0%0/3
A and B
A= $$sockB= $$sock eq "*Gearman::Worker::\$sock"
| |||||||||||||||||||||||||||||||||||||||||||
| 127 | || die | ||||||||||||||||||||||||||||||||||||||||||
| 128 | "provided value is not a GLOB of type Gearman::Worker::\$sock"; | ||||||||||||||||||||||||||||||||||||||||||
| 129 | } ## end unless (Scalar::Util::blessed...) | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | my $err = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||||||||||||||||
| 132 | 0 | my $code = shift; | |||||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | Scalar::Util::blessed($sock) && $sock->close() if $sock->connected; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/6
$sock->connected && (builtin::blessed($sock) && $sock->close)
builtin::blessed($sock) and $sock->close
MC/DC0%0/3
$sock->connected && (builtin::blessed($sock) && $sock->close)
$sock->connected
builtin::blessed($sock)
$sock->close
Truth table0%0/4
A && B and C
A= $sock->connectedB= builtin::blessed($sock)C= $sock->close
| |||||||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | $$err_ref = $code if ref $err_ref; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 135 | 0 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 136 | 0 | }; | |||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | 0 | $sock->blocking(0); | |||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | 0 | my $is = IO::Select->new($sock); | |||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | my $readlen = 12; | |||||||||||||||||||||||||||||||||||||||||
| 143 | 0 | my $offset = 0; | |||||||||||||||||||||||||||||||||||||||||
| 144 | 0 | my $buf = ''; | |||||||||||||||||||||||||||||||||||||||||
| 145 | 0 | my $using_ssl = $sock->isa("IO::Socket::SSL"); | |||||||||||||||||||||||||||||||||||||||||
| 146 | |||||||||||||||||||||||||||||||||||||||||||
| 147 | 0 | my ($magic, $type, $len); | |||||||||||||||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||||||||||||||||
| 149 | 0 | warn " Starting up event loop\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||
| 150 | 0 | while (1) { | |||||||||||||||||||||||||||||||||||||||||
| 151 | 0 | ▶ | if ($using_ssl && $sock->pending()) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$using_ssl and $sock->pending
MC/DC0%0/2
$using_ssl and $sock->pending
$using_ssl
$sock->pending
Truth table0%0/3
A and B
A= $using_sslB= $sock->pending
| |||||||||||||||||||||||||||||||||||||||||||
| 152 | 0 | warn " We have @{[ $sock->pending() ]} bytes...\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 155 | 0 | my $time_remaining = undef; | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | if (defined $timeout) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 157 | 0 | warn " We have a timeout of $timeout\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||
| 158 | 0 | $time_remaining = $time_start + $timeout - Time::HiRes::time(); | |||||||||||||||||||||||||||||||||||||||||
| 159 | 0 | ▶ | return $err->("timeout") if $time_remaining < 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | $is->can_read($time_remaining) || next; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 163 | } ## end else [ if ($using_ssl && $sock...)] | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | warn " Entering read loop\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | my ($ok, $err_code) = _read_sock($sock, \$buf, \$readlen, \$offset); | |||||||||||||||||||||||||||||||||||||||||
| 167 | 0 | ▶ | if (!defined($ok)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 169 | } | ||||||||||||||||||||||||||||||||||||||||||
| 170 | elsif ($ok == 0) { | ||||||||||||||||||||||||||||||||||||||||||
| 171 | 0 | return $err->($err_code); | |||||||||||||||||||||||||||||||||||||||||
| 172 | } | ||||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | if (!defined $type) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 175 | 0 | ▶ | next unless length($buf) >= 12; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | my $header = substr($buf, 0, 12, ''); | |||||||||||||||||||||||||||||||||||||||||
| 177 | 0 | ($magic, $type, $len) = unpack("a4NN", $header); | |||||||||||||||||||||||||||||||||||||||||
| 178 | 0 | ▶ | return $err->("malformed_magic: '$magic'") unless $magic eq "\0RES"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 179 | 0 | my $starting = length($buf); | |||||||||||||||||||||||||||||||||||||||||
| 180 | 0 | $readlen = $len - $starting; | |||||||||||||||||||||||||||||||||||||||||
| 181 | 0 | $offset = $starting; | |||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | 0 | ▶ | if ($readlen) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 184 | 0 | my ($ok, $err_code) | |||||||||||||||||||||||||||||||||||||||||
| 185 | = _read_sock($sock, \$buf, \$readlen, \$offset); | ||||||||||||||||||||||||||||||||||||||||||
| 186 | 0 | ▶ | if (!defined($ok)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 187 | 0 | next; | |||||||||||||||||||||||||||||||||||||||||
| 188 | } | ||||||||||||||||||||||||||||||||||||||||||
| 189 | elsif ($ok == 0) { | ||||||||||||||||||||||||||||||||||||||||||
| 190 | 0 | return $err->($err_code); | |||||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||||
| 192 | } ## end if ($readlen) | ||||||||||||||||||||||||||||||||||||||||||
| 193 | } ## end if (!defined $type) | ||||||||||||||||||||||||||||||||||||||||||
| 194 | |||||||||||||||||||||||||||||||||||||||||||
| 195 | 0 | $type = $cmd{$type}; | |||||||||||||||||||||||||||||||||||||||||
| 196 | 0 | ▶ | return $err->("bogus_command") unless $type; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | 0 | ▶ | return $err->("bogus_command_type") unless index($type->[0], "O") != -1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 198 | |||||||||||||||||||||||||||||||||||||||||||
| 199 | 0 | warn " Fully formed res packet, returning; type=$type->[1] len=$len\n" | |||||||||||||||||||||||||||||||||||||||||
| 200 | if DEBUG; | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | 0 | $sock->blocking(1); | |||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 205 | 0 | type => $type->[1], | |||||||||||||||||||||||||||||||||||||||||
| 206 | len => $len, | ||||||||||||||||||||||||||||||||||||||||||
| 207 | blobref => \$buf, | ||||||||||||||||||||||||||||||||||||||||||
| 208 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 209 | } ## end while (1) | ||||||||||||||||||||||||||||||||||||||||||
| 210 | } ## end sub read_res_packet | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | sub _read_sock { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_read_sock: not called CC=6 SCAR=37.4
| |||||||||||||||||||||||||||||||||||||||||||
| 213 | 0 | my ($sock, $buf_ref, $readlen_ref, $offset_ref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 214 | 0 | local $!; | |||||||||||||||||||||||||||||||||||||||||
| 215 | 0 | my $rv = sysread($sock, $$buf_ref, $$readlen_ref, $$offset_ref); | |||||||||||||||||||||||||||||||||||||||||
| 216 | 0 | ▶ | unless ($rv) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 217 | 0 | warn " Read error: $!\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | $! == EAGAIN && return; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | } | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | 0 | ▶ | return (0, "read_error") unless defined $rv; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 222 | 0 | ▶ | return (0, "eof") unless $rv; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||||||||
| 224 | 0 | ▶ | unless ($rv >= $$readlen_ref) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | warn | |||||||||||||||||||||||||||||||||||||||||
| 226 | " Partial read of $rv bytes, at offset $$offset_ref, readlen was $$readlen_ref\n" | ||||||||||||||||||||||||||||||||||||||||||
| 227 | if DEBUG; | ||||||||||||||||||||||||||||||||||||||||||
| 228 | 0 | $$offset_ref += $rv; | |||||||||||||||||||||||||||||||||||||||||
| 229 | 0 | $$readlen_ref -= $rv; | |||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | 0 | $sock->blocking(1); | |||||||||||||||||||||||||||||||||||||||||
| 232 | 0 | my $ret = _read_sock($sock, $buf_ref, $readlen_ref, $offset_ref); | |||||||||||||||||||||||||||||||||||||||||
| 233 | 0 | $sock->blocking(0); | |||||||||||||||||||||||||||||||||||||||||
| 234 | 0 | return $ret; | |||||||||||||||||||||||||||||||||||||||||
| 235 | } ## end unless ($rv >= $$readlen_ref) | ||||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | warn " Finished reading\n" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||
| 238 | 0 | return (1); | |||||||||||||||||||||||||||||||||||||||||
| 239 | } ## end sub _read_sock | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | =head2 read_text_status($sock, $err_ref) | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | 1 | ▶ | sub read_text_status { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
read_text_status: called CC=6 SCAR=28.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 246 | 1 | my $sock = shift; | |||||||||||||||||||||||||||||||||||||||||
| 247 | 1 | my $err_ref = shift; | |||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | 1 | ▶ | my $err = sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.4
| |||||||||||||||||||||||||||||||||||||||||||
| 250 | 1 | my $code = shift; | |||||||||||||||||||||||||||||||||||||||||
| 251 | 1 | ▶ | $sock->close() if $sock->connected; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 252 | 1 | ▶ | $$err_ref = $code if ref $err_ref; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 253 | 1 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 254 | 1 | }; | |||||||||||||||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||||||||||||||||
| 256 | 1 | ▶ | $sock->connected || return $err->("can't read from unconnected socket"); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 257 | 0 | my @lines; | |||||||||||||||||||||||||||||||||||||||||
| 258 | 0 | my $complete = 0; | |||||||||||||||||||||||||||||||||||||||||
| 259 | 0 | ▶ | while (my $line = <$sock>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 260 | 0 | chomp $line; | |||||||||||||||||||||||||||||||||||||||||
| 261 | 0 | ▶ | return $err->($1) if $line =~ /^ERR (\w+) /; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||||
| 263 | 0 | ▶ | if ($line eq '.') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 264 | 0 | $complete++; | |||||||||||||||||||||||||||||||||||||||||
| 265 | 0 | last; | |||||||||||||||||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||||
| 268 | 0 | push @lines, $line; | |||||||||||||||||||||||||||||||||||||||||
| 269 | } ## end while (my $line = <$sock>) | ||||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | ▶ | return $err->("eof") unless $complete; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | 0 | return @lines; | |||||||||||||||||||||||||||||||||||||||||
| 273 | } ## end sub read_text_status | ||||||||||||||||||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||||||||||||||||
| 275 | =head2 send_req($sock, $reqref) | ||||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 278 | |||||||||||||||||||||||||||||||||||||||||||
| 279 | 5 | ▶ | sub send_req { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
send_req: called CC=7 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 280 | 5 | my ($sock, $reqref) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 281 | 5 | ▶ | return 0 unless $sock; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||||||||||||||||
| 283 | 2 | my $data = ${$reqref}; | |||||||||||||||||||||||||||||||||||||||||
| 284 | 2 | (my $total_len) = (my $len) = length($data); | |||||||||||||||||||||||||||||||||||||||||
| 285 | 2 | my ($num_zero_writes, $offset) = (0, 0); | |||||||||||||||||||||||||||||||||||||||||
| 286 | 2 | local $SIG{PIPE} = "IGNORE"; | |||||||||||||||||||||||||||||||||||||||||
| 287 | |||||||||||||||||||||||||||||||||||||||||||
| 288 | 2 | ▶ | while ($len && ($num_zero_writes < 5)) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$len and $num_zero_writes < 5
MC/DC0%0/2
$len and $num_zero_writes < 5
$len
$num_zero_writes < 5
Truth table33%1/3
A and B
A= $lenB= $num_zero_writes < 5
| |||||||||||||||||||||||||||||||||||||||||||
| 289 | 2 | my $written = $sock->syswrite($data, $len, $offset); | |||||||||||||||||||||||||||||||||||||||||
| 290 | 1 | ▶ | if (!defined $written) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | 1 | warn "send_req: syswrite error: $!" if DEBUG; | |||||||||||||||||||||||||||||||||||||||||
| 292 | 1 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 293 | } | ||||||||||||||||||||||||||||||||||||||||||
| 294 | elsif ($written > 0) { | ||||||||||||||||||||||||||||||||||||||||||
| 295 | 0 | $len -= $written; | |||||||||||||||||||||||||||||||||||||||||
| 296 | 0 | $offset += $written; | |||||||||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||||||||
| 298 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 299 | 0 | $num_zero_writes++; | |||||||||||||||||||||||||||||||||||||||||
| 300 | } | ||||||||||||||||||||||||||||||||||||||||||
| 301 | } ## end while ($len && ($num_zero_writes...)) | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | 0 | ▶ | return ($total_len > 0 && $offset == $total_len); | ||||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$total_len > 0 && $offset == $total_len
MC/DC0%0/2
$total_len > 0 && $offset == $total_len
$total_len > 0
$offset == $total_len
Truth table0%0/3
A && B
A= $total_len > 0B= $offset == $total_len
| |||||||||||||||||||||||||||||||||||||||||||
| 304 | } ## end sub send_req | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | =head2 wait_for_readability($fileno, $timeout) | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | given a file descriptor number and a timeout, | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | wait for that descriptor to become readable | ||||||||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||||||||
| 312 | B<return> 0 or 1 on if it did or not | ||||||||||||||||||||||||||||||||||||||||||
| 313 | |||||||||||||||||||||||||||||||||||||||||||
| 314 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 315 | |||||||||||||||||||||||||||||||||||||||||||
| 316 | 1 | ▶ | sub wait_for_readability { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
wait_for_readability: called CC=4 SCAR=15.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 317 | 1 | my ($fileno, $timeout) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 318 | 1 | ▶ | return 0 unless $fileno && $timeout; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$fileno and $timeout
MC/DC0%0/2
$fileno and $timeout
$fileno
$timeout
Truth table33%1/3
A and B
A= $filenoB= $timeout
| |||||||||||||||||||||||||||||||||||||||||||
| 319 | |||||||||||||||||||||||||||||||||||||||||||
| 320 | 1 | my $rin = ''; | |||||||||||||||||||||||||||||||||||||||||
| 321 | 1 | vec($rin, $fileno, 1) = 1; | |||||||||||||||||||||||||||||||||||||||||
| 322 | 1 | my $nfound = select($rin, undef, undef, $timeout); | |||||||||||||||||||||||||||||||||||||||||
| 323 | |||||||||||||||||||||||||||||||||||||||||||
| 324 | # nfound can be undef or 0, both failures, or 1, a success | ||||||||||||||||||||||||||||||||||||||||||
| 325 | 1 | ▶ | return $nfound ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 326 | } ## end sub wait_for_readability | ||||||||||||||||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||||||||||||||||
| 328 | # | ||||||||||||||||||||||||||||||||||||||||||
| 329 | # _pack_command($prefix, $key, $arg) | ||||||||||||||||||||||||||||||||||||||||||
| 330 | # | ||||||||||||||||||||||||||||||||||||||||||
| 331 | 139 | ▶ | sub _pack_command { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pack_command: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 332 | 139 | my ($prefix, $key, $arg) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 333 | 139 | ▶ | ($key && $num{$key}) || die sprintf("Bogus type arg of '%s'", $key || ''); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%5/5
$key and $Gearman::Util::num{$key}
$key || ''
MC/DC100%2/2
$key and $Gearman::Util::num{$key}
$key
$Gearman::Util::num{$key}
MC/DC100%1/1
$key || ''
$key
Truth table100%3/3
A and B
A= $keyB= $Gearman::Util::num{$key}
Truth table100%2/2
A
A= $key
| |||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | 135 | ▶ | $arg ||= ''; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$arg ||= ""
MC/DC100%1/1
$arg ||= ""
$arg
Truth table100%2/2
A
A= $arg
| |||||||||||||||||||||||||||||||||||||||||||
| 336 | 135 | my $len = length($arg); | |||||||||||||||||||||||||||||||||||||||||
| 337 | 135 | return "\0$prefix" . pack("NN", $num{$key}, $len) . $arg; | |||||||||||||||||||||||||||||||||||||||||
| 338 | } ## end sub _pack_command | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | 20 | 1; | |||||||||||||||||||||||||||||||||||||||||