Worker/Redis.pm

filter: Statementstmt 23.1% 34 / 147 Branchbran 6.2% 2 / 32 Conditioncond 0.0% 0 / 29 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 31.2% 10 / 32 Podpod 46.6% 7 / 15 totaltotal 19.4% 53 / 273 SCAR 62.2
CC
29
Cov
17%
trigger
42.0
configure
20.5
stop
20.0
CRAP 504.5
↑ Summary Client/Redis.pm »
1 package Job::Async::Worker::Redis;
2  
3 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 5 ▶use parent qw(Job::Async::Worker);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 our $VERSION = '0.002'; # VERSION
9  
10 =head1 NAME
11  
12 Job::Async::Worker::Redis - L<Net::Async::Redis> worker implementation for L<Job::Async::Worker>
13  
14 =head1 SYNOPSIS
15  
16 =head1 DESCRIPTION
17  
18 =cut
19  
20 5 ▶use curry::weak;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 5 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 5 ▶use Job::Async::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
24 5 ▶use Future::Utils qw(repeat);
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 5 ▶use Net::Async::Redis;
Subroutine BEGIN: called CC=1 SCAR=0.0
28  
29 =head2 incoming_job
30  
31 Source for jobs received from the C<< BRPOP(LPUSH) >> queue wait.
32  
33 =cut
34  
35 sub incoming_job {
36 0 ▶ my ($self) = @_;
Subroutine incoming_job: not called CC=3 SCAR=24.8
Pod documented
37 0 ▶ $self->{incoming_job} //= do {
Condition0%0/3
$$self{"incoming_job"} //= do {
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"incoming_job"} //= do { die "needs to be part of a loop" unless $self->loop; my $src = $self->ryu->source; $src->each($self->curry::weak::on_job_received); $src }
$$self{"incoming_job"} do { die "needs to be part of a loop" unless $self->loop; my $src = $self->ryu->source; $src->each($self->curry::weak::on_job_received); $src }
Truth table0%0/3
A //= B
A= $$self{"incoming_job"}B= do { die "needs to be part of a loop" unless $self->loop; my $src = $self->ryu->source; $src->each($self->curry::weak::on_job_received); $src }
ABresult
1X1
011
000
38 0 ▶ die 'needs to be part of a loop' unless $self->loop;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->loop 0 0 0
39 0 my $src = $self->ryu->source;
40 0 $src->each($self->curry::weak::on_job_received);
41 0 $src
42 }
43 }
44  
45 =head2 on_job_received
46  
47 Called for each job that's received.
48  
49 =cut
50  
51 sub on_job_received {
52 0 ▶ my ($self, $id) = (shift, @$_);
Subroutine on_job_received: not called CC=2 SCAR=17.9
Pod documented
53 0 my ($queue) = $self->pending_queues;
54 0 local @{$log->{context}}{qw(worker_id job_id queue)} = ($self->id, $id, $queue);
55 0 $log->debugf('Received job');
56 0 ▶ if(exists $self->{pending_jobs}{$id}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $$self{'pending_jobs'}{$id}) { } 0 0 0
57 0 $log->errorf("Already have job %s", $id);
58 0 die 'Duplicate job ID';
59 } else {
60 0 undef $self->{pending_jobs}{$id};
61 }
62 0 my $job_count = 0 + keys %{$self->{pending_jobs}};
63 0 $log->tracef("Current job count is %d", $job_count);
64 0 $self->trigger;
65 $self->redis->hgetall('job::' . $id)->then(sub {
66 0 ▶ my ($items) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
67 Future::Utils::call {
68 0 ▶ local @{$log->{context}}{qw(worker_id job_id queue)} = ($self->id, $id, $queue);
Subroutine __ANON__: not called CC=2 SCAR=17.9
69 0 my %data = @$items;
70 0 $self->{pending_jobs}{$id} = my $job = Job::Async::Job->new(
71 data => Job::Async::Job->structured_data(\%data),
72 id => $id,
73 future => my $f = $self->loop->new_future,
74 );
75 0 $log->tracef('Job content is %s', { %$job });
76 $f->on_done(sub {
77 0 ▶ my ($rslt) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
78 0 local @{$log->{context}}{qw(worker_id job_id)} = ($self->id, $id);
79 0 $log->tracef("Result was %s", $rslt);
80 my $code = sub {
81 0 ▶ my $tx = shift;
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 local @{$log->{context}}{qw(worker_id job_id)} = ($self->id, $id);
83 try {
84 0 delete $self->{pending_jobs}{$id};
85 0 $log->tracef('Removing job from processing queue');
86 return Future->needs_all(
87 $tx->hmset('job::' . $id, result => "$rslt"),
88 0 $tx->publish('client::' . $data{_reply_to}, $id),
89 $tx->lrem($self->prefixed_queue($self->processing_queue) => 1, $id),
90 )
91 0 } catch {
92 0 $log->errorf("Failed due to %s", $@);
93 0 return Future->fail($@, redis => $self->id, $id);
94 }
95 0 };
96 (
97 $self->use_multi
98 ? $self->redis->multi($code)
99 : $code->($self->redis)
100 )->on_ready($self->curry::weak::trigger)
101 0 ▶ ->on_fail(sub { warn "failed? " . shift })
Subroutine __ANON__: not called CC=1 SCAR=6.9
102 0 ▶ ->retain;
Branch: 1 branch
BranchTrueFalseTotal
$self->use_multi ? : 0 0 0
103 0 });
104 0 $f->on_ready($self->curry::weak::trigger);
105 0 ▶ if(my $timeout = $self->timeout) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $timeout = $self->timeout) 0 0 0
106 Future->needs_any(
107 $f,
108 $self->loop->timeout_future(after => $timeout)
109 )->on_fail(sub {
110 0 ▶ local @{$log->{context}}{qw(worker_id job_id)} = ($self->id, $id);
Subroutine __ANON__: not called CC=2 SCAR=17.9
111 0 ▶ $log->errorf("Timeout but already completed with %s", $f->state) if $f->is_ready;
Branch: 1 branch
BranchTrueFalseTotal
if $f->is_ready 0 0 0
112 0 $f->fail('timeout')
113 0 })->retain;
114 }
115 0 $self->jobs->emit($job);
116 0 $f
117 }
118 0 })->on_fail(sub {
119 0 ▶ $log->errorf("Unable to retrieve hash key for data: %s", join " ", @_)
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 0 })->retain
121 }
122  
123 0 ▶sub use_multi { shift->{use_multi} }
Subroutine use_multi: not called CC=1 SCAR=6.9
Pod undocumented
124  
125 0 ▶sub prefix { shift->{prefix} //= 'jobs' }
Subroutine prefix: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
(shift())->{"prefix"} //= "jobs"
l!l
00
MC/DC0%0/1
(shift())->{"prefix"} //= "jobs"
(shift())->{"prefix"}
Truth table0%0/2
A
A= (shift())->{"prefix"}
Aresult
11
00
126  
127 =head2 pending_queues
128  
129 Note that L<reliable mode|Job::Async::Redis/reliable> only
130 supports a single queue, and will fail if you attempt to start with multiple
131 queues defined.
132  
133 =cut
134  
135 0 ▶sub pending_queues { @{ shift->{pending_queues} ||= [qw(pending)] } }
Subroutine pending_queues: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
(shift())->{"pending_queues"} ||= ["pending"]
l!l
00
MC/DC0%0/1
(shift())->{"pending_queues"} ||= ["pending"]
(shift())->{"pending_queues"}
Truth table0%0/2
A
A= (shift())->{"pending_queues"}
Aresult
11
00
136  
137 =head2 processing_queue
138  
139 =cut
140  
141 0 ▶sub processing_queue { shift->{processing_queue} //= 'processing' }
Subroutine processing_queue: not called CC=2 SCAR=17.9
Pod documented
Condition0%0/2
(shift())->{"processing_queue"} //= "processing"
l!l
00
MC/DC0%0/1
(shift())->{"processing_queue"} //= "processing"
(shift())->{"processing_queue"}
Truth table0%0/2
A
A= (shift())->{"processing_queue"}
Aresult
11
00
142  
143 =head2 start
144  
145 =cut
146  
147 sub start {
148 0 ▶ my ($self) = @_;
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
149  
150 0 $self->trigger;
151 }
152  
153 =head2 stop
154  
155 Requests to stop processing.
156  
157 Returns a future which will complete when all currently-processing jobs have
158 finished.
159  
160 =cut
161  
162 sub stop {
163 0 ▶ my ($self) = @_;
Subroutine stop: not called CC=4 SCAR=30.0
Pod documented
164 0 ▶ $self->{stopping_future} ||= $self->loop->new_future;
Condition0%0/3
$$self{"stopping_future"} ||= $self->loop->new_future
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"stopping_future"} ||= $self->loop->new_future
$$self{"stopping_future"} $self->loop->new_future
Truth table0%0/3
A ||= B
A= $$self{"stopping_future"}B= $self->loop->new_future
ABresult
1X1
011
000
165 0 my $pending = 0 + keys %{$self->{pending_jobs}};
166 0 ▶ if(!$pending && $self->{awaiting_job}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $pending and $$self{"awaiting_job"}) 0 0 0
Condition0%0/3
not $pending and $$self{"awaiting_job"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $pending and $$self{"awaiting_job"}
not $pending $$self{"awaiting_job"}
Truth table0%0/3
not(A) and B
A= not $pendingB= $$self{"awaiting_job"}
ABresult
0X0
100
111
167 # This will ->cancel a Net::Async::Redis future. Currently that's just
168 # ignored to no great effect, but it would be nice sometime to do
169 # something useful with that.
170 0 $self->{awaiting_job}->cancel;
171 0 $self->{stopping_future}->done;
172 }
173 # else, either a job is being processed, or there are pending ones.
174 # sub trigger will recheck
175 0 return $self->{stopping_future};
176 }
177  
178 sub queue_redis {
179 0 ▶ my ($self) = @_;
Subroutine queue_redis: not called CC=2 SCAR=17.9
Pod undocumented
180 0 ▶ unless($self->{queue_redis}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"queue_redis"}) 0 0 0
181 $self->add_child(
182 0 $self->{queue_redis} = Net::Async::Redis->new(
183 uri => $self->uri,
184 )
185 );
186 0 $self->{queue_redis}->connect;
187 }
188 0 return $self->{queue_redis};
189 }
190  
191 sub redis {
192 0 ▶ my ($self) = @_;
Subroutine redis: not called CC=2 SCAR=17.9
Pod undocumented
193 0 ▶ unless($self->{redis}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($$self{"redis"}) 0 0 0
194 $self->add_child(
195 0 $self->{redis} = Net::Async::Redis->new(
196 uri => $self->uri,
197 )
198 );
199 0 $self->{redis}->connect;
200 }
201 0 return $self->{redis};
202 }
203  
204 sub prefixed_queue {
205 0 ▶ my ($self, $q) = @_;
Subroutine prefixed_queue: not called CC=2 SCAR=17.9
Pod undocumented
206 0 ▶ return $q unless length(my $prefix = $self->prefix);
Branch: 1 branch
BranchTrueFalseTotal
unless length(my $prefix = $self->prefix) 0 0 0
207 0 return join '::', $self->prefix, $q;
208 }
209  
210 sub trigger {
211 0 ▶ my ($self) = @_;
Subroutine trigger: not called CC=6 SCAR=37.4
Pod undocumented
212 0 local @{$log->{context}}{qw(worker_id queue)} = ($self->id, my ($queue) = $self->pending_queues);
213 0 my $pending = 0 + keys %{$self->{pending_jobs}};
214 0 $log->tracef('Trigger called with %d pending tasks, %d max', $pending, $self->max_concurrent_jobs);
215 0 ▶ return if $pending >= $self->max_concurrent_jobs;
Branch: 1 branch
BranchTrueFalseTotal
if $pending >= $self->max_concurrent_jobs 0 0 0
216 0 ▶ if(!$pending and $self->{stopping_future}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $pending and $$self{"stopping_future"}) 0 0 0
Condition0%0/3
not $pending and $$self{"stopping_future"}
!ll&&!rl&&r
000
MC/DC0%0/2
not $pending and $$self{"stopping_future"}
not $pending $$self{"stopping_future"}
Truth table0%0/3
not(A) and B
A= not $pendingB= $$self{"stopping_future"}
ABresult
0X0
100
111
217 0 $self->{stopping_future}->done;
218 0 return;
219 }
220 0 ▶ $log->tracef("Start awaiting task") unless $self->{awaiting_job};
Branch: 1 branch
BranchTrueFalseTotal
unless $$self{"awaiting_job"} 0 0 0
221 $self->{awaiting_job} //= $self->queue_redis->brpoplpush(
222 $self->prefixed_queue($queue) => $self->prefixed_queue($self->processing_queue), 0
223 )->on_ready(sub {
224 0 ▶ my $f = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
225 0 delete $self->{awaiting_job};
226 0 local @{$log->{context}}{qw(worker_id queue)} = ($self->id, $queue);
227 0 $log->tracef('Had task from queue, pending now %d', 0 + keys %{$self->{pending_jobs}});
228 try {
229 0 my ($id, $queue, @details) = $f->get;
230 0 ▶ $queue //= $queue;
Condition0%0/3
$queue //= $queue
l!l&&r!l&&!r
000
MC/DC0%0/2
$queue //= $queue
$queue $queue
Truth table0%0/3
A //= B
A= $queueB= $queue
ABresult
1X1
011
000
231 0 $self->incoming_job->emit([ $id, $queue ]);
232 0 } catch {
233 0 $log->errorf("Failed to retrieve and process job: %s", $@);
234 }
235 0 $self->loop->later($self->curry::weak::trigger);
236 0 ▶ });
Condition0%0/3
$$self{"awaiting_job"} //= $self->queue_redis->brpoplpush($self->prefixed_queue($queue), $self->prefixed_queue($self->processing_queue), 0)->on_ready(sub {
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"awaiting_job"} //= $self->queue_redis->brpoplpush($self->prefixed_queue($queue), $self->prefixed_queue($self->processing_queue), 0)->on_ready(sub { my $f = shift(); delete $self->{"awaiting_job"}; local(@{$$Job::Async::Worker::Redis::log{"context"};}{"worker_id", "queue"}) = ($self->id, $queue); $Job::Async::Worker::Redis::log->tracef("Had task from queue, pending now %d", 0 + keys(%{$$self{"pending_jobs"};})); do { XXX }; $self->loop->later($self->curry::weak::trigger); } )
$$self{"awaiting_job"} $self->queue_redis->brpoplpush($self->prefixed_queue($queue), $self->prefixed_queue($self->processing_queue), 0)->on_ready(sub { my $f = shift(); delete $self->{"awaiting_job"}; local(@{$$Job::Async::Worker::Redis::log{"context"};}{"worker_id", "queue"}) = ($self->id, $queue); $Job::Async::Worker::Redis::log->tracef("Had task from queue, pending now %d", 0 + keys(%{$$self{"pending_jobs"};})); do { XXX }; $self->loop->later($self->curry::weak::trigger); } )
Truth table0%0/3
A //= B
A= $$self{"awaiting_job"}B= $self->queue_redis->brpoplpush($self->prefixed_queue($queue), $self->prefixed_queue($self->processing_queue), 0)->on_ready(sub { my $f = shift(); delete $self->{"awaiting_job"}; local(@{$$Job::Async::Worker::Redis::log{"context"};}{"worker_id", "queue"}) = ($self->id, $queue); $Job::Async::Worker::Redis::log->tracef("Had task from queue, pending now %d", 0 + keys(%{$$self{"pending_jobs"};})); do { XXX }; $self->loop->later($self->curry::weak::trigger); } )
ABresult
1X1
011
000
237 0 return;
238 }
239  
240 0 ▶sub max_concurrent_jobs { shift->{max_concurrent_jobs} //= 1 }
Subroutine max_concurrent_jobs: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
(shift())->{"max_concurrent_jobs"} //= 1
l!l
00
MC/DC0%0/1
(shift())->{"max_concurrent_jobs"} //= 1
(shift())->{"max_concurrent_jobs"}
Truth table0%0/2
A
A= (shift())->{"max_concurrent_jobs"}
Aresult
11
00
241  
242 0 ▶sub uri { shift->{uri} }
Subroutine uri: not called CC=1 SCAR=6.9
Pod undocumented
243  
244 sub configure {
245 1 ▶ my ($self, %args) = @_;
Subroutine configure: called CC=7 SCAR=30.2
Pod documented
246 1 for my $k (qw(uri max_concurrent_jobs prefix mode processing_queue use_multi)) {
247 6 ▶ $self->{$k} = delete $args{$k} if exists $args{$k};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$k} 0 6 6
248 }
249  
250 1 ▶ if(exists $args{pending_queues}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $args{"pending_queues"}) 0 1 1
251 0 ▶ if(my $queues = $args{pending_queues}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $queues = $args{'pending_queues'}) { } 0 0 0
252 0 ▶ die 'Only a single queue is supported in reliable mode' if $self->mode eq 'reliable' and @$queues > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $self->mode eq "reliable" and @$queues > 1 0 0 0
Condition0%0/3
$self->mode eq "reliable" and @$queues > 1
!ll&&!rl&&r
000
MC/DC0%0/2
$self->mode eq "reliable" and @$queues > 1
$self->mode eq "reliable" @$queues > 1
Truth table0%0/3
A and B
A= $self->mode eq "reliable"B= @$queues > 1
ABresult
0X0
100
111
253 0 $self->{pending_queues} = $queues;
254 } else {
255 delete $self->{pending_queues}
256 0 }
257 }
258 1 return $self->next::method(%args);
259 }
260  
261 5 1;
262  
263 =head1 AUTHOR
264  
265 Tom Molesworth <TEAM@cpan.org>
266  
267 =head1 LICENSE
268  
269 Copyright Tom Molesworth 2016-2019. Licensed under the same terms as Perl itself.
270  
↑ Summary Client/Redis.pm »