TarantoolQueue.pm untested

filter: Statementstmt 0.0% 0 / 185 Branchbran 0.0% 0 / 78 Conditioncond 0.0% 0 / 18 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 19 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 309 CC 31 SCAR 35.8
CC
31
Cov
0%
_producer
72.0
statistics
72.0
get_meta
42.0
CRAP 35.7
1 package DR::TarantoolQueue;
2 use DR::Tarantool ();
3 use utf8;
4 use strict;
5 use warnings;
6 use Mouse;
7 use Carp;
8 use JSON::XS;
9 require DR::TarantoolQueue::Task;
10 $Carp::Internal{ (__PACKAGE__) }++;
11  
12 our $VERSION = '0.21';
13  
14 =head1 NAME
15  
16 DR::TarantoolQueue - client for tarantool's queue
17  
18  
19 =head1 SYNOPSIS
20  
21 my $queue = DR::TarantoolQueue->new(
22 host => 'tarantool.host',
23 port => 33014,
24 tube => 'request_queue',
25 space => 11,
26  
27 connect_opts => { # see perldoc DR::Tarantool
28 reconnect_period => 1,
29 reconnect_always => 1
30 }
31 );
32  
33  
34 # put empty task into queue with name 'request_queue'
35 my $task = $queue->put;
36  
37 my $task = $queue->put(data => [ 1, 2, 3 ]);
38  
39 printf "task.id = %s\n", $task->id;
40  
41 =head2 DESCRIPTION
42  
43 The module contains sync and async (coro) driver for tarantool queue.
44  
45 =head1 ATTRIBUTES
46  
47 =head2 host (ro) & port (ro)
48  
49 Tarantool's parameters.
50  
51 =head2 connect_opts (ro)
52  
53 Additional options for L<DR::Tarantool>. HashRef.
54  
55 =head2 coro (ro)
56  
57 If B<true> (default) the driver will use L<Coro> tarantool's driver,
58 otherwise the driver will use sync driver.
59  
60 =head2 ttl (rw)
61  
62 Default B<ttl> for tasks.
63  
64 =head2 ttr (rw)
65  
66 Default B<ttr> for tasks.
67  
68 =head2 pri (rw)
69  
70 Default B<pri> for tasks.
71  
72 =head2 delay (rw)
73  
74 Default B<delay> for tasks.
75  
76 =head2 space (rw)
77  
78 Default B<space> for tasks.
79  
80 =head2 tube (rw)
81  
82 Default B<tube> for tasks.
83  
84  
85 =head2 defaults
86  
87 Defaults for queues. B<HashRef>. Key is tube name. Value is a hash with
88 the following fields:
89  
90 =over
91  
92 =item ttl
93  
94 =item ttr
95  
96 =item delay
97  
98 =item pri
99  
100 =back
101  
102 Methods L</put> (L</urgent>) use these parameters if they
103 are absent (otherwise it uses the same global attributes).
104  
105 =cut
106  
107 with 'DR::TarantoolQueue::JSE';
108  
109 has host => (is => 'ro', isa => 'Maybe[Str]');
110 has port => (is => 'ro', isa => 'Maybe[Str]');
111 has coro => (is => 'ro', isa => 'Bool', default => 1);
112  
113 has ttl => (is => 'rw', isa => 'Maybe[Num]');
114 has ttr => (is => 'rw', isa => 'Maybe[Num]');
115 has pri => (is => 'rw', isa => 'Maybe[Num]');
116 has delay => (is => 'rw', isa => 'Maybe[Num]');
117 has space => (is => 'rw', isa => 'Maybe[Str]');
118 has tube => (is => 'rw', isa => 'Maybe[Str]');
119 has connect_opts => (is => 'ro', isa => 'HashRef', default => sub {{}});
120  
121 has defaults => (is => 'ro', isa => 'HashRef', default => sub {{}});
122  
123 has tnt => (
124 is => 'rw',
125 isa => 'Object',
126 lazy => 1,
127 builder => sub {
128 my ($self) = @_;
129 unless ($self->coro) {
130 return DR::Tarantool::tarantool
131 port => $self->port,
132 host => $self->host,
133 spaces => {},
134 %{ $self->connect_opts }
135 ;
136 }
137
138 require Coro;
139 if ($self->{tnt_waiter}) {
140 push @{ $self->{tnt_waiter} } => $Coro::current;
141 Coro::schedule();
142 return $self->tnt;
143 }
144  
145 $self->{tnt_waiter} = [];
146 my $tnt = DR::Tarantool::coro_tarantool
147 port => $self->port,
148 host => $self->host,
149 spaces => {},
150 %{ $self->connect_opts }
151 ;
152 $_->ready for @{ $self->{tnt_waiter} };
153 delete $self->{tnt_waiter};
154 return $tnt;
155 }
156 );
157  
158  
159 sub _check_opts($@) {
160 my $h = shift;
161 my %can = map { ($_ => 1) } @_;
162  
163 for (keys %$h) {
164 next if $can{$_};
165 croak 'unknown option: ' . $_;
166 }
167 }
168  
169 sub _producer {
170 my ($self, $method, $o) = @_;
171  
172 _check_opts $o, qw(space tube delay ttl ttr pri data);
173  
174 my $space = $o->{space};
175 $space = $self->space unless defined $space;
176 croak 'space was not defined' unless defined $space;
177  
178 my $tube = $o->{tube};
179 $tube = $self->tube unless defined $tube;
180 croak 'tube was not defined' unless defined $tube;
181  
182 my ($ttl, $ttr, $pri, $delay);
183  
184 for ([\$ttl, 'ttl'], [\$delay, 'delay'], [\$ttr, 'ttr'], [\$pri, 'pri']) {
185 my $rv = $_->[0];
186 my $n = $_->[1];
187  
188 if (exists $o->{$n}) {
189 $$rv = $o->{$n};
190 } else {
191 if (exists $self->defaults->{ $tube }) {
192 if (exists $self->defaults->{ $tube }{ $n }) {
193 $$rv = $self->defaults->{ $tube }{ $n };
194 } else {
195 $$rv = $self->$n;
196 }
197 } else {
198 $$rv = $self->$n;
199 }
200 }
201 $$rv ||= 0;
202  
203 }
204  
205  
206 my $tuple = $self->tnt->call_lua(
207 "queue.$method" => [
208 $space,
209 $tube,
210 $delay,
211 $ttl,
212 $ttr,
213 $pri,
214 $self->jse->encode($o->{data})
215 ]
216 );
217  
218 return DR::TarantoolQueue::Task->tuple($tuple, $space, $self);
219 }
220  
221 =head1 METHODS
222  
223 =head2 new
224  
225 my $q = DR::TarantoolQueue->new(host => 'abc.com', port => 123);
226  
227 Creates new queue(s) accessor.
228  
229 =cut
230  
231 =head2 dig
232  
233 $q->dig(task => $task);
234 $task->dig; # the same
235  
236 $q->dig(id => $task->id);
237 $q->dig(id => $task->id, space => $task->space);
238  
239 'Dig up' a buried task. Checks, that the task is buried.
240 The task status is changed to ready.
241  
242 =head2 unbury
243  
244 Is a synonym of L</dig>.
245  
246  
247 =head2 delete
248  
249 $q->delete(task => $task);
250 $task->delete; # the same
251  
252 $q->delete(id => $task->id);
253 $q->delete(id => $task->id, space => $task->space);
254  
255 Delete a task from the queue (regardless of task state or status).
256  
257 =head2 peek
258  
259 $q->peek(task => $task);
260 $task->peek; # the same
261  
262 $q->peek(id => $task->id);
263 $q->peek(id => $task->id, space => $task->space);
264  
265 Return a task by task id.
266  
267  
268 =head2 statistics
269  
270 my $s = $q->statistics;
271 my $s = $q->statistics(space => 123);
272 my $s = $q->statistics(space => 123, tube => 'abc');
273 my $s = DR::TarantoolQueue->statistics(space => 123);
274 my $s = DR::TarantoolQueue->statistics(space => 123, tube => 'abc');
275  
276 Return queue module statistics, since server start.
277 The statistics is broken down by queue id.
278 Only queues on which there was some activity are
279 included in the output.
280  
281  
282 =cut
283  
284 sub statistics {
285 my ($self, %o) = @_;
286 _check_opts \%o, qw(space tube);
287 unless (exists $o{space}) {
288 $o{space} = $self->space if ref $self;
289 }
290 unless (exists $o{tube}) {
291 $o{tube} = $self->tube if ref $self;
292 }
293  
294 croak 'space was not defined'
295 if defined $o{tube} and !defined $o{space};
296  
297 my $raw = $self->tnt->call_lua(
298 "queue.statistics" => [
299 defined($o{space}) ? $o{space} : (),
300 defined($o{tube}) ? $o{tube} : ()
301 ]
302 )->raw;
303 return { @$raw };
304 }
305  
306  
307 =head2 get_meta
308  
309 Task was processed (and will be deleted after the call).
310  
311 my $m = $q->get_meta(task => $task);
312 my $m = $q->get_meta(id => $task->id);
313  
314 Returns a hashref with fields:
315  
316  
317 =over
318  
319 =item id
320  
321 task id
322  
323 =item tube
324  
325 queue id
326  
327 =item status
328  
329 task status
330  
331 =item event
332  
333 time of the next important event in task life time, for example,
334 when ttl or ttr expires, in microseconds since start of the UNIX epoch.
335  
336 =item ipri
337  
338 internal value of the task priority
339  
340 =item pri
341  
342 task priority as set when the task was added to the queue
343  
344 =item cid
345  
346 consumer id, of the consumer which took the task (only if the task is taken)
347  
348 =item created
349  
350 time when the task was created (microseconds since start of the UNIX epoch)
351  
352 =item ttl
353  
354 task time to live (microseconds)
355  
356 =item ttr
357  
358 task time to run (microseconds)
359  
360 =item cbury
361  
362 how many times the task was buried
363  
364 =item ctaken
365  
366 how many times the task was taken
367  
368 =item now
369  
370 time recorded when the meta was called
371  
372 =back
373  
374 =cut
375  
376 sub get_meta {
377 my ($self, %o) = @_;
378 _check_opts \%o, qw(task id space);
379 croak 'task was not defined' unless $o{task} or $o{id};
380  
381 my ($id, $space, $tube);
382 if ($o{task}) {
383 ($id, $space, $tube) = ($o{task}->id,
384 $o{task}->space, $o{task}->tube);
385 } else {
386 ($id, $space, $tube) = @o{'id', 'space', 'tube'};
387 $space = $self->space unless defined $o{space};
388 croak 'space is not defined' unless defined $space;
389 $tube = $self->tube unless defined $tube;
390 }
391  
392  
393 my $fields = [
394 { name => 'id', type => 'STR' },
395 { name => 'tube', type => 'STR' },
396 { name => 'status', type => 'STR' },
397 { name => 'event', type => 'NUM64' },
398 { name => 'ipri', type => 'STR', },
399 { name => 'pri', type => 'STR', },
400 { name => 'cid', type => 'NUM', },
401 { name => 'created', type => 'NUM64', },
402 { name => 'ttl', type => 'NUM64' },
403 { name => 'ttr', type => 'NUM64' },
404 { name => 'cbury', type => 'NUM' },
405 { name => 'ctaken', type => 'NUM' },
406 { name => 'now', type => 'NUM64' },
407 ];
408 my $tuple = $self->tnt->call_lua(
409 "queue.meta" => [ $space, $id ], fields => $fields
410 )->raw;
411  
412  
413 return { map { ( $fields->[$_]{name}, $tuple->[$_] ) } 0 .. $#$fields };
414 }
415  
416  
417  
418  
419 =head1 Producer methods
420  
421 =head2 put
422  
423 $q->put;
424 $q->put(data => { 1 => 2 });
425 $q->put(space => 1, tube => 'abc',
426 delay => 10, ttl => 3600,
427 ttr => 60, pri => 10, data => [ 3, 4, 5 ]);
428 $q->put(data => 'string');
429  
430  
431 Enqueue a task. Returns new L<task|DR::TarantoolQueue::Task> object.
432 The list of fields with task data (C<< data => ... >>) is optional.
433  
434  
435 If 'B<space>' and (or) 'B<tube>' aren't defined the method
436 will try to use them from L<queue|DR::TarantoolQueue/new> object.
437  
438 =cut
439  
440 sub put {
441 my ($self, %opts) = @_;
442 return $self->_producer(put => \%opts);
443 }
444  
445 =head2 urgent
446  
447 Enqueue a task. The task will get the highest priority.
448 If delay is not zero, the function is equivalent to
449 L<put|DR::TarantoolQueue/put>.
450  
451 =cut
452  
453 sub urgent {
454 my ($self, %opts) = @_;
455 return $self->_producer(urgent => \%opts);
456 }
457  
458  
459 =head1 Consumer methods
460  
461 =head2 take
462  
463 my $task = $q->take;
464 my $task = $q->take(timeout => 0.5);
465 my $task = $q->take(space => 1, tube => 'requests, timeout => 20);
466  
467 If there are tasks in the queue ready for execution,
468 take the highest-priority task. Otherwise, wait for
469 a ready task to appear in the queue, and, as soon as
470 it appears, mark it as taken and return to the consumer.
471 If there is a timeout, and the task doesn't appear until
472 the timeout expires, returns B<undef>. If timeout is not
473 given, waits indefinitely.
474  
475 All the time while the consumer is working on a task,
476 it must keep the connection to the server open. If a
477 connection disappears while the consumer is still
478 working on a task, the task is put back on the ready list.
479  
480 =cut
481  
482 sub take {
483 my ($self, %o) = @_;
484 _check_opts \%o, qw(space tube timeout);
485 $o{space} = $self->space unless defined $o{space};
486 croak 'space was not defined' unless defined $o{space};
487 $o{tube} = $self->tube unless defined $o{tube};
488 croak 'tube was not defined' unless defined $o{tube};
489 $o{timeout} ||= 0;
490  
491  
492 my $tuple = $self->tnt->call_lua(
493 'queue.take' => [
494 $o{space},
495 $o{tube},
496 $o{timeout}
497 ]
498 );
499  
500  
501 return DR::TarantoolQueue::Task->tuple($tuple, $o{space}, $self);
502 }
503  
504  
505 =head2 ack
506  
507 $q->ack(task => $task);
508 $task->ack; # the same
509  
510 $q->ack(id => $task->id);
511 $q->ack(space => $task->space, id => $task->id);
512  
513  
514 Confirm completion of a task. Before marking a task as
515 complete, this function verifies that:
516  
517 =over
518  
519 =item *
520  
521 the task is taken
522  
523 =item *
524  
525 the consumer that is confirming the task is the one which took it
526  
527 =back
528  
529 Consumer identity is established using a session identifier.
530 In other words, the task must be confirmed by the same connection
531 which took it. If verification fails, the function returns an error.
532  
533 On success, deletes the task from the queue. Throws an exception otherwise.
534  
535  
536 =head2 requeue
537  
538 $q->requeue(task => $task);
539 $task->requeue; # the same
540  
541 $q->requeue(id => $task->id);
542 $q->requeue(id => $task->id, space => $task->space);
543  
544 Return a task to the queue, the task is not executed.
545 Puts the task at the end of the queue, so that it's executed
546 only after all existing tasks in the queue are executed.
547  
548  
549 =head2 bury
550  
551 $q->bury(task => $task);
552 $task->bury; # the same
553  
554 $q->bury(id => $task->id);
555 $q->bury(id => $task->id, space => $task->space);
556  
557 Mark a task as B<buried>. This special status excludes the task
558 from the active list, until it's dug up. This function is useful
559 when several attempts to execute a task lead to a failure. Buried
560 tasks can be monitored by the queue owner, and treated specially.
561  
562  
563 =cut
564  
565 for my $m (qw(ack requeue bury dig unbury delete peek)) {
566 no strict 'refs';
567 next if *{ __PACKAGE__ . "::$m" }{CODE};
568 *{ __PACKAGE__ . "::$m" } = sub {
569 my ($self, %o) = @_;
570 _check_opts \%o, qw(task id space);
571 croak 'task was not defined' unless $o{task} or $o{id};
572  
573 my ($id, $space);
574 if ($o{task}) {
575 ($id, $space) = ($o{task}->id, $o{task}->space);
576 } else {
577 ($id, $space) = @o{'id', 'space'};
578 $space = $self->space unless defined $o{space};
579 croak 'space is not defined' unless defined $space;
580 }
581  
582 my $tuple = $self->tnt->call_lua( "queue.$m" => [ $space, $id ] );
583 my $task = DR::TarantoolQueue::Task->tuple($tuple, $space, $self);
584  
585 if ($m eq 'delete') {
586 $task->_set_status('removed');
587 } elsif ($m eq 'ack') {
588 $task->_set_status('ack(removed)');
589 }
590 $task;
591 }
592 }
593  
594  
595 =head2 release
596  
597 $q->release(task => $task);
598 $task->release; # the same
599  
600 $q->release(id => $task->id, space => $task->space);
601 $q->release(task => $task, delay => 10); # delay the task
602 $q->release(task => $task, ttl => 3600); # append task's ttl
603  
604 Return a task back to the queue: the task is not executed.
605 Additionally, a new time to live and re-execution delay can be provided.
606  
607 =cut
608  
609 sub release {
610 my ($self, %o) = @_;
611 _check_opts \%o, qw(task id space ttl delay);
612 $o{delay} ||= 0;
613 my ($id, $space);
614 if ($o{task}) {
615 ($id, $space) = ($o{task}->id, $o{task}->space);
616 } else {
617 ($id, $space) = @o{'id', 'space'};
618 $space = $self->space unless defined $o{space};
619 croak 'space is not defined' unless defined $space;
620 }
621 my $tuple = $self->tnt->call_lua('queue.release' =>
622 [ $space, $id, $o{delay}, $o{ttl} || () ]
623 );
624 return DR::TarantoolQueue::Task->tuple($tuple, $space, $self);
625 }
626  
627  
628  
629 =head2 done
630  
631 $q->done(task => $task, data => { result => '123' });
632 $task->done(data => { result => '123' }); # the same
633 $q->done(id => $task->id, space => $task->space);
634  
635 Mark a task as complete (done), but don't delete it. Replaces task
636 data with the supplied B<data>.
637  
638 =cut
639  
640 sub done {
641 my ($self, %o) = @_;
642 _check_opts \%o, qw(task id space data);
643 my ($id, $space);
644 if ($o{task}) {
645 ($id, $space) = ($o{task}->id, $o{task}->space);
646 } else {
647 ($id, $space) = @o{'id', 'space'};
648 $space = $self->space unless defined $o{space};
649 croak 'space is not defined' unless defined $space;
650 }
651 my $tuple = $self->tnt->call_lua('queue.done' =>
652 [ $space, $id, $self->jse->encode($o{data}) ]
653 );
654 return DR::TarantoolQueue::Task->tuple($tuple, $space, $self);
655 }
656  
657  
658 =head1 COPYRIGHT AND LICENCE
659  
660 Copyright (C) 2012 by Dmitry E. Oboukhov <unera@debian.org>
661 Copyright (C) 2012 by Roman V. Nikolaev <rshadow@rambler.ru>
662  
663 This library is free software; you can redistribute it and/or modify
664 it under the same terms as Perl itself, either Perl version 5.8.8 or,
665 at your option, any later version of Perl 5 you may have available.
666  
667 =cut
668  
669 __PACKAGE__->meta->make_immutable();