File Coverage

blib/lib/Qudo.pm
Criterion Covered Total %
statement 18 79 22.7
branch 0 12 0.0
condition 0 20 0.0
subroutine 6 22 27.2
pod 0 13 0.0
total 24 146 16.4


line stmt bran cond sub pod time code
1             package Qudo;
2 30     30   112 use strict;
  30         30  
  30         777  
3 30     30   104 use warnings;
  30         37  
  30         1055  
4              
5             our $VERSION = '0.01_03';
6              
7 30     30   8059 use Qudo::Manager;
  30         59  
  30         277  
8 30     30   776 use Carp;
  30         39  
  30         1613  
9 30     30   124 use UNIVERSAL::require;
  30         40  
  30         90  
10 30     30   739 use List::Util qw/shuffle/;
  30         38  
  30         26060  
11              
12             our $RETRY_SECONDS = 30;
13             our $FIND_JOB_LIMIT_SIZE = 30;
14             our $DEFAULT_DRIVER = 'Skinny';
15             our $EXCEPTION_LIMIT_SIZE = 10;
16             our $EXCEPTION_OFFSET_SIZE = 0;
17             our $JOB_STATUS_LIMIT_SIZE = 10;
18             our $JOB_STATUS_OFFSET_SIZE = 0;
19              
20             sub new {
21 0     0 0   my $class = shift;
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     0     my $self = shift;
43              
44 0           my $driver = 'Qudo::Driver::' . $self->{driver_class};
45 0 0         $driver->use or Carp::croak $@;
46 0           $driver->init_driver($self);
47             }
48              
49             sub set_connection {
50 0     0 0   my ($self, $dsn, $connection) = @_;
51 0           $self->{connections}->{$dsn} = $connection;
52             }
53             sub get_connection {
54 0     0 0   my ($self, $dsn) = @_;
55 0           $self->{connections}->{$dsn};
56             }
57              
58             sub shuffled_databases {
59 0     0 0   my $self = shift;
60 0           my @dsns = keys %{$self->{connections}};
  0            
61 0           return shuffle(@dsns);
62             }
63              
64             sub driver {
65 0     0 0   my ($self, $dsn) = @_;
66 0   0       $dsn ||= $self->shuffled_databases;
67 0           $self->driver_for($dsn);
68             }
69              
70             sub driver_for {
71 0     0 0   my ($self, $dsn) = @_;
72 0           $self->get_connection($dsn);
73             }
74              
75             sub manager {
76 0     0 0   my $self = shift;
77              
78             $self->{manager} ||= Qudo::Manager->new(
79 0     0     driver_for => sub { $self->driver_for(+shift) },
80 0     0     shuffled_databases => sub { $self->shuffled_databases },
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   0       );
87             }
88              
89             sub enqueue {
90 0     0 0   my $self = shift;
91 0           $self->manager->enqueue(@_);
92             }
93              
94             sub work {
95 0     0 0   my ($self, $work_delay) = @_;
96 0   0       $work_delay ||= 5;
97              
98 0           my $manager = $self->manager;
99 0 0         unless ($manager->has_abilities) {
100 0           Carp::croak 'manager dose not have abilities.';
101             }
102              
103 0           while (1) {
104 0 0         sleep $work_delay unless $manager->work_once;
105             }
106             }
107              
108             sub job_list {
109 0     0 0   my ($self, $funcs) = @_;
110              
111 0           return $self->driver->job_list($self->{find_job_limit_size}, $funcs);
112             }
113              
114             sub job_count {
115 0     0 0   my ($self, $funcs, $dsn) = @_;
116              
117 0 0         if ($dsn) {
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     0 0   my ($self, $args, $dsn) = @_;
130              
131 0   0       $args->{limit} ||= $EXCEPTION_LIMIT_SIZE;
132 0   0       $args->{offset} ||= $EXCEPTION_OFFSET_SIZE;
133              
134 0 0         if ($dsn) {
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     0 0   my ($self, $args, $dsn) = @_;
147              
148 0   0       $args->{limit} ||= $JOB_STATUS_LIMIT_SIZE;
149 0   0       $args->{offset} ||= $JOB_STATUS_OFFSET_SIZE;
150              
151 0 0         if ($dsn) {
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
213              
214             Masaru Hoshino
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             1;