blib/lib/Qudo.pm

filter: Statementstmt 30.6% 27 / 88 Branchbran 0.0% 0 / 12 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 27.2% 6 / 22 Podpod 0.0% 0 / 13 totaltotal 19.6% 33 / 168 CC 17 SCAR 25.3
CC
17
Cov
22%
exception_list
30.0
job_status_list
30.0
work
20.0
CRAP 12.5
« lib/Qudo/Worker.pm ↑ Summary blib/lib/Qudo/Manager.pm »
1 package Qudo;
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  
5 30 our $VERSION = '0.01_03';
6  
7 30 ▶use Qudo::Manager;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 30 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 30 ▶use UNIVERSAL::require;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 30 ▶use List::Util qw/shuffle/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 30 our $RETRY_SECONDS = 30;
13 30 our $FIND_JOB_LIMIT_SIZE = 30;
14 30 our $DEFAULT_DRIVER = 'Skinny';
15 30 our $EXCEPTION_LIMIT_SIZE = 10;
16 30 our $EXCEPTION_OFFSET_SIZE = 0;
17 30 our $JOB_STATUS_LIMIT_SIZE = 10;
18 30 our $JOB_STATUS_OFFSET_SIZE = 0;
19  
20 sub new {
21 0 ▶ my $class = shift;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
22  
23 0 my $self = bless {
24 retry_seconds => $RETRY_SECONDS,
25 find_job_limit_size => $FIND_JOB_LIMIT_SIZE,
26 driver_class => $DEFAULT_DRIVER,
27 default_hooks => [],
28 default_plugins => [],
29 manager => '',
30 manager_abilities => [],
31 databases => [],
32 connections => +{},
33 @_,
34 }, $class;
35  
36 0 $self->_setup_driver;
37  
38 0 return $self;
39 }
40  
41 sub _setup_driver {
42 0 ▶ my $self = shift;
Subroutine _setup_driver: not called CC=2 SCAR=17.9
43  
44 0 my $driver = 'Qudo::Driver::' . $self->{driver_class};
45 0 ▶ $driver->use or Carp::croak $@;
Branch: 1 branch
BranchTrueFalseTotal
$driver->use or Carp::croak($@) 0 0 0
46 0 $driver->init_driver($self);
47 }
48  
49 sub set_connection {
50 0 ▶ my ($self, $dsn, $connection) = @_;
Subroutine set_connection: not called CC=1 SCAR=6.9
Pod undocumented
51 0 $self->{connections}->{$dsn} = $connection;
52 }
53 sub get_connection {
54 0 ▶ my ($self, $dsn) = @_;
Subroutine get_connection: not called CC=1 SCAR=6.9
Pod undocumented
55 0 $self->{connections}->{$dsn};
56 }
57  
58 sub shuffled_databases {
59 0 ▶ my $self = shift;
Subroutine shuffled_databases: not called CC=1 SCAR=6.9
Pod undocumented
60 0 my @dsns = keys %{$self->{connections}};
61 0 return shuffle(@dsns);
62 }
63  
64 sub driver {
65 0 ▶ my ($self, $dsn) = @_;
Subroutine driver: not called CC=2 SCAR=17.9
Pod undocumented
66 0 ▶ $dsn ||= $self->shuffled_databases;
Condition0%0/3
$dsn ||= $self->shuffled_databases
l!l&&r!l&&!r
000
MC/DC0%0/2
$dsn ||= $self->shuffled_databases
$dsn $self->shuffled_databases
Truth table0%0/3
A ||= B
A= $dsnB= $self->shuffled_databases
ABresult
1X1
011
000
67 0 $self->driver_for($dsn);
68 }
69  
70 sub driver_for {
71 0 ▶ my ($self, $dsn) = @_;
Subroutine driver_for: not called CC=1 SCAR=6.9
Pod undocumented
72 0 $self->get_connection($dsn);
73 }
74  
75 sub manager {
76 0 ▶ my $self = shift;
Subroutine manager: not called CC=2 SCAR=17.9
Pod undocumented
77  
78 $self->{manager} ||= Qudo::Manager->new(
79 0 ▶ driver_for => sub { $self->driver_for(+shift) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
80 0 ▶ shuffled_databases => sub { $self->shuffled_databases },
Subroutine __ANON__: not called CC=1 SCAR=6.9
81 find_job_limit_size => $self->{find_job_limit_size},
82 retry_seconds => $self->{retry_seconds},
83 default_hooks => $self->{default_hooks},
84 default_plugins => $self->{default_plugins},
85 abilities => $self->{manager_abilities},
86 0 ▶ );
Condition0%0/3
$self->{"manager"} ||= "Qudo::Manager"->new("driver_for", sub { $self->driver_for(shift()); } , "shuffled_databases", sub { $self->shuffled_databases; } , "find_job_limit_size", $self->{"find_job_limit_size"}, "retry_seconds", $self->{"retry_seconds"}, "default_hooks", $self->{"default_hooks"}, "default_plugins", $self->{"default_plugins"}, "abilities", $self->{"manager_abilities"})
l!l&&r!l&&!r
000
MC/DC0%0/2
$self->{"manager"} ||= "Qudo::Manager"->new("driver_for", sub { $self->driver_for(shift()); } , "shuffled_databases", sub { $self->shuffled_databases; } , "find_job_limit_size", $self->{"find_job_limit_size"}, "retry_seconds", $self->{"retry_seconds"}, "default_hooks", $self->{"default_hooks"}, "default_plugins", $self->{"default_plugins"}, "abilities", $self->{"manager_abilities"})
$self->{"manager"} "Qudo::Manager"->new("driver_for", sub { $self->driver_for(shift()); } , "shuffled_databases", sub { $self->shuffled_databases; } , "find_job_limit_size", $self->{"find_job_limit_size"}, "retry_seconds", $self->{"retry_seconds"}, "default_hooks", $self->{"default_hooks"}, "default_plugins", $self->{"default_plugins"}, "abilities", $self->{"manager_abilities"})
Truth table0%0/3
A ||= B
A= $self->{"manager"}B= "Qudo::Manager"->new("driver_for", sub { $self->driver_for(shift()); } , "shuffled_databases", sub { $self->shuffled_databases; } , "find_job_limit_size", $self->{"find_job_limit_size"}, "retry_seconds", $self->{"retry_seconds"}, "default_hooks", $self->{"default_hooks"}, "default_plugins", $self->{"default_plugins"}, "abilities", $self->{"manager_abilities"})
ABresult
1X1
011
000
87 }
88  
89 sub enqueue {
90 0 ▶ my $self = shift;
Subroutine enqueue: not called CC=1 SCAR=6.9
Pod undocumented
91 0 $self->manager->enqueue(@_);
92 }
93  
94 sub work {
95 0 ▶ my ($self, $work_delay) = @_;
Subroutine work: not called CC=4 SCAR=30.0
Pod undocumented
96 0 ▶ $work_delay ||= 5;
Condition0%0/2
$work_delay ||= 5
l!l
00
MC/DC0%0/1
$work_delay ||= 5
$work_delay
Truth table0%0/2
A
A= $work_delay
Aresult
11
00
97  
98 0 my $manager = $self->manager;
99 0 ▶ unless ($manager->has_abilities) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($manager->has_abilities) 0 0 0
100 0 Carp::croak 'manager dose not have abilities.';
101 }
102  
103 0 while (1) {
104 0 ▶ sleep $work_delay unless $manager->work_once;
Branch: 1 branch
BranchTrueFalseTotal
unless $manager->work_once 0 0 0
105 }
106 }
107  
108 sub job_list {
109 0 ▶ my ($self, $funcs) = @_;
Subroutine job_list: not called CC=1 SCAR=6.9
Pod undocumented
110  
111 0 return $self->driver->job_list($self->{find_job_limit_size}, $funcs);
112 }
113  
114 sub job_count {
115 0 ▶ my ($self, $funcs, $dsn) = @_;
Subroutine job_count: not called CC=3 SCAR=24.8
Pod undocumented
116  
117 0 ▶ if ($dsn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn) 0 0 0
118 0 return $self->driver_for($dsn)->job_count($funcs);
119 }
120  
121 0 my %job_count;
122 0 for my $db ($self->shuffled_databases) {
123 0 $job_count{$db} = $self->driver_for($db)->job_count($funcs);
124 }
125 0 return \%job_count;
126 }
127  
128 sub exception_list {
129 0 ▶ my ($self, $args, $dsn) = @_;
Subroutine exception_list: not called CC=5 SCAR=34.0
Pod undocumented
130  
131 0 ▶ $args->{limit} ||= $EXCEPTION_LIMIT_SIZE;
Condition0%0/3
$args->{"limit"} ||= $Qudo::EXCEPTION_LIMIT_SIZE
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"limit"} ||= $Qudo::EXCEPTION_LIMIT_SIZE
$args->{"limit"} $Qudo::EXCEPTION_LIMIT_SIZE
Truth table0%0/3
A ||= B
A= $args->{"limit"}B= $Qudo::EXCEPTION_LIMIT_SIZE
ABresult
1X1
011
000
132 0 ▶ $args->{offset} ||= $EXCEPTION_OFFSET_SIZE;
Condition0%0/3
$args->{"offset"} ||= $Qudo::EXCEPTION_OFFSET_SIZE
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"offset"} ||= $Qudo::EXCEPTION_OFFSET_SIZE
$args->{"offset"} $Qudo::EXCEPTION_OFFSET_SIZE
Truth table0%0/3
A ||= B
A= $args->{"offset"}B= $Qudo::EXCEPTION_OFFSET_SIZE
ABresult
1X1
011
000
133  
134 0 ▶ if ($dsn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn) 0 0 0
135 0 return $self->driver_for($dsn)->exception_list($args);
136 }
137  
138 0 my %exception_list;
139 0 for my $db ($self->shuffled_databases) {
140 0 $exception_list{$db} = $self->driver_for($db)->exception_list($args);
141 }
142 0 return \%exception_list;
143 }
144  
145 sub job_status_list {
146 0 ▶ my ($self, $args, $dsn) = @_;
Subroutine job_status_list: not called CC=5 SCAR=34.0
Pod undocumented
147  
148 0 ▶ $args->{limit} ||= $JOB_STATUS_LIMIT_SIZE;
Condition0%0/3
$args->{"limit"} ||= $Qudo::JOB_STATUS_LIMIT_SIZE
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"limit"} ||= $Qudo::JOB_STATUS_LIMIT_SIZE
$args->{"limit"} $Qudo::JOB_STATUS_LIMIT_SIZE
Truth table0%0/3
A ||= B
A= $args->{"limit"}B= $Qudo::JOB_STATUS_LIMIT_SIZE
ABresult
1X1
011
000
149 0 ▶ $args->{offset} ||= $JOB_STATUS_OFFSET_SIZE;
Condition0%0/3
$args->{"offset"} ||= $Qudo::JOB_STATUS_OFFSET_SIZE
l!l&&r!l&&!r
000
MC/DC0%0/2
$args->{"offset"} ||= $Qudo::JOB_STATUS_OFFSET_SIZE
$args->{"offset"} $Qudo::JOB_STATUS_OFFSET_SIZE
Truth table0%0/3
A ||= B
A= $args->{"offset"}B= $Qudo::JOB_STATUS_OFFSET_SIZE
ABresult
1X1
011
000
150  
151 0 ▶ if ($dsn) {
Branch: 1 branch
BranchTrueFalseTotal
if ($dsn) 0 0 0
152 0 return $self->driver_for($dsn)->job_status_list($args);
153 }
154  
155 0 my %job_status_list;
156 0 for my $db ($self->shuffled_databases) {
157 0 $job_status_list{$db} = $self->driver_for($db)->job_status_list($args);
158 }
159 0 return \%job_status_list;
160 }
161  
162 =head1 NAME
163  
164 Qudo - simple job queue manager
165  
166 =head1 SYNOPSIS
167  
168 # enqueue job:
169 use Qudo;
170 my $qudo = Qudo->new(
171 driver_class => 'Skinny',
172 databases => [+{
173 dsn => 'dbi:SQLite:/tmp/qudo.db',
174 username => '',
175 password => '',
176 }],
177 );
178 $qudo->enqueue("Worker::Test", { arg => 'arg', uniqkey => 'uniqkey'});
179
180 # do work:
181 use Qudo;
182 my $qudo2 = Qudo->new(
183 driver_class => 'Skinny',
184 databases => [+{
185 dsn => 'dbi:SQLite:/tmp/qudo.db',
186 username => '',
187 password => '',
188 }],
189 manager_abilities => [qw/Worker::Test/],
190 );
191 $qudo2->work(); # boot manager
192 # work work work!
193  
194 =head1 DESCRIPTION
195  
196 Qudo is simple job queue manager system.
197  
198 Your application can insert job into DB ,that is managed by Qudo.
199 And Your application can get & execute job by Qudo worker.
200 Qudo corresponds to deal with DB as Mysql and SQLite.
201  
202 If you add Hook Point around job's working method ,
203 you can add it easily and many point of work milestone.
204 Qudo is consided about adding Hook Point Flexibility.
205  
206 =head1 REPOS
207  
208 http://github.com/nekokak/qudo/tree/master
209  
210 =head1 AUTHOR
211  
212 Atsushi Kobayashi <nekokak _at_ gmail dot com>
213  
214 Masaru Hoshino <masartz _at_ gmail dot com>
215  
216 =head1 COPYRIGHT
217  
218 This program is free software; you can redistribute
219 it and/or modify it under the same terms as Perl itself.
220  
221 The full text of the license can be found in the
222 LICENSE file included with this module.
223  
224 =cut
225  
226 30 1;
« lib/Qudo/Worker.pm ↑ Summary blib/lib/Qudo/Manager.pm »