blib/lib/Qudo/Manager.pm

filter: Statementstmt 15.0% 19 / 126 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 19.3% 6 / 31 Podpod 0.0% 0 / 23 totaltotal 10.8% 25 / 231 CC 23 SCAR 21.2
CC
23
Cov
12%
enqueue
20.0
_grab_a_job
20.0
register_plugins
12.0
CRAP 8.3
« blib/lib/Qudo.pm ↑ Summary blib/lib/Qudo/Driver/DBI/DBD.pm »
1 package Qudo::Manager;
2 30 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 30 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 30 ▶use Qudo::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 30 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 30 ▶use Qudo::HookLoader;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 sub new {
10 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
11  
12 0 my $self = bless {
13 driver_for => '',
14 shuffled_databases => '',
15 find_job_limit_size => '',
16 retry_seconds => '',
17 fanc_map => +{},
18 default_hooks => [],
19 default_plugins => [],
20 hooks => +{},
21 plugin => +{},
22 abilities => [],
23 @_
24 }, $class;
25  
26 0 $self->global_register_hooks(@{$self->{default_hooks}});
27 0 $self->register_plugins(@{$self->{default_plugins}});
28 0 $self->register_abilities(@{$self->{abilities}});
29  
30 0 return $self;
31 }
32  
33 0 ▶sub driver_for { $_[0]->{driver_for}->($_[1]) }
Subroutine driver_for: not called CC=1 SCAR=6.9
Pod undocumented
34 0 ▶sub shuffled_databases { $_[0]->{shuffled_databases}->() }
Subroutine shuffled_databases: not called CC=1 SCAR=6.9
Pod undocumented
35 0 ▶sub plugin { $_[0]->{plugin} }
Subroutine plugin: not called CC=1 SCAR=6.9
Pod undocumented
36  
37 sub register_abilities {
38 0 ▶ my ($self, @abilities) = @_;
Subroutine register_abilities: not called CC=2 SCAR=17.9
Pod undocumented
39  
40 0 for my $ability (@abilities) {
41 0 $self->can_do($ability);
42 }
43 }
44  
45 sub has_abilities {
46 0 ▶ keys %{$_[0]->{func_map}};
Subroutine has_abilities: not called CC=1 SCAR=6.9
Pod undocumented
47 }
48  
49 sub register_plugins {
50 0 ▶ my ($self, @plugins) = @_;
Subroutine register_plugins: not called CC=3 SCAR=24.8
Pod undocumented
51  
52 0 for my $plugin (@plugins) {
53 0 ▶ $plugin->require or Carp::croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$plugin->require or Carp::croak($@) 0 0 0
54 0 my ($plugin_name, $code) = $plugin->load();
55 0 $self->{plugin}->{$plugin_name} = $code;
56 }
57 }
58  
59 sub call_hook {
60 0 ▶ my ($self, $hook_point, $worker_class, $args) = @_;
Subroutine call_hook: not called CC=3 SCAR=24.8
Pod undocumented
61  
62 0 for my $module (keys %{$worker_class->hooks->{$hook_point}}) {
63 0 my $code = $worker_class->hooks->{$hook_point}->{$module};
64 0 $code->($args);
65 }
66  
67 0 for my $module (keys %{$self->hooks->{$hook_point}}) {
68 0 my $code = $self->hooks->{$hook_point}->{$module};
69 0 $code->($args);
70 }
71 }
72  
73 0 ▶sub hooks { $_[0]->{hooks} }
Subroutine hooks: not called CC=1 SCAR=6.9
Pod undocumented
74  
75 sub global_register_hooks {
76 0 ▶ my ($self, @hook_modules) = @_;
Subroutine global_register_hooks: not called CC=1 SCAR=6.9
Pod undocumented
77  
78 0 Qudo::HookLoader->register_hooks($self, \@hook_modules);
79 }
80  
81 sub global_unregister_hooks {
82 0 ▶ my ($self, @hook_modules) = @_;
Subroutine global_unregister_hooks: not called CC=1 SCAR=6.9
Pod undocumented
83  
84 0 Qudo::HookLoader->unregister_hooks($self, \@hook_modules);
85 }
86  
87 sub can_do {
88 0 ▶ my ($self, $funcname) = @_;
Subroutine can_do: not called CC=1 SCAR=6.9
Pod undocumented
89  
90 0 $funcname->use;
91 0 $self->{func_map}->{$funcname} = 1;
92 }
93  
94 sub funcname_to_id {
95 0 ▶ my ($self, $funcname, $db) = @_;
Subroutine funcname_to_id: not called CC=2 SCAR=17.9
Pod undocumented
96 0 ▶ $self->{_func_cache}->{$db}->{funcname2id}->{$funcname} ||= $self->driver_for($db)->get_func_id( $funcname );
Condition0%0/3
$self->{"_func_cache"}{$db}{"funcname2id"}{$funcname} ||= $self->driver_for($db)->get_func_id($funcname)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_func_cache"}{$db}{"funcname2id"}{$funcname} ||= $self->driver_for($db)->get_func_id($funcname)
$self->{"_func_cache"}{$db}{"funcname2id"}{$funcname} $self->driver_for($db)->get_func_id($funcname)
Truth table0%0/3
A ||= B
A= $self->{"_func_cache"}{$db}{"funcname2id"}{$funcname}B= $self->driver_for($db)->get_func_id($funcname)
ABresult
1X1
011
000
97 }
98  
99 sub funcid_to_name {
100 0 ▶ my ($self, $funcid, $db) = @_;
Subroutine funcid_to_name: not called CC=2 SCAR=17.9
Pod undocumented
101 0 ▶ $self->{_func_cache}->{$db}->{funcid2name}->{$funcid} ||= $self->driver_for($db)->get_func_name( $funcid );
Condition0%0/3
$self->{"_func_cache"}{$db}{"funcid2name"}{$funcid} ||= $self->driver_for($db)->get_func_name($funcid)
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"_func_cache"}{$db}{"funcid2name"}{$funcid} ||= $self->driver_for($db)->get_func_name($funcid)
$self->{"_func_cache"}{$db}{"funcid2name"}{$funcid} $self->driver_for($db)->get_func_name($funcid)
Truth table0%0/3
A ||= B
A= $self->{"_func_cache"}{$db}{"funcid2name"}{$funcid}B= $self->driver_for($db)->get_func_name($funcid)
ABresult
1X1
011
000
102 }
103  
104 sub enqueue {
105 0 ▶ my ($self, $funcname, $arg) = @_;
Subroutine enqueue: not called CC=4 SCAR=30.0
Pod undocumented
106  
107 0 my $db = $self->shuffled_databases;
108 0 my $func_id = $self->funcname_to_id($funcname, $db);
109  
110 0 ▶ unless ($func_id) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($func_id) 0 0 0
111 0 croak "$funcname can't get";
112 }
113  
114 my $args = +{
115 func_id => $func_id,
116 arg => $arg->{arg},
117 uniqkey => $arg->{uniqkey},
118 run_after => $arg->{run_after}||0,
119 0 ▶ priority => $arg->{priority} ||0,
Condition0%0/4
$arg->{'run_after'} || 0
l!l
00
$arg->{'priority'} || 0
l!l
00
MC/DC0%0/1
$arg->{'run_after'} || 0
$arg->{'run_after'}
MC/DC0%0/1
$arg->{'priority'} || 0
$arg->{'priority'}
Truth table0%0/2
A
A= $arg->{'run_after'}
Aresult
11
00
Truth table0%0/2
A
A= $arg->{'priority'}
Aresult
11
00
120 };
121  
122 0 $self->call_hook('pre_enqueue', $funcname, $args);
123 0 $self->call_hook('serialize', $funcname, $args);
124  
125 0 my $job_id = $self->driver_for($db)->enqueue($args);
126 0 my $job = $self->lookup_job($job_id, $db);
127  
128 0 $self->call_hook('post_enqueue', $funcname, $job);
129  
130 0 return $job;
131 }
132  
133 sub reenqueue {
134 0 ▶ my ($self, $job, $args) = @_;
Subroutine reenqueue: not called CC=1 SCAR=6.9
Pod undocumented
135  
136 0 my $db = $self->shuffled_databases;
137 0 $self->driver_for($db)->reenqueue($job->id, $args);
138  
139 0 return $self->lookup_job($job->id);
140 }
141  
142 sub dequeue {
143 0 ▶ my ($self, $job) = @_;
Subroutine dequeue: not called CC=1 SCAR=6.9
Pod undocumented
144 0 $self->driver_for($job->db)->dequeue({id => $job->id});
145 }
146  
147 sub work_once {
148 0 ▶ my $self = shift;
Subroutine work_once: not called CC=3 SCAR=24.8
Pod undocumented
149  
150 0 my $job = $self->find_job;
151 0 ▶ return unless $job;
Branch: 1 branch
BranchTrueFalseTotal
unless $job 0 0 0
152  
153 0 my $worker_class = $job->funcname;
154 0 ▶ return unless $worker_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $worker_class 0 0 0
155  
156 0 $self->call_hook('deserialize', $worker_class, $job);
157 0 $self->call_hook('pre_work', $worker_class, $job);
158  
159 0 my $res = $worker_class->work_safely($self, $job);
160  
161 0 $self->call_hook('post_work', $worker_class, $job);
162  
163 0 return $res;
164 }
165  
166 sub lookup_job {
167 0 ▶ my ($self, $job_id, $db) = @_;
Subroutine lookup_job: not called CC=3 SCAR=24.8
Pod undocumented
168  
169 0 ▶ $db ||= $self->shuffled_databases;
Condition0%0/3
$db ||= $self->shuffled_databases
l!l&&r!l&&!r
000
MC/DC0%0/2
$db ||= $self->shuffled_databases
$db $self->shuffled_databases
Truth table0%0/3
A ||= B
A= $dbB= $self->shuffled_databases
ABresult
1X1
011
000
170  
171 0 my $callback = $self->driver_for($db)->lookup_job($job_id);
172 0 my $job_data = $callback->();
173 0 ▶ return $job_data ? $self->_data2job($job_data, $db) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$job_data ? : 0 0 0
174 }
175  
176 sub find_job {
177 0 ▶ my $self = shift;
Subroutine find_job: not called CC=3 SCAR=24.8
Pod undocumented
178  
179 0 for my $db ($self->shuffled_databases) {
180 0 ▶ return unless keys %{$self->{func_map}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"func_map"};} 0 0 0
181 0 my $callback = $self->driver_for($db)->find_job($self->{find_job_limit_size}, $self->{func_map});
182  
183 0 return $self->_grab_a_job($callback, $db);
184 }
185 }
186  
187 sub _data2job {
188 0 ▶ my ($self, $job_data, $db) = @_;
Subroutine _data2job: not called CC=1 SCAR=6.9
189  
190 0 Qudo::Job->new(
191 manager => $self,
192 job_data => $job_data,
193 db => $db,
194 );
195 }
196  
197 sub _grab_a_job {
198 0 ▶ my ($self, $callback, $db) = @_;
Subroutine _grab_a_job: not called CC=4 SCAR=30.0
199  
200 0 while (1) {
201 0 my $job_data = $callback->();
202 0 ▶ last unless $job_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $job_data 0 0 0
203  
204 0 my $old_grabbed_until = $job_data->{job_grabbed_until};
205 0 ▶ my $server_time = $self->driver_for($db)->get_server_time
Branch: 1 branch
BranchTrueFalseTotal
my $server_time = $self->driver_for($db)->get_server_time or die "expected a server time" 0 0 0
206 or die "expected a server time";
207  
208 0 my $worker_class = $job_data->{func_name};
209 my $grab_job = $self->driver_for($db)->grab_a_job(
210 grabbed_until => ($server_time + $worker_class->grab_for),
211 job_id => $job_data->{job_id},
212 0 old_grabbed_until => $old_grabbed_until,
213 );
214 0 ▶ next if $grab_job < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $grab_job < 1 0 0 0
215  
216 0 return $self->_data2job($job_data, $db);
217 }
218 0 return;
219 }
220  
221 sub job_failed {
222 0 ▶ my ($self, $job, $message) = @_;
Subroutine job_failed: not called CC=2 SCAR=17.9
Pod undocumented
223  
224 0 ▶ $self->driver_for($job->db)->logging_exception(
Condition0%0/3
$job->arg_origin || $job->arg
l!l&&r!l&&!r
000
MC/DC0%0/2
$job->arg_origin || $job->arg
$job->arg_origin $job->arg
Truth table0%0/3
A || B
A= $job->arg_originB= $job->arg
ABresult
1X1
011
000
225 {
226 func_id => $job->func_id,
227 message => $message,
228 uniqkey => $job->uniqkey,
229 arg => $job->arg_origin || $job->arg,
230 }
231 );
232 }
233  
234 sub set_job_status {
235 0 ▶ my ($self, $job, $status) = @_;
Subroutine set_job_status: not called CC=2 SCAR=17.9
Pod undocumented
236  
237 0 ▶ $self->driver_for($job->db)->set_job_status(
Condition0%0/3
$job->arg_origin || $job->arg
l!l&&r!l&&!r
000
MC/DC0%0/2
$job->arg_origin || $job->arg
$job->arg_origin $job->arg
Truth table0%0/3
A || B
A= $job->arg_originB= $job->arg
ABresult
1X1
011
000
238 {
239 func_id => $job->func_id,
240 arg => $job->arg_origin || $job->arg,
241 uniqkey => $job->uniqkey,
242 status => $status,
243 job_start_time => $job->job_start_time,
244 job_end_time => time(),
245 }
246 );
247 }
248  
249 sub enqueue_from_failed_job {
250 0 ▶ my ($self, $exception_log, $db) = @_;
Subroutine enqueue_from_failed_job: not called CC=2 SCAR=17.9
Pod undocumented
251  
252 0 ▶ if ( $exception_log->{retried} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exception_log->{"retried"}) 0 0 0
253 0 Carp::carp('this exception is already retried');
254 0 return;
255 }
256 my $args = +{
257 func_id => $exception_log->{func_id},
258 arg => $exception_log->{arg},
259 uniqkey => $exception_log->{uniqkey},
260 0 };
261  
262 0 my $job_id = $self->driver_for($db)->enqueue($args);
263  
264 0 $self->driver_for($db)->retry_from_exception_log($exception_log->{id});
265  
266 0 $self->lookup_job($job_id, $db);
267 }
268  
269 30 1;
270  
« blib/lib/Qudo.pm ↑ Summary blib/lib/Qudo/Driver/DBI/DBD.pm »