blib/lib/Qudo/Manager.pm

filter: Statementstmt 12.9% 19 / 147 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 13 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 17.6% 6 / 34 Podpod 34.6% 9 / 26 totaltotal 12.9% 34 / 262 CC 29 SCAR 25.2
CC
29
Cov
10%
register_plugins
42.0
enqueue
30.0
find_job
20.0
CRAP 12.5
« blib/lib/Qudo.pm ↑ Summary blib/lib/Qudo/Job.pm »
1 package Qudo::Manager;
2 33 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 33 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 33 ▶use Qudo::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 33 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 33 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 33 ▶use Scalar::Util qw/weaken/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 0 ▶sub new {
Subroutine new: not called CC=1 SCAR=6.9
Pod documented
10 0 my $class = shift;
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 0 weaken($self->{qudo});
26  
27 0 $self->register_hooks(@{$self->{default_hooks}});
28 0 $self->register_plugins(@{$self->{default_plugins}});
29 0 $self->register_abilities(@{$self->{abilities}});
30  
31 0 $self;
32 }
33  
34 0 ▶sub driver_for { $_[0]->{qudo}->driver_for($_[1]) }
Subroutine driver_for: not called CC=1 SCAR=6.9
Pod documented
35 0 ▶sub shuffled_databases { $_[0]->{qudo}->shuffled_databases() }
Subroutine shuffled_databases: not called CC=1 SCAR=6.9
Pod documented
36  
37 0 ▶sub plugin { $_[0]->{plugin} }
Subroutine plugin: not called CC=1 SCAR=6.9
Pod documented
38 0 ▶sub hooks { $_[0]->{hooks} }
Subroutine hooks: not called CC=1 SCAR=6.9
Pod undocumented
39  
40 0 ▶sub can_do {
Subroutine can_do: not called CC=1 SCAR=6.9
Pod undocumented
41 0 my ($self, $funcname) = @_;
42 0 $self->{func_map}->{$funcname} = 1;
43 }
44  
45 0 ▶sub register_abilities {
Subroutine register_abilities: not called CC=2 SCAR=17.9
Pod documented
46 0 my ($self, @abilities) = @_;
47  
48 0 for my $ability (@abilities) {
49 0 $self->can_do($ability);
50 }
51 }
52  
53 0 ▶sub has_abilities {
Subroutine has_abilities: not called CC=1 SCAR=6.9
Pod undocumented
54 0 keys %{$_[0]->{func_map}};
55 }
56  
57 0 ▶sub register_plugins {
Subroutine register_plugins: not called CC=6 SCAR=37.4
Pod documented
58 0 my ($self, @plugins) = @_;
59  
60 0 for my $plugin (@plugins) {
61  
62 0 my ($plugin_name, $code);
63  
64 0 ▶ if (ref $plugin eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $plugin eq 'HASH') { } 0 0 0
elsif (not ref $plugin) { } 0 0 0
65 0 my $klass = $plugin->{name};
66 0 ▶ $klass->require or Carp::croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$klass->require or Carp::croak($@) 0 0 0
67 0 ($plugin_name, $code) = $klass->load($plugin->{option});
68 } elsif (not ref $plugin) {
69 0 ▶ $plugin->require or Carp::croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$plugin->require or Carp::croak($@) 0 0 0
70 0 ($plugin_name, $code) = $plugin->load();
71 } else {
72 0 Carp::croak 'register_plugins require HASH or SCALAR.';
73 }
74  
75 0 $self->{plugin}->{$plugin_name} = $code;
76 }
77 }
78  
79 0 ▶sub call_hook {
Subroutine call_hook: not called CC=2 SCAR=17.9
Pod undocumented
80 0 my ($self, $hook_point, $args) = @_;
81  
82 0 for my $module (keys %{$self->hooks->{$hook_point}}) {
83 0 my $code = $self->hooks->{$hook_point}->{$module};
84 0 $code->($args);
85 }
86 }
87  
88 0 ▶sub register_hooks {
Subroutine register_hooks: not called CC=3 SCAR=24.8
Pod undocumented
89 0 my ($self, @hook_modules) = @_;
90  
91 0 for my $module (@hook_modules) {
92 0 ▶ $module->require or Carp::croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$module->require or Carp::croak($@) 0 0 0
93 0 $module->load($self);
94 }
95 }
96  
97 0 ▶sub global_register_hooks {
Subroutine global_register_hooks: not called CC=1 SCAR=6.9
Pod documented
98 0 warn q{global_register_hooks method is deprecated. Use 'register_hooks' instead.};
99 0 shift->register_hooks(@_);
100 }
101  
102 0 ▶sub unregister_hooks {
Subroutine unregister_hooks: not called CC=2 SCAR=17.9
Pod undocumented
103 0 my ($self, @hook_modules) = @_;
104  
105 0 for my $module (@hook_modules) {
106 0 $module->unload($self);
107 }
108 }
109  
110 0 ▶sub global_unregister_hooks {
Subroutine global_unregister_hooks: not called CC=1 SCAR=6.9
Pod documented
111 0 warn q{global_unregister_hooks method is deprecated. Use 'unregister_hooks' instead.};
112 0 shift->unregister_hooks(@_);
113 }
114  
115 0 ▶sub funcname_to_id {
Subroutine funcname_to_id: not called CC=2 SCAR=17.9
Pod undocumented
116 0 my ($self, $funcname, $db) = @_;
117  
118 0 ▶ $self->{_func_cache}->{$db}->{funcname2id}->{$funcname} or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_func_cache"}{$db}{"funcname2id"}{$funcname} or do { my $func = $self->driver_for($db)->func_from_name($funcname); $self->{"_func_cache"}{$db}{"funcname2id"}{$funcname} = $func->{"id"}; $self->{"_func_cache"}{$db}{"funcid2name"}{$func->{"id"}} = $func->{"name"} } 0 0 0
119 0 my $func = $self->driver_for($db)->func_from_name($funcname);
120 0 $self->{_func_cache}->{$db}->{funcname2id}->{$funcname} = $func->{id};
121 0 $self->{_func_cache}->{$db}->{funcid2name}->{$func->{id}} = $func->{name};
122  
123 };
124 0 $self->{_func_cache}->{$db}->{funcname2id}->{$funcname};
125 }
126  
127 0 ▶sub funcnames_to_ids {
Subroutine funcnames_to_ids: not called CC=1 SCAR=6.9
Pod undocumented
128 0 my ($self, $db) = @_;
129 [
130 map {
131 0 $self->funcname_to_id($_, $db)
132 0 } keys %{$self->{func_map}}
133 ];
134 }
135  
136 0 ▶sub funcid_to_name {
Subroutine funcid_to_name: not called CC=2 SCAR=17.9
Pod undocumented
137 0 my ($self, $funcid, $db) = @_;
138  
139 0 ▶ $self->{_func_cache}->{$db}->{funcid2name}->{$funcid} or do {
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_func_cache"}{$db}{"funcid2name"}{$funcid} or do { my $func = $self->driver_for($db)->func_from_id($funcid); $self->{"_func_cache"}{$db}{"funcname2id"}{$func->name} = $func->{"id"}; $self->{"_func_cache"}{$db}{"funcidename"}{$funcid} = $func->{"name"} } 0 0 0
140 0 my $func = $self->driver_for($db)->func_from_id($funcid);
141 0 $self->{_func_cache}->{$db}->{funcname2id}->{$func->name} = $func->{id};
142 0 $self->{_func_cache}->{$db}->{funcidename}->{$funcid} = $func->{name};
143 };
144 0 $self->{_func_cache}->{$db}->{funcid2name}->{$funcid};
145 }
146  
147 0 ▶sub enqueue {
Subroutine enqueue: not called CC=5 SCAR=34.0
Pod documented
148 0 my ($self, $funcname, $arg, $db) = @_;
149  
150 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
151 0 my $func_id = $self->funcname_to_id($funcname, $db);
152  
153 my $args = +{
154 func_id => $func_id,
155 arg => $arg->{arg},
156 uniqkey => $arg->{uniqkey},
157 run_after => $arg->{run_after}||0,
158 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
159 };
160  
161 0 $self->call_hook('pre_enqueue', $args);
162 0 $self->call_hook('serialize', $args);
163  
164 0 my $job_id = $self->driver_for($db)->enqueue($args);
165 0 ▶ return if $arg->{suppress_job};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"suppress_job"} 0 0 0
166  
167 0 my $job = $self->lookup_job($job_id, $db);
168  
169 0 $self->call_hook('post_enqueue', $job);
170  
171 0 return $job;
172 }
173  
174 0 ▶sub reenqueue {
Subroutine reenqueue: not called CC=1 SCAR=6.9
Pod undocumented
175 0 my ($self, $job, $args) = @_;
176  
177 0 my $db = $self->shuffled_databases;
178 0 $self->driver_for($db)->reenqueue($job->id, $args);
179  
180 0 return $self->lookup_job($job->id, $db);
181 }
182  
183 0 ▶sub dequeue {
Subroutine dequeue: not called CC=1 SCAR=6.9
Pod undocumented
184 0 my ($self, $job) = @_;
185 0 $self->driver_for($job->db)->dequeue({id => $job->id});
186 }
187  
188 0 ▶sub work_once {
Subroutine work_once: not called CC=3 SCAR=24.8
Pod undocumented
189 0 my $self = shift;
190  
191 0 my $job = $self->find_job;
192 0 ▶ return unless $job;
Branch: 1 branch
BranchTrueFalseTotal
unless $job 0 0 0
193  
194 0 my $worker_class = $job->funcname;
195 0 ▶ return unless $worker_class;
Branch: 1 branch
BranchTrueFalseTotal
unless $worker_class 0 0 0
196  
197 0 $self->call_hook('deserialize', $job);
198 0 $self->call_hook('pre_work', $job);
199  
200 0 $worker_class->work_safely($job);
201  
202 0 $self->call_hook('post_work', $job);
203  
204 0 return 1;
205 }
206  
207 0 ▶sub lookup_job {
Subroutine lookup_job: not called CC=2 SCAR=17.9
Pod undocumented
208 0 my ($self, $job_id, $db) = @_;
209  
210 0 my $callback = $self->driver_for($db)->lookup_job($job_id);
211 0 my $job_data = $callback->();
212 0 ▶ return $job_data ? $self->_data2job($job_data, $db) : undef;
Branch: 1 branch
BranchTrueFalseTotal
$job_data ? : 0 0 0
213 }
214  
215 0 ▶sub find_job {
Subroutine find_job: not called CC=4 SCAR=30.0
Pod undocumented
216 0 my $self = shift;
217  
218 0 for my $db ($self->shuffled_databases) {
219 0 ▶ return unless keys %{$self->{func_map}};
Branch: 1 branch
BranchTrueFalseTotal
unless keys %{$$self{"func_map"};} 0 0 0
220 0 my $func_ids = $self->funcnames_to_ids($db);
221 0 my $callback = $self->driver_for($db)->find_job($self->{find_job_limit_size}, $func_ids);
222  
223 0 my $job = $self->_grab_a_job($callback, $db);
224 0 ▶ return $job if defined $job;
Branch: 1 branch
BranchTrueFalseTotal
if defined $job 0 0 0
225 }
226 }
227  
228 0 ▶sub _data2job {
Subroutine _data2job: not called CC=1 SCAR=6.9
229 0 my ($self, $job_data, $db) = @_;
230  
231 0 Qudo::Job->new(
232 manager => $self,
233 job_data => $job_data,
234 db => $db,
235 );
236 }
237  
238 0 ▶sub _grab_a_job {
Subroutine _grab_a_job: not called CC=4 SCAR=30.0
239 0 my ($self, $callback, $db) = @_;
240  
241 0 while (1) {
242 0 my $job_data = $callback->();
243 0 ▶ last unless $job_data;
Branch: 1 branch
BranchTrueFalseTotal
unless $job_data 0 0 0
244  
245 0 my $old_grabbed_until = $job_data->{job_grabbed_until};
246 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
247 or die "expected a server time";
248  
249 0 my $worker_class = $self->funcid_to_name($job_data->{func_id}, $db);
250 my $grab_job = $self->driver_for($db)->grab_a_job(
251 grabbed_until => ($server_time + $worker_class->grab_for),
252 job_id => $job_data->{job_id},
253 0 old_grabbed_until => $old_grabbed_until,
254 );
255 0 ▶ next if $grab_job < 1;
Branch: 1 branch
BranchTrueFalseTotal
if $grab_job < 1 0 0 0
256  
257 0 return $self->_data2job($job_data, $db);
258 }
259 0 return;
260 }
261  
262 0 ▶sub job_failed {
Subroutine job_failed: not called CC=2 SCAR=17.9
Pod undocumented
263 0 my ($self, $job, $message) = @_;
264  
265 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
266 {
267 func_id => $job->func_id,
268 message => $message,
269 uniqkey => $job->uniqkey,
270 arg => $job->arg_origin || $job->arg,
271 }
272 );
273 }
274  
275 0 ▶sub set_job_status {
Subroutine set_job_status: not called CC=2 SCAR=17.9
Pod undocumented
276 0 my ($self, $job, $status) = @_;
277  
278 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
279 {
280 func_id => $job->func_id,
281 arg => $job->arg_origin || $job->arg,
282 uniqkey => $job->uniqkey,
283 status => $status,
284 job_start_time => $job->job_start_time,
285 job_end_time => time(),
286 }
287 );
288 }
289  
290 0 ▶sub enqueue_from_failed_job {
Subroutine enqueue_from_failed_job: not called CC=2 SCAR=17.9
Pod undocumented
291 0 my ($self, $exception_log, $db) = @_;
292  
293 0 ▶ if ( $exception_log->{retried} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($exception_log->{"retried"}) 0 0 0
294 0 Carp::carp('this exception is already retried');
295 0 return;
296 }
297 my $args = +{
298 func_id => $exception_log->{func_id},
299 arg => $exception_log->{arg},
300 uniqkey => $exception_log->{uniqkey},
301 0 };
302  
303 0 my $job_id = $self->driver_for($db)->enqueue($args);
304  
305 0 $self->driver_for($db)->retry_from_exception_log($exception_log->{id});
306  
307 0 $self->lookup_job($job_id, $db);
308 }
309  
310 33 1;
311  
312 =head1 NAME
313  
314 Qudo::Manager - qudo manager class.
315  
316 =head1 DESCRIPTION
317  
318 Qudo::Manager is job managiment base class.
319 It the job enqueue, dequeue, lookup and more.
320  
321 =head1 METHODS
322  
323 =head2 new
324  
325 get Qudo::Manager instance.
326 It is called from L<Qudo> usually.
327  
328 =head2 driver_for
329  
330 get database driver from some databases.
331  
332 =head2 shuffled_databases
333  
334 get shuffled databases.
335  
336 =head2 plugin
337  
338 get plugin instances.
339  
340 =head2 register_abilities
341  
342 The function that this manager is processing is registered.
343  
344 =head2 register_plugins
345  
346 Plugin is set for manager.
347  
348 =head2 global_register_hooks
349  
350 Hooks are set for manager.
351  
352 =head2 global_unregister_hooks
353  
354 Hooks are unset from manager.
355  
356 =head2 C<Qudo-E<gt>enqueue( $funcname, $args )>
357  
358 enqueue the job.
359  
360 =over 4
361  
362 =item * C<$funcname>
363  
364 =item * C<$args>
365  
366 =over 4
367  
368 =item * C<arg>
369  
370 job argments.
371  
372 =item * C<uniqkey>
373  
374 job unique key.
375  
376 =item * C<run_after>
377  
378 the value you want to check <= against on the run_after column
379 require int value.
380  
381 =item * C<priority>
382  
383 job priority.
384 require int value.
385  
386 =back
387  
388 =back
389  
390 =cut
391  
« blib/lib/Qudo.pm ↑ Summary blib/lib/Qudo/Job.pm »