Client/Redis.pm

filter: Statementstmt 34.8% 39 / 112 Branchbran 7.1% 1 / 14 Conditioncond 0.0% 0 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 34.2% 12 / 35 Podpod 46.6% 7 / 15 totaltotal 31.0% 59 / 190 SCAR 42.8
CC
13
Cov
30%
submit
12.0
prefix
6.0
prefixed_queue
6.0
CRAP 71.9
« Worker/Redis.pm ↑ Summary Redis.pm »
1 package Job::Async::Client::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 mro;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 5 ▶use parent qw(Job::Async::Client);
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 5 our $VERSION = '0.003'; # VERSION
11  
12 =head1 NAME
13  
14 Job::Async::Client::Redis - L<Net::Async::Redis> client implementation for L<Job::Async::Client>
15  
16 =head1 SYNOPSIS
17  
18 =head1 DESCRIPTION
19  
20 =cut
21  
22 5 ▶no indirect;
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 5 ▶use Syntax::Keyword::Try;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 5 ▶use JSON::MaybeUTF8 qw(:v1);
Subroutine BEGIN: called CC=1 SCAR=0.0
26 5 ▶use Ryu::Async;
Subroutine BEGIN: called CC=1 SCAR=0.0
27 5 ▶use Job::Async::Utils;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 5 ▶use Net::Async::Redis 1.003;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 5 ▶use Log::Any qw($log);
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 # Our client has a single Redis connection, a UUID to
33 # represent the client, and expects to see job announcements
34 # on the pubsub channel client::$client_id. For each
35 # announcement, the payload represents the job ID, and we get
36 # the actual details from the job hash.
37  
38 sub _add_to_loop {
39 0 ▶ my ($self) = @_;
Subroutine _add_to_loop: not called CC=1 SCAR=6.9
40 $self->add_child(
41 0 $self->{client} = Net::Async::Redis->new(
42 uri => $self->uri,
43 )
44 );
45 $self->add_child(
46 0 $self->{subscriber} = Net::Async::Redis->new(
47 uri => $self->uri,
48 )
49 );
50 $self->add_child(
51 0 $self->{submitter} = Net::Async::Redis->new(
52 uri => $self->uri,
53 )
54 );
55 $self->add_child(
56 0 $self->{ryu} = Ryu::Async->new
57 );
58 }
59  
60 =head2 client
61  
62 =cut
63  
64 0 ▶sub client { shift->{client} }
Subroutine client: not called CC=1 SCAR=6.9
Pod documented
65  
66 =head2 subscriber
67  
68 =cut
69  
70 0 ▶sub subscriber { shift->{subscriber} }
Subroutine subscriber: not called CC=1 SCAR=6.9
Pod documented
71  
72 =head2 submitter
73  
74 =cut
75  
76 0 ▶sub submitter { shift->{submitter} }
Subroutine submitter: not called CC=1 SCAR=6.9
Pod documented
77  
78 0 ▶sub ryu { shift->{ryu} }
Subroutine ryu: not called CC=1 SCAR=6.9
Pod undocumented
79  
80 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
81  
82 sub prefixed_queue {
83 0 ▶ my ($self, $q) = @_;
Subroutine prefixed_queue: not called CC=2 SCAR=17.9
Pod undocumented
84 0 ▶ return $q unless length(my $prefix = $self->prefix);
Branch: 1 branch
BranchTrueFalseTotal
unless length(my $prefix = $self->prefix) 0 0 0
85 0 return join '::', $self->prefix, $q;
86 }
87 0 ▶sub queue { shift->{queue} //= 'pending' }
Subroutine queue: not called CC=2 SCAR=17.9
Pod undocumented
Condition0%0/2
(shift())->{"queue"} //= "pending"
l!l
00
MC/DC0%0/1
(shift())->{"queue"} //= "pending"
(shift())->{"queue"}
Truth table0%0/2
A
A= (shift())->{"queue"}
Aresult
11
00
88  
89 =head2 start
90  
91 =cut
92  
93 sub start {
94 0 ▶ my ($self) = @_;
Subroutine start: not called CC=1 SCAR=6.9
Pod documented
95 0 local $log->{context}{client_id} = $self->id;
96 try {
97 0 $log->tracef("Client awaiting Redis connections via %s", '' . $self->uri);
98 return Future->wait_all(
99 $self->client->connect,
100 $self->submitter->connect,
101 $self->subscriber->connect
102 )->then(sub {
103 0 ▶ local $log->{context}{client_id} = $self->id;
Subroutine __ANON__: not called CC=1 SCAR=6.9
104 0 $log->tracef("Subscribing to notifications");
105 0 return $self->subscriber
106 ->subscribe('client::' . $self->id)
107 ->on_done(
108 $self->curry::weak::on_subscribed
109 );
110 })
111 0 } catch {
112 0 $log->errorf('Failed on connection setup - %s', $@);
113 0 die $@;
114 }
115 }
116  
117 =head2 on_subscribed
118  
119 =cut
120  
121 sub on_subscribed {
122 0 ▶ my ($self, $sub) = @_;
Subroutine on_subscribed: not called CC=1 SCAR=6.9
Pod documented
123 0 local $log->{context}{client_id} = $self->id;
124 # Every time someone tells us they finished a job, we pull back the details
125 # and check the results
126 $sub->events
127 ->map('payload')
128 ->each(sub {
129 0 ▶ my ($id) = @_;
Subroutine __ANON__: not called CC=2 SCAR=17.9
130 0 local @{$log->{context}}{qw(client_id job_id)} = ($self->id, $id);
131 0 $log->tracef("Received job notification");
132 0 my $job = $self->pending_job($id);
133 0 my $client = $self->client;
134 ($job ? $client->hmget('job::' . $id, 'result')->then(sub {
135 0 ▶ local @{$log->{context}}{qw(client_id job_id)} = ($self->id, $id);
Subroutine __ANON__: not called CC=2 SCAR=17.9
136 0 my ($result) = @{$_[0]};
137 0 my $type = substr $result, 0, 1, '';
138 0 ▶ $result = decode_json_utf8($result) if $type eq 'J';
Branch: 1 branch
BranchTrueFalseTotal
if $type eq "J" 0 0 0
139 0 $log->tracef('Job result %s', $result);
140 0 $job->done($result);
141 }) : Future->done)->then(sub {
142 0 ▶ local @{$log->{context}}{qw(client_id job_id)} = ($self->id, $id);
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 $log->tracef('Removing job data');
144 0 $client->del('job::' . $id);
145 })->on_fail(sub {
146 0 ▶ local @{$log->{context}}{qw(client_id job_id)} = ($self->id, $id);
Subroutine __ANON__: not called CC=1 SCAR=6.9
147 0 $log
148 0 ▶ })->retain;
Branch: 1 branch
BranchTrueFalseTotal
$job ? : 0 0 0
149 0 });
150  
151 0 $log->tracef("Redis connections established, starting client operations");
152 }
153  
154 sub submit {
155 0 ▶ my $self = shift;
Subroutine submit: not called CC=3 SCAR=24.8
Pod documented
156 my $job = (@_ == 1)
157 ? shift
158 0 ▶ : do {
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 ? : 0 0 0
159 0 Job::Async::Job->new(
160 future => $self->loop->new_future,
161 id => Job::Async::Utils::uuid(),
162 data => { @_ },
163 );
164 };
165 0 $self->{pending_job}{$job->id} = $job;
166 my $code = sub {
167 0 ▶ my $tx = shift;
Subroutine __ANON__: not called CC=2 SCAR=17.9
168 0 ▶ my $id = $job->id // die 'no job ID?';
Condition0%0/2
$job->id // die("no job ID?")
l!l
00
MC/DC0%0/1
$job->id // die("no job ID?")
$job->id
Truth table0%0/2
A
A= $job->id
Aresult
11
00
169 return Future->needs_all(
170 $tx->hmset(
171 'job::' . $id,
172 _reply_to => $self->id,
173 0 %{ $job->flattened_data }
174 ),
175 $tx->lpush($self->prefixed_queue($self->queue), $id)
176 ->on_done(sub {
177 0 ▶ my ($count) = @_;
Subroutine __ANON__: not called CC=1 SCAR=6.9
178 0 local @{$log->{context}}{qw(client_id job_id)} = ($self->id, $id);
179 0 $log->tracef('Job count for [%s] now %d', $self->queue, $count);
180 0 $self->queue_length
181 ->emit($count);
182 })
183 0 );
184 0 };
185 return (
186 $self->use_multi
187 ? $self->submitter->multi($code)
188 : $code->($self->submitter)
189 0 ▶ )->then(sub { $job->future })
Subroutine __ANON__: not called CC=1 SCAR=6.9
190 0 ▶ ->retain
Branch: 1 branch
BranchTrueFalseTotal
$self->use_multi ? : 0 0 0
191 }
192  
193 sub queue_length {
194 0 ▶ my ($self) = @_;
Subroutine queue_length: not called CC=2 SCAR=17.9
Pod undocumented
195 0 ▶ $self->{queue_length} ||= $self->ryu->source(
Condition0%0/3
$$self{"queue_length"} ||= $self->ryu->source("label", 'Currently pending events for ' . $self->queue)
l!l&&r!l&&!r
000
MC/DC0%0/2
$$self{"queue_length"} ||= $self->ryu->source("label", 'Currently pending events for ' . $self->queue)
$$self{"queue_length"} $self->ryu->source("label", 'Currently pending events for ' . $self->queue)
Truth table0%0/3
A ||= B
A= $$self{"queue_length"}B= $self->ryu->source("label", 'Currently pending events for ' . $self->queue)
ABresult
1X1
011
000
196 label => 'Currently pending events for ' . $self->queue
197 );
198 }
199  
200 0 ▶sub use_multi { shift->{use_multi} }
Subroutine use_multi: not called CC=1 SCAR=6.9
Pod undocumented
201  
202 sub pending_job {
203 0 ▶ my ($self, $id) = @_;
Subroutine pending_job: not called CC=2 SCAR=17.9
Pod undocumented
204 0 ▶ die 'no ID' unless defined $id;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $id 0 0 0
205 0 return delete $self->{pending_job}{$id}
206 }
207  
208 sub configure {
209 2 ▶ my ($self, %args) = @_;
Subroutine configure: called CC=3 SCAR=11.1
Pod documented
210 2 for (qw(queue uri use_multi prefix)) {
211 8 ▶ $self->{$_} = delete $args{$_} if exists $args{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $args{$_} 0 8 8
212 }
213 2 $self->next::method(%args)
214 }
215  
216 0 ▶sub uri { shift->{uri} }
Subroutine uri: not called CC=1 SCAR=6.9
Pod undocumented
217  
218 5 1;
219  
220 =head1 AUTHOR
221  
222 Tom Molesworth <TEAM@cpan.org>
223  
224 =head1 LICENSE
225  
226 Copyright Tom Molesworth 2016-2017. Licensed under the same terms as Perl itself.
227  
« Worker/Redis.pm ↑ Summary Redis.pm »