Client/Redis.pm

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