about summary refs log tree commit diff
path: root/lib/presque/ControlHandler.pm
blob: 7ef56e5687b0cab7f81c8cfe441d27a38f30377d (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
package presque::ControlHandler;

use JSON;
use Moose;
extends 'Tatsumaki::Handler';

with (
  'presque::Role::QueueName',
  'presque::Role::Error',
  'presque::Role::Response',
  'presque::Role::RequireQueue' => {methods => [qw/get post/]},
);

__PACKAGE__->asynchronous(1);

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

    $self->application->redis->get(
        $self->_queue_stat($queue_name),
        sub {
            my $status = shift;
            $self->entity(
                {   queue  => $queue_name,
                    status => $status
                }
            );
        }
    );
}

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

    my $content = $self->request->content;

    return $self->http_error('content is missing') if !$content;

    my $json = JSON::decode_json( $content );
    if ( $json->{status} eq 'start' ) {
        $self->_set_status( $queue_name, 1 );
    }
    elsif ( $json->{status} eq 'stop' ) {
        $self->_set_status( $queue_name, 0 );
    }
    else {
        $self->http_error('invalid status '.$content->{status});
    }
}

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

    my $key = $self->_queue_stat($queue_name);

    $self->application->redis->set($key, $status);
    $self->entity(
        {   queue    => $queue_name,
            response => 'updated',
        }
    );
}

1;
__END__

=head1 NAME

presque::ControlHandler - a redis based message queue

=head1 DESCRIPTION

=head1 METHODS

=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