about summary refs log tree commit diff
path: root/lib/presque/StatusHandler.pm
blob: e8870da1f208a61465216ca0337e174b46005795 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package presque::StatusHandler;

use JSON;
use Moose;
extends 'Tatsumaki::Handler';
with
  'presque::Role::Queue::Names',
  'presque::Role::Error',
  'presque::Role::Response';

__PACKAGE__->asynchronous(1);

sub get {
    my ($self, $queue_name) = @_;

    if ($queue_name) {
        my $input     = $self->request->parameters;
        my $with_desc = ($input && $input->{with_desc}) ? 1 : 0;
        my $key       = $self->_queue($queue_name);
        $self->application->redis->llen(
            $key,
            sub {
                my $size = shift;
                if ($with_desc) {
                    $self->application->redis->lrange(
                        $self->_queue($queue_name),
                        0, $size,
                        sub {
                            my $jobs = shift;
                            $self->application->redis->mget(
                                @$jobs,
                                sub {
                                    my $full_jobs = shift;
                                    $self->entity(
                                        {   queue => $queue_name,
                                            size  => $size,
                                            jobs  => $full_jobs,
                                        }
                                    );
                                }
                            );
                        }
                    );
                }
                else {
                    $self->entity({queue => $queue_name, size => $size});
                }
            }
        );
    }
    else {
        $self->application->redis->smembers(
            $self->_queue_set,
            sub {
                my $res = shift;
                $self->entity({queues => $res, size => scalar @$res});
            }
        );
    }
}

1;
__END__

=head1 NAME

presque::StatusHandler - return the current size of a queue

=head1 DESCRIPTION

Return the current size of a queue

=head2 GET

=over 4

=item path

/status/:queue_name

=item response

content-type : application/json

code : 200

content : {"queue":"queue_name", "size":10}

=back

The response contains the following informations

=over 2

=item B<queue>

name of the queue

=item B<size>

size of the queue

=back

=head1 AUTHOR

franck cuny E<lt>franck@lumberjaph.netE<gt>

=head1 SEE ALSO

=head1 LICENSE

Copyright 2010 by Linkfluence

L<http://linkfluence.net>

This library is free software; you can redistribute it and/or modify
it under the same terms as Perl itself.

=cut