Qless/Queue.pm

filter: Statementstmt 23.1% 19 / 82 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 36 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 28.5% 6 / 21 Podpod 71.4% 10 / 14 totaltotal 18.8% 35 / 186 CC 24 SCAR 31.9
CC
24
Cov
14%
put
56.0
recur
56.0
heartbeat
20.0
CRAP 24.3
↑ Summary Qless/Worker.pm »
1 package Qless::Queue;
2 =head1 NAME
3  
4 Qless:Queue
5  
6 =cut
7  
8 1 ▶use strict; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use JSON::XS qw(decode_json encode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Qless::Jobs;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Qless::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Time::HiRes qw();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 METHODS
15  
16 =head2 C<new>
17 =cut
18 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
19 0 my $class = shift;
20 0 my ($name, $client, $worker_name) = @_;
21  
22 0 ▶ $class = ref $class if ref $class;
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 0 0 0
23 0 my $self = bless {}, $class;
24  
25 0 $self->{'name'} = $name;
26 0 $self->{'client'} = $client;
27 0 $self->{'worker_name'} = $worker_name;
28  
29 0 $self;
30 }
31  
32 0 ▶sub generate_jid {
Subroutine generate_jid: not called CC=1 SCAR=6.9
Pod undocumented
33 0 my ($self, $data) = @_;
34 0 return $self->{'worker_name'}.'-'.CORE::time.'-'.sprintf('%06d', int(rand(999999)));
35 }
36  
37 0 ▶sub client { $_[0]->{'client'} }
Subroutine client: not called CC=1 SCAR=6.9
Pod undocumented
38 0 ▶sub name { $_[0]->{'name'} }
Subroutine name: not called CC=1 SCAR=6.9
Pod undocumented
39 0 ▶sub worker_name { $_[0]->{'worker_name'} }
Subroutine worker_name: not called CC=1 SCAR=6.9
Pod undocumented
40  
41  
42 =head2 C<jobs>
43  
44 =cut
45  
46 0 ▶sub jobs {
Subroutine jobs: not called CC=1 SCAR=6.9
Pod documented
47 0 my ($self) = @_;
48 0 Qless::Jobs->new($self->{'name'}, $self->{'client'});
49 }
50  
51 =head2 C<counts>
52  
53 =cut
54  
55 0 ▶sub counts {
Subroutine counts: not called CC=1 SCAR=6.9
Pod documented
56 0 my ($self) = @_;
57 0 return decode_json($self->{'client'}->_queues([], Time::HiRes::time, $self->{'name'}));
58 }
59  
60 =head2 C<heartbeat>
61 =cut
62 0 ▶sub heartbeat {
Subroutine heartbeat: not called CC=4 SCAR=30.0
Pod documented
63 0 my ($self, $new_value) = @_;
64  
65 0 my $config = $self->{'client'}->config;
66  
67 0 ▶ if (defined $new_value) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $new_value) 0 0 0
68 0 $config->set($self->{'name'}.'-heartbeat', $new_value);
69 0 return;
70 }
71  
72 0 ▶ return $config->get($self->{'name'}.'-heartbeat') || $config->get('heartbeat') || 60;
Condition0%0/5
$config->get($self->{'name'} . '-heartbeat') || $config->get('heartbeat') || 60
l!l
00
$config->get($self->{"name"} . '-heartbeat') or $config->get("heartbeat")
l!l&&r!l&&!r
000
MC/DC0%0/2
$config->get($self->{'name'} . '-heartbeat') || $config->get('heartbeat') || 60
$config->get($self->{"name"} . '-heartbeat') $config->get("heartbeat")
Truth table0%0/3
A or B
A= $config->get($self->{"name"} . '-heartbeat')B= $config->get("heartbeat")
ABresult
1X1
011
000
73 }
74  
75 =head2 C<put>
76 =cut
77 0 ▶sub put {
Subroutine put: not called CC=7 SCAR=40.3
Pod documented
78 0 my ($self, $klass, $data, %args ) = @_;
79  
80 return $self->{'client'}->_put([$self->{'name'}],
81 $args{'jid'} || $self->generate_jid($data),
82 $klass,
83 encode_json($data),
84 Time::HiRes::time,
85 $args{'delay'} || 0,
86 'priority', $args{'priority'} || 0,
87 'tags', encode_json($args{'tags'} || []),
88 'retries', $args{'retries'} || 5,
89 0 ▶ 'depends', encode_json($args{'depends'} || []),
Condition0%0/13
$args{'jid'} || $self->generate_jid($data)
l!l&&r!l&&!r
000
$args{'delay'} || 0
l!l
00
$args{'priority'} || 0
l!l
00
$args{'tags'} || []
l!l
00
$args{'retries'} || 5
l!l
00
$args{'depends'} || []
l!l
00
MC/DC0%0/2
$args{'jid'} || $self->generate_jid($data)
$args{'jid'} $self->generate_jid($data)
MC/DC0%0/1
$args{'delay'} || 0
$args{'delay'}
MC/DC0%0/1
$args{'priority'} || 0
$args{'priority'}
MC/DC0%0/1
$args{'tags'} || []
$args{'tags'}
MC/DC0%0/1
$args{'retries'} || 5
$args{'retries'}
MC/DC0%0/1
$args{'depends'} || []
$args{'depends'}
Truth table0%0/3
A || B
A= $args{'jid'}B= $self->generate_jid($data)
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{'delay'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'priority'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'tags'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'retries'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'depends'}
Aresult
11
00
90 );
91 }
92  
93 =head2 C<recur>
94 =cut
95 0 ▶sub recur {
Subroutine recur: not called CC=7 SCAR=40.3
Pod documented
96 0 my ($self, $klass, $data, @args) = @_;
97  
98 0 my $interval;
99 my %args;
100  
101 0 ▶ if (scalar(@args)%2) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar(@args) % 2) { } 0 0 0
102 0 $interval = shift @args;
103 0 %args = @args;
104 }
105 else {
106 0 %args = @args;
107 0 $interval = $args{'interval'};
108 }
109  
110 return $self->{'client'}->_recur([], 'on', $self->{'name'},
111 $args{'jid'} || $self->generate_jid($data),
112 $klass,
113 encode_json($data),
114 Time::HiRes::time,
115 'interval', $interval, $args{'offset'} || 0,
116 'priority', $args{'priority'} || 0,
117 'tags', encode_json($args{'tags'} || []),
118 0 ▶ 'retries', $args{'retries'} || 5,
Condition0%0/11
$args{'jid'} || $self->generate_jid($data)
l!l&&r!l&&!r
000
$args{'offset'} || 0
l!l
00
$args{'priority'} || 0
l!l
00
$args{'tags'} || []
l!l
00
$args{'retries'} || 5
l!l
00
MC/DC0%0/2
$args{'jid'} || $self->generate_jid($data)
$args{'jid'} $self->generate_jid($data)
MC/DC0%0/1
$args{'offset'} || 0
$args{'offset'}
MC/DC0%0/1
$args{'priority'} || 0
$args{'priority'}
MC/DC0%0/1
$args{'tags'} || []
$args{'tags'}
MC/DC0%0/1
$args{'retries'} || 5
$args{'retries'}
Truth table0%0/3
A || B
A= $args{'jid'}B= $self->generate_jid($data)
ABresult
1X1
011
000
Truth table0%0/2
A
A= $args{'offset'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'priority'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'tags'}
Aresult
11
00
Truth table0%0/2
A
A= $args{'retries'}
Aresult
11
00
119 );
120  
121 }
122  
123 =head2 C<pop>
124 =cut
125 0 ▶sub pop {
Subroutine pop: not called CC=4 SCAR=30.0
Pod documented
126 0 my ($self, $count) = @_;
127 0 my $jobs = [ map { Qless::Job->new($self->{'client'}, decode_json($_)) }
128 0 ▶ @{ $self->{'client'}->_pop([$self->{'name'}], $self->{'worker_name'}, $count||1, Time::HiRes::time) } ];
Condition0%0/2
$count || 1
l!l
00
MC/DC0%0/1
$count || 1
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
129 0 ▶ if (!defined $count) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $count) 0 0 0
130 0 ▶ return scalar @{ $jobs } ? $jobs->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$jobs;} ? : 0 0 0
131 }
132  
133 0 return @{ $jobs };
134 }
135  
136 =head2 C<peek>
137 =cut
138 0 ▶sub peek {
Subroutine peek: not called CC=4 SCAR=30.0
Pod documented
139 0 my ($self, $count) = @_;
140 0 my $jobs = [ map { Qless::Job->new($self->{'client'}, decode_json($_)) }
141 0 ▶ @{ $self->{'client'}->_peek([$self->{'name'}], $count||1, Time::HiRes::time) } ];
Condition0%0/2
$count || 1
l!l
00
MC/DC0%0/1
$count || 1
$count
Truth table0%0/2
A
A= $count
Aresult
11
00
142 0 ▶ if (!defined $count) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $count) 0 0 0
143 0 ▶ return scalar @{ $jobs } ? $jobs->[0] : undef;
Branch: 1 branch
BranchTrueFalseTotal
scalar @{$jobs;} ? : 0 0 0
144 }
145  
146 0 return @{ $jobs };
147 }
148  
149 =head2 C<stats>
150  
151 =cut
152  
153 0 ▶sub stats {
Subroutine stats: not called CC=2 SCAR=17.9
Pod documented
154 0 my ($self, $date) = @_;
155 0 ▶ return decode_json($self->{'client'}->_stats([], $self->{'name'}, $date || Time::HiRes::time));
Condition0%0/3
$date || &Time::HiRes::time()
l!l&&r!l&&!r
000
MC/DC0%0/2
$date || &Time::HiRes::time()
$date &Time::HiRes::time()
Truth table0%0/3
A || B
A= $dateB= &Time::HiRes::time()
ABresult
1X1
011
000
156 }
157  
158 =head2 C<length>
159  
160 =cut
161  
162 0 ▶sub length {
Subroutine length: not called CC=1 SCAR=6.9
Pod documented
163 0 my ($self) = @_;
164  
165 0 my $redis = $self->{'client'}->{'redis'};
166 0 my $sum = 0;
167 0 ▶ my $sum_cb = sub { $sum += shift };
Subroutine __ANON__: not called CC=1 SCAR=6.9
168  
169 0 $redis->zcard('ql:q:'.$self->{'name'}.'-locks', $sum_cb);
170 0 $redis->zcard('ql:q:'.$self->{'name'}.'-work', $sum_cb);
171 0 $redis->zcard('ql:q:'.$self->{'name'}.'-scheduled', $sum_cb);
172 0 $redis->wait_all_responses;
173  
174 0 return $sum;
175 }
176  
177 1 1;
↑ Summary Qless/Worker.pm »