blib/lib/Qudo/Driver/DBI.pm

filter: Statementstmt 11.4% 30 / 263 Branchbran 0.0% 0 / 64 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 24.1% 7 / 29 Podpod 0.0% 0 / 17 totaltotal 9.3% 37 / 396 CC 41 SCAR 30.8
CC
41
Cov
9%
enqueue
72.0
exception_list
56.0
job_status_list
20.0
CRAP 21.7
↑ Summary blib/lib/Qudo/Test.pm »
1 package Qudo::Driver::DBI;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Qudo::Driver::DBI::DBD;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub init_driver {
11 0 ▶ my ($class, $master) = @_;
Subroutine init_driver: not called CC=2 SCAR=17.9
Pod undocumented
12  
13 0 for my $database (@{$master->{databases}}) {
14 0 my $connection = bless {
15 database => $database,
16 dbh => '',
17 dbd => '',
18 }, $class;
19 0 $connection->_connect();
20  
21 0 my $dbd_type = $connection->{dbh}->{Driver}->{Name};
22 0 $connection->{dbd} = Qudo::Driver::DBI::DBD->new($dbd_type);
23  
24 0 $master->set_connection($database->{dsn}, $connection);
25 }
26 }
27  
28 sub _connect {
29 0 ▶ my $self = shift;
Subroutine _connect: not called CC=2 SCAR=17.9
30
31 $self->{dbh} = DBI->connect(
32 $self->{database}->{dsn},
33 $self->{database}->{username},
34 $self->{database}->{password},
35 0 ▶ { RaiseError => 1, PrintError => 0, AutoCommit => 1, %{ $self->{database}->{connect_options} || {} } }
Branch: 1 branch
BranchTrueFalseTotal
$self->{"database"}{"connect_options"} or {} 0 0 0
36 );
37 }
38  
39 sub job_status_list {
40 0 ▶ my ($self, $args) = @_;
Subroutine job_status_list: not called CC=4 SCAR=30.0
Pod undocumented
41  
42 0 my $sql = q{
43 SELECT
44 job_status.id,
45 job_status.func_id,
46 job_status.arg,
47 job_status.uniqkey,
48 job_status.status,
49 job_status.job_start_time,
50 job_status.job_end_time
51 FROM
52 job_status
53 };
54  
55 0 my @funcs;
56 0 ▶ if( $args->{funcs} ){
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"funcs"}) 0 0 0
57 0 ▶ if( ref($args->{funcs}) eq 'ARRAY' ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $args->{'funcs'} eq 'ARRAY') { } 0 0 0
58 0 @funcs = @{$args->{funcs}};
59 }
60 else{
61 0 push @funcs , $args->{funcs};
62 }
63  
64 $sql .= sprintf( q{
65 INNER JOIN
66 func
67 ON
68 job_status.func_id = func.id
69 0 WHERE (func.name IN (%s) )}, join(',',map{'?'} @funcs) );
70 }
71  
72 0 $sql .= q{ LIMIT ? OFFSET ? };
73  
74 my $sth = $self->_execute(
75 $sql,
76 0 [ @funcs , $args->{limit} ,$args->{offset} ]
77 );
78  
79 0 my @job_status_list;
80 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
81 0 push @job_status_list, $row;
82 }
83 0 return \@job_status_list;
84 }
85  
86 sub job_count {
87 0 ▶ my ($self , $funcs) = @_;
Subroutine job_count: not called CC=3 SCAR=24.8
Pod undocumented
88  
89 0 my $sql = q{
90 SELECT
91 COUNT(job.id) AS count
92 FROM
93 job, func
94 WHERE
95 job.func_id = func.id
96 };
97 0 ▶ if( $funcs ){
Branch: 1 branch
BranchTrueFalseTotal
if ($funcs) 0 0 0
98 0 $sql .= q{ AND }. $self->_join_func_name($funcs);
99 }
100  
101 0 my $sth = $self->{dbh}->prepare( $sql );
102  
103 0 eval{
104 0 $sth->execute( @{$funcs} );
105 };
106 0 ▶ if( my $e = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
107 0 croak 'job_count ERROR'.$e;
108 }
109 0 my $ret = $sth->fetchrow_hashref();
110 0 return $ret->{count};
111 }
112  
113 sub job_list {
114 0 ▶ my ($self, $limit, $funcs) = @_;
Subroutine job_list: not called CC=4 SCAR=30.0
Pod undocumented
115  
116 0 my $sql = $self->_search_job_sql();
117 0 $sql .= q{
118 WHERE
119 job.grabbed_until <= ?
120 AND
121 job.run_after <= ?
122 };
123 0 my @bind = $self->get_server_time;
124 0 push @bind, $self->get_server_time;
125  
126 # func.name
127 0 ▶ if( $funcs ){
Branch: 1 branch
BranchTrueFalseTotal
if ($funcs) 0 0 0
128 0 $sql .= q{ AND }. $self->_join_func_name($funcs);
129 0 push @bind , @{$funcs};
130 }
131  
132 # limit
133 0 $sql .= q{LIMIT ?};
134 0 push @bind , $limit;
135  
136 0 my $sth = $self->{dbh}->prepare( $sql );
137  
138 0 eval{
139 0 $sth->execute( @bind );
140 };
141 0 ▶ if( my $e = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
142 0 croak 'job_list ERROR'.$e;
143 }
144  
145 0 my $code = $self->_get_job_data( $sth );
146  
147 0 my @jobs;
148 0 while (1) {
149 0 my $row = $code->();
150 0 ▶ last unless $row;
Branch: 1 branch
BranchTrueFalseTotal
unless $row 0 0 0
151 0 push @jobs, $row;
152 }
153 0 return \@jobs;
154 }
155  
156 sub exception_list {
157 0 ▶ my ($self, $args) = @_;
Subroutine exception_list: not called CC=7 SCAR=40.3
Pod undocumented
158  
159 0 my @bind = ();
160 0 my $limit = $args->{limit};
161 0 my $offset = $args->{offset};
162 0 ▶ my $funcs = $args->{funcs} || '';
Condition0%0/2
$args->{'funcs'} || ''
l!l
00
MC/DC0%0/1
$args->{'funcs'} || ''
$args->{'funcs'}
Truth table0%0/2
A
A= $args->{'funcs'}
Aresult
11
00
163 0 my $sql = q{
164 SELECT
165 exception_log.id,
166 exception_log.func_id,
167 exception_log.exception_time,
168 exception_log.message,
169 exception_log.uniqkey,
170 exception_log.arg,
171 exception_log.retried
172 FROM
173 exception_log
174 };
175  
176 # funcs
177 0 ▶ if ($funcs) {
Branch: 1 branch
BranchTrueFalseTotal
if ($funcs) 0 0 0
178 0 $sql .= q{
179 INNER JOIN
180 func
181 ON
182 exception_log.func_id = func.id
183 WHERE
184 };
185 0 $sql .= $self->_join_func_name($funcs);
186 0 push @bind , @{$funcs};
187 }
188  
189 # limit
190 0 ▶ if( $limit ){
Branch: 1 branch
BranchTrueFalseTotal
if ($limit) 0 0 0
191 0 $sql .= q{ LIMIT ? };
192 0 push @bind , $limit;
193 }
194  
195 #offset
196 0 ▶ if( $offset ){
Branch: 1 branch
BranchTrueFalseTotal
if ($offset) 0 0 0
197 0 $sql .= q{OFFSET ?};
198 0 push @bind , $offset;
199 }
200  
201 0 my $sth = $self->{dbh}->prepare( $sql );
202 0 eval{
203 0 $sth->execute( @bind );
204 };
205 0 ▶ if( my $e = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
206 0 croak 'exception_list ERROR'.$e;
207 }
208 0 my @exception_list;
209 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
210 0 push @exception_list, $row;
211 }
212 0 return \@exception_list;
213 }
214  
215  
216 sub lookup_job {
217 0 ▶ my ($self, $job_id) = @_;
Subroutine lookup_job: not called CC=3 SCAR=24.8
Pod undocumented
218  
219 0 my $sql = $self->_search_job_sql();
220  
221 0 my @bind;
222 # func.name
223 0 ▶ if( $job_id ){
Branch: 1 branch
BranchTrueFalseTotal
if ($job_id) 0 0 0
224 0 $sql .= q{ WHERE job.id = ?};
225 0 push @bind , $job_id;
226 }
227  
228 # limit
229 0 $sql .= q{LIMIT 1};
230  
231 0 my $sth = $self->{dbh}->prepare( $sql );
232  
233 0 eval{
234 0 $sth->execute( @bind );
235 };
236 0 ▶ if( my $e = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
237 0 croak 'lookup_job ERROR'.$e;
238 }
239  
240 0 return $self->_get_job_data( $sth );
241 }
242  
243 sub find_job {
244 0 ▶ my ($self, $limit, $func_map) = @_;
Subroutine find_job: not called CC=3 SCAR=24.8
Pod undocumented
245  
246 0 my $sql = $self->_search_job_sql();
247 0 $sql .= q{
248 WHERE
249 job.grabbed_until <= ?
250 AND
251 job.run_after <= ?
252 };
253 0 my @bind = $self->get_server_time;
254 0 push @bind, $self->get_server_time;
255  
256 # func.name
257 0 ▶ if( $func_map ){
Branch: 1 branch
BranchTrueFalseTotal
if ($func_map) 0 0 0
258 0 my $keys = [keys %$func_map];
259 0 $sql .= q{ AND }. $self->_join_func_name($keys);
260 0 push @bind , @{$keys};
261 }
262  
263 # priority
264 0 $sql .= q{ ORDER BY job.priority DESC };
265  
266 # limit
267 0 $sql .= q{ LIMIT ? };
268 0 push @bind , $limit;
269  
270 0 my $sth = $self->{dbh}->prepare( $sql );
271  
272 0 eval{
273 0 $sth->execute( @bind );
274 };
275 0 ▶ if( my $e = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
276 0 croak 'find_job ERROR'.$e;
277 }
278  
279 0 return $self->_get_job_data( $sth );
280 }
281  
282 sub _search_job_sql {
283 0 ▶ q{
Subroutine _search_job_sql: not called CC=1 SCAR=6.9
284 SELECT
285 job.id AS id,
286 job.arg AS arg,
287 job.uniqkey AS uniqkey,
288 job.func_id AS func_id,
289 job.grabbed_until AS grabbed_until,
290 job.retry_cnt AS retry_cnt,
291 job.priority AS priority,
292 func.name AS funcname
293 FROM job
294 INNER JOIN
295 func ON job.func_id = func.id
296 };
297 }
298  
299 sub _get_job_data {
300 0 ▶ my ($self, $sth) = @_;
Subroutine _get_job_data: not called CC=1 SCAR=6.9
301 sub{
302 0 ▶ while (my $row = $sth->fetchrow_hashref) {
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
if (my $row = $sth->fetchrow_hashref) 0 0 0
303 return +{
304 job_id => $row->{id},
305 job_arg => $row->{arg},
306 job_uniqkey => $row->{uniqkey},
307 job_grabbed_until => $row->{grabbed_until},
308 job_retry_cnt => $row->{retry_cnt},
309 job_priority => $row->{priority},
310 func_id => $row->{func_id},
311 func_name => $row->{funcname},
312 0 };
313 }
314 0 return;
315 0 };
316 }
317  
318 sub grab_a_job {
319 0 ▶ my ($self, %args) = @_;
Subroutine grab_a_job: not called CC=2 SCAR=17.9
Pod undocumented
320  
321 my $sth = $self->{dbh}->prepare(
322 0 q{
323 UPDATE
324 job
325 SET
326 grabbed_until = ?
327 WHERE
328 id = ?
329 AND
330 grabbed_until = ?
331 }
332 );
333  
334 0 my $rows;
335 0 eval{
336 $rows = $sth->execute(
337 $args{grabbed_until},
338 $args{job_id},
339 $args{old_grabbed_until},
340 0 );
341 };
342 0 ▶ if( my $e = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
343 0 croak 'grab_a_job ERROR'.$e;
344 0 return;
345 }
346  
347 0 return $rows;
348 }
349  
350 sub logging_exception {
351 0 ▶ my ($self, $args) = @_;
Subroutine logging_exception: not called CC=2 SCAR=17.9
Pod undocumented
352  
353 my $sth = $self->{dbh}->prepare(
354 0 q{
355 INSERT INTO exception_log
356 ( func_id , message , uniqkey, arg, exception_time, retried)
357 VALUES
358 ( ? , ? , ?, ?, ?, ?)
359 }
360 );
361  
362 0 eval{
363 $sth->execute(
364 $args->{func_id} ,
365 $args->{message} ,
366 $args->{uniqkey} ,
367 $args->{arg} ,
368 0 time(),
369 0,
370 );
371 };
372 0 ▶ if( my $e = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
373 0 croak 'logging_exception ERROR'.$e;
374 }
375 0 return;
376 }
377  
378 sub set_job_status{
379 0 ▶ my ($self, $args) = @_;
Subroutine set_job_status: not called CC=1 SCAR=6.9
Pod undocumented
380  
381 0 my @column = keys %{$args};
382 0 my $sql = $self->_build_insert_sql(
383 'job_status',
384 \@column
385 );
386  
387 0 my @bind = map {$args->{$_}} @column;
388  
389 0 $self->_execute(
390 $sql,
391 \@bind
392 );
393  
394 0 return;
395 }
396  
397 sub get_server_time {
398 0 ▶ my $self = shift;
Subroutine get_server_time: not called CC=2 SCAR=17.9
Pod undocumented
399  
400 0 my $unixtime_sql = $self->{dbd}->sql_for_unixtime;
401 0 my $time;
402 0 eval {
403 0 $time = $self->{dbh}->selectrow_array("SELECT $unixtime_sql");
404 };
405 0 ▶ if ($@) { $time = time }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
406 0 return $time;
407 }
408  
409 sub enqueue {
410 0 ▶ my ($self, $args) = @_;
Subroutine enqueue: not called CC=8 SCAR=42.8
Pod undocumented
411  
412 0 ▶ $args->{enqueue_time} ||= time;
Condition0%0/3
$args->{"enqueue_time"} ||= time
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"enqueue_time"} ||= time
$args->{"enqueue_time"} time
Truth table0%0/3
A ||= B
A= $args->{"enqueue_time"}B= time
ABresult
1X1
011
000
413 0 ▶ $args->{grabbed_until} ||= 0;
Condition0%0/2
$args->{"grabbed_until"} ||= 0
l!l
00
MC/DC0%0/1
$args->{"grabbed_until"} ||= 0
$args->{"grabbed_until"}
Truth table0%0/2
A
A= $args->{"grabbed_until"}
Aresult
11
00
414 0 ▶ $args->{retry_cnt} ||= 0;
Condition0%0/2
$args->{"retry_cnt"} ||= 0
l!l
00
MC/DC0%0/1
$args->{"retry_cnt"} ||= 0
$args->{"retry_cnt"}
Truth table0%0/2
A
A= $args->{"retry_cnt"}
Aresult
11
00
415 0 ▶ $args->{run_after} = time + ($args->{run_after}||0);
Condition0%0/2
$args->{'run_after'} || 0
l!l
00
MC/DC0%0/1
$args->{'run_after'} || 0
$args->{'run_after'}
Truth table0%0/2
A
A= $args->{'run_after'}
Aresult
11
00
416 0 ▶ $args->{priority} ||= 0;
Condition0%0/2
$args->{"priority"} ||= 0
l!l
00
MC/DC0%0/1
$args->{"priority"} ||= 0
$args->{"priority"}
Truth table0%0/2
A
A= $args->{"priority"}
Aresult
11
00
417  
418 0 my @column = keys %{$args};
419 0 my $sql = $self->_build_insert_sql(
420 'job',
421 \@column
422 );
423  
424 0 my $sth_ins = $self->{dbh}->prepare( $sql );
425 0 my @bind = map {$args->{$_}} @column;
426 0 eval{
427 0 $sth_ins->execute( @bind );
428 };
429 0 ▶ if( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
430 0 croak 'enqueue ERROR'.$@;
431 }
432  
433 0 my $id = $self->{dbd}->last_insert_id($self->{dbh}, $sth_ins);
434 my $sth_sel = $self->{dbh}->prepare(
435 0 q{SELECT * FROM job WHERE id = ?}
436 );
437  
438 0 $sth_sel->execute( $id );
439 0 my $ret_sel = $sth_sel->fetchrow_hashref();
440 0 ▶ return $ret_sel ? $ret_sel->{id} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ret_sel ? : 0 0 0
441 }
442  
443 sub reenqueue {
444 0 ▶ my ($self, $job_id, $args) = @_;
Subroutine reenqueue: not called CC=3 SCAR=24.8
Pod undocumented
445  
446 my $sth = $self->{dbh}->prepare(
447 0 q{
448 UPDATE
449 job
450 SET
451 enqueue_time = ?,
452 run_after = ?,
453 retry_cnt = ?
454 WHERE
455 id = ?
456 }
457 );
458  
459 0 my $row;
460 0 eval{
461 $row = $sth->execute(
462 time,
463 (time + ($args->{retry_delay}||0) ),
464 $args->{retry_cnt},
465 0 ▶ $job_id,
Condition0%0/2
$args->{'retry_delay'} || 0
l!l
00
MC/DC0%0/1
$args->{'retry_delay'} || 0
$args->{'retry_delay'}
Truth table0%0/2
A
A= $args->{'retry_delay'}
Aresult
11
00
466 );
467 };
468 0 ▶ if( my $e = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
469 0 croak 'reenqueue ERROR'.$e;
470 0 return;
471 }
472  
473 0 return $row;
474 }
475  
476  
477 sub dequeue {
478 0 ▶ my ($self, $args) = @_;
Subroutine dequeue: not called CC=2 SCAR=17.9
Pod undocumented
479 my $sth = $self->{dbh}->prepare(
480 0 q{DELETE FROM job WHERE id = ?}
481 );
482  
483 0 my $row;
484 0 eval{
485 0 $row = $sth->execute( $args->{id} );
486 };
487 0 ▶ if( my $e = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
488 0 croak 'dequeue ERROR'.$e;
489 }
490  
491 0 return $row;
492 }
493  
494  
495 sub get_func_id {
496 0 ▶ my ($self, $funcname) = @_;
Subroutine get_func_id: not called CC=4 SCAR=30.0
Pod undocumented
497
498 my $sth_sel = $self->{dbh}->prepare(
499 0 q{SELECT * FROM func WHERE name = ?}
500 );
501  
502 0 $sth_sel->execute( $funcname );
503 0 my $func_id;
504 0 my $ret_hashref = $sth_sel->fetchrow_hashref();
505 0 ▶ if ( $ret_hashref ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_hashref) { } 0 0 0
506 0 $func_id = $ret_hashref->{id};
507 }
508 else{
509 my $sth_ins = $self->{dbh}->prepare(
510 0 q{INSERT INTO func ( name ) VALUES ( ? )}
511 );
512 0 eval{
513 0 $sth_ins->execute( $funcname );
514 };
515 0 ▶ if( my $e = $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 0 0
516 0 croak $e;
517 }
518 0 $sth_sel->execute( $funcname );
519 0 my $ret_hashref = $sth_sel->fetchrow_hashref();
520 0 ▶ if ( $ret_hashref ){
Branch: 1 branch
BranchTrueFalseTotal
if ($ret_hashref) 0 0 0
521 0 $func_id = $ret_hashref->{id};
522 }
523 }
524  
525 0 return $func_id;
526 }
527  
528 sub get_func_name {
529 0 ▶ my ($self, $funcid) = @_;
Subroutine get_func_name: not called CC=3 SCAR=24.8
Pod undocumented
530  
531 0 my $sth;
532 0 eval {
533 $sth = $self->{dbh}->prepare(
534 0 q{SELECT * FROM func WHERE id = ?}
535 );
536 0 $sth->execute( $funcid );
537 };
538 0 ▶ if ($@) { croak $@ }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
539  
540 0 my $ret_hashref = $sth->fetchrow_hashref();
541 0 ▶ return $ret_hashref ? $ret_hashref->{name} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$ret_hashref ? : 0 0 0
542 }
543  
544 sub retry_from_exception_log {
545 0 ▶ my ($self, $exception_log_id) = @_;
Subroutine retry_from_exception_log: not called CC=1 SCAR=6.9
Pod undocumented
546  
547 0 $self->_execute(
548 q{UPDATE exception_log SET retried = 1 WHERE id = ?},
549 [$exception_log_id]
550 );
551 }
552  
553 sub _execute {
554 0 ▶ my ($self, $sql, $bind) = @_;
Subroutine _execute: not called CC=2 SCAR=17.9
555  
556 0 my $sth;
557 0 eval {
558 0 $sth = $self->{dbh}->prepare($sql);
559 0 $sth->execute(@{$bind});
560 };
561 0 ▶ if ($@) { croak $@ }
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
562 0 $sth;
563 }
564  
565 sub _join_func_name{
566 3 ▶ my ($self , $funcs ) = @_;
Subroutine _join_func_name: called CC=1 SCAR=0.0
567  
568 my $func_name = sprintf(
569 q{ func.name IN (%s) } ,
570 3 join(',', map { '?' } @{$funcs} )
571 );
572  
573 3 return $func_name;
574 }
575  
576 sub _build_insert_sql{
577 2 ▶ my( $self , $table , $column_ary_ref ) = @_;
Subroutine _build_insert_sql: called CC=1 SCAR=0.0
578  
579 2 my $sql = qq{ INSERT INTO $table ( };
580 2 $sql .= join ' , ' , @{$column_ary_ref};
581 2 $sql .= ' ) VALUES ( ';
582 2 $sql .= join( ' , ', ('?') x @{$column_ary_ref} );
583 2 $sql .= ' )';
584  
585 2 return $sql;
586 }
587  
588 1 1;
589  
590  
↑ Summary blib/lib/Qudo/Test.pm »