File Coverage

blib/lib/Eixo/Queue/MongoDriver.pm
Criterion Covered Total %
statement 26 42 61.9
branch 2 10 20.0
condition 2 6 33.3
subroutine 8 13 61.5
pod 0 8 0.0
total 38 79 48.1


line stmt bran cond sub pod time code
1             package Eixo::Queue::MongoDriver;
2              
3 2     2   54146 use strict;
  2         3  
  2         43  
4 2     2   802 use MongoDB;
  2         2061549  
  2         44  
5 2     2   414 use Eixo::Base::Clase;
  2         8001  
  2         13  
6 2     2   585 use Eixo::Queue::Job;
  2         4  
  2         12  
7              
8             has(
9              
10             db=>undef,
11              
12             collection=>undef,
13              
14             host=>'localhost',
15              
16             port=>27017,
17              
18             __connection=>undef,
19             );
20              
21             sub addJob{
22 0     0 0 0 my ($self, $job) = @_;
23              
24 0 0       0 unless($job->isa('Eixo::Queue::Job')){
25            
26 0         0 die(ref($self) . '::addJob: an Eixo::Queue::Job was expected');
27              
28             }
29              
30             $self->getCollection->insert_one({
31             _id => $job->id,
32 0         0 %{$job->to_hash}
  0         0  
33             });
34              
35             }
36              
37             sub updateJob{
38 0     0 0 0 my ($self, $job) = @_;
39              
40 0         0 $self->getCollection->update(
41             {_id=>$job->id} ,
42              
43             $job->to_hash
44             );
45             }
46              
47             sub getJob{
48 0     0 0 0 my ($self, $id) = @_;
49              
50 0         0 $self->__format(
51              
52             $self->getCollection->find({
53            
54             _id=>$id
55              
56             })->next
57              
58             );
59             }
60              
61             sub find{
62 0     0 0 0 my ($self, $query, $sort) = @_;
63              
64 0         0 $self->__format(
65              
66             $self->getCollection
67            
68             ->find($query)
69            
70             ->sort($sort)
71              
72             ->all
73             );
74             }
75              
76             sub getPendingJob{
77 2     2 0 702 my ($self, %args) = @_;
78              
79 2         5 my $query = {
80              
81             status=>"WAITING"
82             };
83              
84 2 50       8 $query->{queue} = $args{queue} if(defined $args{queue});
85              
86 2         6 $self->__format(
87              
88             $self->getCollection->find_and_modify({
89              
90             query =>$query,
91             sort => {creation_timestamp => 1},
92             update => {
93             '$set' => {
94             status => 'PROCESSING',
95             start_timestamp => time
96             }
97             },
98             new => 1,
99             })
100              
101             );
102              
103             }
104              
105              
106             sub __format{
107 0     0   0 my ($self, @jobs) = @_;
108              
109             @jobs = map {
110              
111 0         0 Eixo::Queue::Job->new(%$_);
112              
113 0         0 } grep { ref($_) } @jobs;
  0         0  
114              
115 0 0       0 wantarray ? @jobs : (@jobs < 2) ? $jobs[0] : \@jobs;
    0          
116             }
117              
118              
119             sub getCollection{
120 2     2 0 4 my ($self, $collection) = @_;
121              
122 2   33     9 $collection = $collection || $self->collection;
123              
124 2         15 $self->getDb->get_collection($collection);
125              
126             }
127              
128             sub getDb{
129 2     2 0 2 my ($self, $db) = @_;
130              
131 2   33     24 $db = $db || $self->db;
132              
133 2         12 $self->getConnection->get_database($db);
134            
135              
136             }
137              
138             sub getConnection{
139              
140 2 50   2 0 6 return $_[0]->__connection if($_[0]->__connection);
141              
142 2         9 my $c;
143              
144 2         6 $_[0]->__connection(
145              
146              
147             $c = MongoDB::MongoClient->new(
148              
149             host=>$_[0]->host,
150              
151             port=>$_[0]->port
152              
153             )
154              
155             );
156              
157 2         78393 $_[0]->__connection;
158             }
159              
160             #__PACKAGE__->new->getConexion;
161              
162             1;