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