Queue/QueueRabbit.pm untested

filter: Statementstmt 0.0% 0 / 56 Branchbran 0.0% 0 / 2 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 92 CC 2 SCAR 9.4
CC
2
Cov
0%
suscribe
6.0
initialize
2.0
add
2.0
CRAP 2.6
1 package Eixo::Queue::QueueRabbit;
2  
3 use strict;
4 use Scalar::Util qw(weaken);
5  
6 use Eixo::Base::Clase "Eixo::Queue";
7  
8 use Eixo::Queue::JobCifrador;
9 use Eixo::Queue::RabbitDriver;
10  
11  
12 has(
13  
14  
15 host=>undef,
16  
17 port=>undef,
18  
19 vhost=>undef,
20  
21 user=>undef,
22  
23 password=>undef,
24  
25 driver=>undef,
26  
27 secret=>undef,
28  
29 exchange=>undef,
30  
31 routing_key=>undef,
32 );
33  
34 sub initialize{
35  
36 $_[0]->SUPER::initialize(@_[1..$#_]);
37  
38 $_[0]->driver(
39  
40 Eixo::Queue::RabbitDriver->new(
41  
42 host=>$_[0]->host,
43  
44 port=>$_[0]->port,
45  
46 vhost=>$_[0]->vhost,
47  
48 user=>$_[0]->user,
49  
50 password=>$_[0]->password
51 )
52  
53 );
54 }
55  
56 sub add :Sig(self, Eixo::Queue::Job){
57 my ($self, $job, $routing_key, $opciones) = @_;
58  
59 my $message = $self->__crypJob($job);
60  
61 $routing_key = $routing_key || $self->routing_key;
62  
63 $self->driver->publicar(
64  
65 $self->__crypJob($job),
66  
67 $self->exchange,
68  
69 $routing_key,
70  
71 $opciones
72 );
73 }
74  
75 sub addAndWait :Sig(self, Eixo::Queue::Job, s, CODE){
76 my (
77  
78 $self,
79 $job,
80 $routing_key_wait,
81 $callback,
82 $timeout
83  
84 ) = @_;
85  
86 $self->add($job, undef, {
87  
88 mandatory=>1,
89  
90 });
91  
92 $self->wait(
93 $routing_key_wait,
94 $callback,
95 ref($job),
96 $timeout
97 );
98 }
99  
100 sub suscribe :Sig(self, CODE){
101 my ($self, $callback, $job_class) = @_;
102  
103 $job_class = $job_class || "Eixo::Queue::Job";
104  
105 my $kself = $self;
106  
107 weaken($kself);
108  
109 $self->__suscribe(
110  
111 $self->routing_key,
112  
113 sub {
114 my ($message, $next, $end) = @_;
115  
116 my $job = $kself->__decryptJob(
117  
118 $message->cuerpo,
119  
120 $job_class
121  
122 );
123  
124 $message->recibido();
125  
126 if($callback->($job)){
127 $next->();
128 }
129 else{
130 $end->();
131 }
132  
133 }
134  
135 );
136 }
137  
138 sub wait :Sig(self, s, CODE, s){
139 my ($self, $routing_key_wait, $callback, $job_class, $timeout) = @_;
140  
141 my $kself = $self;
142  
143 weaken($kself);
144  
145 $timeout && eval{
146  
147 local $SIG{ALRM} = sub {
148  
149 $kself->driver->terminar();
150  
151 $callback->("TIMEOUT");
152  
153 # alarm(0);
154 };
155  
156 alarm($timeout);
157  
158 };
159  
160 $self->__suscribe(
161  
162 $routing_key_wait,
163  
164 sub {
165 my ($message, $next, $end) = @_;
166  
167 my $job = $kself->__decryptJob(
168  
169 $message->cuerpo,
170  
171 $job_class
172  
173 );
174  
175 $message->recibido;
176  
177 $callback->(undef, $job);
178
179 $end->();
180  
181 }
182  
183 );
184
185 }
186  
187 sub __suscribe :Sig(self, s, CODE){
188 my ($self, $routing_key_wait, $callback) = @_;
189  
190 $self->driver->suscribirse(
191  
192 $self->exchange,
193  
194 $routing_key_wait,
195  
196 $callback
197 );
198 }
199  
200  
201 sub __decryptJob{
202 my ($self, $message, $job_class) = @_;
203
204 $job_class->new->descifrar(
205
206 $message,
207  
208 $self->secret
209  
210 );
211 }
212  
213 sub __crypJob{
214 my ($self, $job) = @_;
215  
216 $job->cifrar($self->secret);
217 }
218  
219  
220 1;