Beanstalk/Receive.pm

filter: Statementstmt 21.7% 32 / 147 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 16.9% 9 / 53 Podpod n/a totaltotal 15.8% 41 / 258 CC 27 SCAR 28.0
CC
27
Cov
16%
__ANON__
56.0
__ANON__
56.0
_build_on_not_found
42.0
CRAP 16.4
« Beanstalk/Send.pm ↑ Summary Beanstalk.pm »
1 package Net::Async::Beanstalk::Receive;
2  
3 3 our $VERSION = '0.001';
4 3 $VERSION = eval $VERSION;
5  
6 =head1 NAME
7  
8 Net::Async::Beanstalk::Receive - Methods to handle responses from beanstalk
9  
10 =head1 DOCUMENTED ELSEWHERE
11  
12 This module's external API is documented in L<Net::Async::Beanstalk>
13  
14 =cut
15  
16 3 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 3 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶use strictures 2;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use List::Util qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use MooX::EventHandler;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use Net::Async::Beanstalk::Constant qw(:receive :state);
Subroutine BEGIN: called CC=1 SCAR=0.0
24 3 ▶use YAML::Any qw(Load);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 3 ▶use namespace::clean;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 # TODO: Document internal API
28  
29 has_event [
30 'on_read',
31 'on_invalid_response',
32 69 (map { lc "on_$_" } keys %RESPONSE),
33 3 (map { lc "on_${_}_final" } @WITHDATA),
34 ];
35  
36 0 ▶sub _build_on_read {
Subroutine _build_on_read: not called CC=1 SCAR=6.9
37 0 ▶ sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
38 0 my $self = shift;
39 0 my ($buf, $eof) = @_;
40 0 my $data;
41  
42 0 my $state = $self->current_command;
43 0 ▶ if (ref $state and $state->[STATE_MOAR]) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $state and $state->[1]) { } 0 0 0
Condition0%0/3
ref $state and $state->[1]
!ll&&!rl&&r
000
MC/DC0%0/2
ref $state and $state->[1]
ref $state $state->[1]
Truth table0%0/3
A and B
A= ref $stateB= $state->[1]
ABresult
0X0
100
111
44 0 my ($had, $size) = @{ $state->[STATE_MOAR] };
45 0 ▶ return 0 unless $$buf =~ s/^(.{$size})$NL//s;
Branch: 1 branch
BranchTrueFalseTotal
unless $$buf =~ s/^(.{$size})$Net::Async::Beanstalk::Receive::NL//s 0 0 0
46 0 push @{ $state->[STATE_RECEIVE] }, $data = $1;
47 0 $buf = $had;
48  
49 } else {
50 # prot.c defines NAME_CHARS as alphanum & -+/;.$_()
51 0 ▶ return 0 unless $$buf =~ s{^([A-Za-z0-9+/;.\$_() -]+)$NL}{};
Branch: 1 branch
BranchTrueFalseTotal
unless $$buf =~ s[^([A-Za-z0-9+/;.\$_() -]+)$Net::Async::Beanstalk::Receive::NL][] 0 0 0
52 0 push @{ $state->[STATE_RECEIVE] }, $buf = $1;
53 }
54  
55 0 my ($got, @args) = split / /, $buf;
56  
57 0 ▶ if (not $RESPONSE{$got}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $Net::Async::Beanstalk::Receive::RESPONSE{$got}) { } 0 0 0
58 0 $self->maybe_invoke_event(on_invalid_response => $got, $buf);
59 # Can't continue; state's all screwey
60 0 die "Protocol error";
61 } else {
62 0 $self->_assert_state($got);
63 0 ▶ if ($data) {
Branch: 1 branch
BranchTrueFalseTotal
if ($data) { } 0 0 0
64 0 $self->maybe_invoke_event(lc "on_${got}_final", @args, $data);
65 } else {
66 0 $self->maybe_invoke_event(lc "on_$got", @args);
67 }
68 }
69  
70 0 return 1;
71 }
72 0 }
73  
74 # Events which are handled in unusual ways
75  
76 # OK returns some YAML data
77 0 ▶sub _build_on_ok_final {
Subroutine _build_on_ok_final: not called CC=1 SCAR=6.9
78 0 ▶ sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
79 # TODO: Alert when the result of list-tubes-watching or
80 # whatever-finds-out-used-tube are called and differ from values
81 # cached in using/watching
82 0 my $self = shift;
83 0 my ($data) = @_;
84 0 my $decoded = Load($data);
85 0 ▶ my @decoded = ref $decoded eq 'ARRAY' ? @$decoded : %$decoded;
Branch: 1 branch
BranchTrueFalseTotal
ref $decoded eq 'ARRAY' ? : 0 0 0
86 # Assert \@decoded {=} $self->watching if $source eq 'list-tubes-watched' ?
87 0 $self->finish_command(@decoded);
88 }
89 0 }
90  
91 # using and watching events change or assert which tubes are active
92 0 ▶sub _build_on_using {
Subroutine _build_on_using: not called CC=1 SCAR=6.9
93 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
94 0 my $self = shift;
95 0 my $state = $self->current_command;
96 0 my ($tube) = @_;
97 0 ▶ if ($state->[STATE_COMMAND] eq 'list-tube-used') {
Branch: 2 branches
BranchTrueFalseTotal
if ($state->[4] eq 'list-tube-used') { } 0 0 0
elsif ($state->[4] eq 'use') { } 0 0 0
98 # Assert $tube eq $self->using ?
99 0 $self->finish_command($tube);
100 } elsif ($state->[STATE_COMMAND] eq 'use') {
101 0 $self->_set_using($tube);
102 0 $self->finish_command($tube);
103 }
104 }
105 0 }
106  
107 0 ▶sub _build_on_watching {
Subroutine _build_on_watching: not called CC=1 SCAR=6.9
108 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
109 0 my $self = shift;
110 0 my $state = $self->current_command;
111 0 my $tube = $state->[STATE_DATUM];
112 0 my ($count) = @_;
113 0 ▶ if ($state->[STATE_COMMAND] eq 'ignore') {
Branch: 2 branches
BranchTrueFalseTotal
if ($state->[4] eq 'ignore') { } 0 0 0
elsif ($state->[4] eq 'watch') { } 0 0 0
114 0 delete $self->_watching->{$tube};
115 0 $self->finish_command($tube, $count);
116 } elsif ($state->[STATE_COMMAND] eq 'watch') {
117 0 $self->_watching->{$tube} = 1;
118 0 $self->finish_command($tube, $count);
119 }
120 }
121 0 }
122  
123 # Repetetive responses; First define some magic ...
124  
125 # ... with 1 way to wait,
126 0 ▶sub _makesub_wantmoar {
Subroutine _makesub_wantmoar: not called CC=1 SCAR=6.9
127 0 my ($command) = @_;
128 0 ▶ sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
129 0 my $self = shift;
130 0 my $size = pop;
131 0 $self->current_command->[STATE_MOAR] = [join(' ', $command, @_), $size];
132 0 };
133 }
134  
135 # ... 3 ways to fail,
136  
137 0 ▶sub _makesub_fail {
Subroutine _makesub_fail: not called CC=2 SCAR=17.9
138 0 ▶ my $how = @_ >= 4 ? shift : ERROR_FAIL;
Branch: 1 branch
BranchTrueFalseTotal
@_ >= 4 ? : 0 0 0
139 0 my ($category, $message, $start) = @_; # backwards
140 0 ▶ sub {
Subroutine __ANON__: not called CC=7 SCAR=40.3
141 0 my $state = $_[0]->current_command;
142 0 ▶ $state = $_[0]->_command_stack if $start == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $start == 0 0 0 0
143 0 ▶ my @nothing = (shift @$state) x ($start // STATE_DATUM);
Condition0%0/2
$start // 5
l!l
00
MC/DC0%0/1
$start // 5
$start
Truth table0%0/2
A
A= $start
Aresult
11
00
144 0 ▶ my $datum = $_[1] || $state->[STATE_DATUM] || '<?>';
Condition0%0/5
$_[1] || $state->[5] || '<?>'
l!l
00
$_[1] or $state->[5]
l!l&&r!l&&!r
000
MC/DC0%0/2
$_[1] || $state->[5] || '<?>'
$_[1] $state->[5]
Truth table0%0/3
A or B
A= $_[1]B= $state->[5]
ABresult
1X1
011
000
145 0 ▶ $how & ERROR_FAIL && $_[0]->fail_command(eval $message, $category, @$state);
Branch: 1 branch
BranchTrueFalseTotal
$how & 2 and $_[0]->fail_command(eval $message, $category, @$state) 0 0 0
146 0 ▶ $how & ERROR_EVENT && $_[0]->invoke_error( $message, $category, @$state);
Branch: 1 branch
BranchTrueFalseTotal
$how & 1 and $_[0]->invoke_error($message, $category, @$state) 0 0 0
147 }
148 0 }
149 0 ▶sub _makesub_error { _makesub(ERROR_EVENT, @_) }
Subroutine _makesub_error: not called CC=1 SCAR=6.9
150 0 ▶sub _makesub_hard { _makesub(ERROR_EVENT | ERROR_FAIL, @_) }
Subroutine _makesub_hard: not called CC=1 SCAR=6.9
151  
152 # ... 2 ways to be done with some data,
153 0 ▶sub _makesub_done { sub { $_[0]->finish_command(@_[1..$#_]) } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _makesub_done: not called CC=1 SCAR=6.9
154 0 ▶sub _makesub_done_datum { sub { $_[0]->finish_command($_[0]->current_command->[STATE_DATUM]) } }
Subroutine __ANON__: not called CC=1 SCAR=6.9
Subroutine _makesub_done_datum: not called CC=1 SCAR=6.9
155  
156 # ... and 1 way to do some combination,
157 0 ▶sub _makesub_multi {
Subroutine _makesub_multi: not called CC=1 SCAR=6.9
158 0 my %respond = @_;
159 0 ▶ sub {
Subroutine __ANON__: not called CC=4 SCAR=30.0
160 0 my $self = shift;
161 0 my $state = $self->current_command;
162 0 my $datum = $_[0];
163 0 for ($state->[STATE_COMMAND]) {
164 my ($sub, @args) = $respond{$_}[0] eq 'finish_with'
165 # There's only actually two of these so a bit of hard-coding is fine
166 0 ? (finish => [ $state->[STATE_DATUM], map {$_[$_]} @{ $respond{$_}[1] } ])
167 0 ▶ : ("$respond{$_}[0]_command" => @{$respond{$_}}[1..$#{$respond{$_}}]);
Branch: 1 branch
BranchTrueFalseTotal
$respond{$_}[0] eq 'finish_with' ? : 0 0 0
168 0 ▶ $args[0] = eval $args[0] if $_ eq 'fail';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "fail" 0 0 0
169 0 $self->$sub(@args);
170 }
171 0 };
172 }
173  
174 # ... then sprinkle it on the protocol.
175  
176 # Lots of ways to not be found
177 0 ▶sub _build_on_not_found {
Subroutine _build_on_not_found: not called CC=6 SCAR=37.4
178 _makesub_multi(
179 0 ( map { my $verb = $_ =~ s/-job//r;
180 0 my $message; # Isn't English fun?
181 0 ▶ if ($_ eq 'bury') { $verb = 'buried';
Branch: 4 branches
BranchTrueFalseTotal
if ($_ eq 'bury') { } 0 0 0
elsif ($_ =~ /e$/) { } 0 0 0
elsif ($_ =~ /[kh]$/) { } 0 0 0
elsif ($_ eq 'stats') { } 0 0 0
182 0 } elsif ($_ =~ /e$/) { $verb = $_ . 'd';
183 0 } elsif ($_ =~ /[kh]$/) { $verb = $_ .'ed';
184 0 } elsif ($_ eq 'stats') { $message = "Statistics were not found for the job: \$datum not found";
185 0 ▶ } $_ => [ fail => 'beanstalk-job' => $message // "The job could not be $verb: \$datum not found" ];
Condition0%0/2
$message // "The job could not be ${verb}: \$datum not found"
l!l
00
MC/DC0%0/1
$message // "The job could not be ${verb}: \$datum not found"
$message
Truth table0%0/2
A
A= $message
Aresult
11
00
186 } qw(bury delete touch kick-job release stats-job) ),
187 peek => [ fail => 'beanstalk-peek' => "The job could not be peeked at: \$datum not found" ],
188 0 ( map { my $adj = $_ =~ s/peek-//r; $_=> [ 'beanstalk-peek' => "The next $adj job could not be peeked at: None found" ],
189 } qw(peek-buried peek-delayed peek-ready) ),
190 'pause-tube' => [ fail => 'beanstalk-tube' => "The tube could not be paused: \$datum not found" ],
191 'stats-tube' => [ fail => 'beanstalk-tube' => "Statistics were not found for the tube: \$datum not found" ],
192 ) }
193  
194 # Two other responses from multiple commands
195 0 ▶sub _build_on_buried { _makesub_multi(
Subroutine _build_on_buried: not called CC=1 SCAR=6.9
196 bury => ['finish' ],
197 put => [ fail => 'beanstalk-put' => "Job was inserted but buried (out of memory): ID \$datum" ],
198 release => [ fail => 'beanstalk-job' => "The job could not be released (out of memory): ID \$datum" ],
199 ) }
200 0 ▶sub _build_on_kicked { _makesub_multi(
Subroutine _build_on_kicked: not called CC=1 SCAR=6.9
201 kick => [ finish_with => [0] ],
202 'kick-job' => ['finish_with' ],
203 ) }
204  
205 # 4 hard errors
206 # This module sent something badly-formed
207 0 ▶sub _build_on_bad_format { _makesub_hard ('beanstalk-internal' => "Protocol error: Bad format", STATE_SEND) }
Subroutine _build_on_bad_format: not called CC=1 SCAR=6.9
208 # The server sent something unexpected.
209 0 ▶sub _build_on_invalid_response{ _makesub_error('beanstalk-server' => "Protocol error: Unknown response", STATE_RECEIVE) }
Subroutine _build_on_invalid_response: not called CC=1 SCAR=6.9
210 # This should never happen; perhaps a version mismatch?
211 0 ▶sub _build_on_unknown_command { _makesub_hard ('beanstalk-internal' => "Protocol error: Unknown command", STATE_COMMAND) }
Subroutine _build_on_unknown_command: not called CC=1 SCAR=6.9
212 # Something broke
213 0 ▶sub _build_on_internal_error { _makesub_error('beanstalk-server' => "Protocol error: Internal error", 0) }
Subroutine _build_on_internal_error: not called CC=1 SCAR=6.9
214  
215 # Everything else is boring
216  
217 0 ▶sub _build_on_deadline_soon { _makesub_fail ('beanstalk-reserve' => "No job was reserved: Deadline soon") }
Subroutine _build_on_deadline_soon: not called CC=1 SCAR=6.9
218 0 ▶sub _build_on_deleted { _makesub_done_datum() }
Subroutine _build_on_deleted: not called CC=1 SCAR=6.9
219 0 ▶sub _build_on_draining { _makesub_fail ('beanstalk-put' => "Job was not inserted: Server is draining") }
Subroutine _build_on_draining: not called CC=1 SCAR=6.9
220 0 ▶sub _build_on_expected_crlf { _makesub_fail ('beanstalk-put' => "Protocol error: Expected cr+lf") }
Subroutine _build_on_expected_crlf: not called CC=1 SCAR=6.9
221 0 ▶sub _build_on_found { _makesub_wantmoar('FOUND') }
Subroutine _build_on_found: not called CC=1 SCAR=6.9
222 0 ▶sub _build_on_found_final { _makesub_done_datum() }
Subroutine _build_on_found_final: not called CC=1 SCAR=6.9
223 0 ▶sub _build_on_inserted { _makesub_done() }
Subroutine _build_on_inserted: not called CC=1 SCAR=6.9
224 0 ▶sub _build_on_job_too_big { _makesub_fail ('beanstalk-put' => "Invalid job: too big") }
Subroutine _build_on_job_too_big: not called CC=1 SCAR=6.9
225 0 ▶sub _build_on_not_ignored { _makesub_fail ('beanstalk-tube' => "The last tube cannot be ignored: \$datum") }
Subroutine _build_on_not_ignored: not called CC=1 SCAR=6.9
226 0 ▶sub _build_on_ok { _makesub_wantmoar('OK') }
Subroutine _build_on_ok: not called CC=1 SCAR=6.9
227 0 ▶sub _build_on_out_of_memory { _makesub_fail ('beanstalk-server' => "Protocol error: Out of memory") }
Subroutine _build_on_out_of_memory: not called CC=1 SCAR=6.9
228 0 ▶sub _build_on_paused { _makesub_done_datum() }
Subroutine _build_on_paused: not called CC=1 SCAR=6.9
229 0 ▶sub _build_on_released { _makesub_done_datum() }
Subroutine _build_on_released: not called CC=1 SCAR=6.9
230 0 ▶sub _build_on_reserved { _makesub_wantmoar('RESERVED') }
Subroutine _build_on_reserved: not called CC=1 SCAR=6.9
231 0 ▶sub _build_on_reserved_final { _makesub_done() }
Subroutine _build_on_reserved_final: not called CC=1 SCAR=6.9
232 0 ▶sub _build_on_timed_out { _makesub_fail ('beanstalk-reserve' => "No job was reserved: Timed out") }
Subroutine _build_on_timed_out: not called CC=1 SCAR=6.9
233 0 ▶sub _build_on_touched { _makesub_done_datum() }
Subroutine _build_on_touched: not called CC=1 SCAR=6.9
234  
235 3 1;
« Beanstalk/Send.pm ↑ Summary Beanstalk.pm »