about summary refs log tree commit diff
diff options
context:
space:
mode:
authorfranck cuny <franck@lumberjaph.net>2010-04-14 13:48:53 +0200
committerfranck cuny <franck@lumberjaph.net>2010-04-14 13:48:53 +0200
commitcb0bb5a80647407f76f43316effe974a2595477b (patch)
tree0cc8ae8c9320236a41860cbff0859f9ca0398ce1
parentadd control handler (diff)
downloadpresque-cb0bb5a80647407f76f43316effe974a2595477b.tar.gz
basic handler to control queue
-rw-r--r--lib/presque/ControlHandler.pm100
1 files changed, 100 insertions, 0 deletions
diff --git a/lib/presque/ControlHandler.pm b/lib/presque/ControlHandler.pm
new file mode 100644
index 0000000..94ceab0
--- /dev/null
+++ b/lib/presque/ControlHandler.pm
@@ -0,0 +1,100 @@
+package presque::ControlHandler;
+
+use Moose;
+extends 'Tatsumaki::Handler';
+__PACKAGE__->asynchronous(1);
+
+before [qw/get post/] => sub {
+    my $self = shift;
+    $self->response->header('Content-Type' => 'application/json');
+};
+
+sub get {
+    my ( $self, $queue_name ) = @_;
+    
+    if ( !$queue_name ) {
+        $self->response->code(404);
+        $self->finish("queue name is missing");
+        return;
+    }
+
+    my $key = 'queuestat:' . $queue_name;
+    $self->application->redis->get(
+        $key,
+        sub {
+            my $status = shift;
+            $self->finish(
+                JSON::encode_json(
+                    { queue => $queue_name, status => $status }
+                )
+            );
+        }
+    );
+}
+
+sub post {
+    my ( $self, $queue_name ) = @_;
+
+    if ( !$queue_name ) {
+        $self->response->code(404);
+        $self->finish("queue name is missing");
+        return;
+    }
+
+    my $content = JSON::decode_json( $self->request->input );
+    if ( $content->{status} eq 'start' ) {
+        $self->_set_status( $queue_name, 1 );
+    }
+    elsif ( $content->{status} eq 'stop' ) {
+        $self->_set_status( $queue_name, 0 );
+    }
+    else {
+        $self->response->code(400);
+        $self->finish(
+            JSON::encode_json(
+                { error => 'invalid status ' . $content->{status} }
+            )
+        );
+    }
+}
+
+sub _set_status {
+    my ( $self, $queue_name, $status ) = @_;
+    my $key = 'queuestat:' . $queue_name;
+
+    $self->application->redis->set(
+        $key, 0,
+        sub {
+            my $res = shift;
+            $self->finish(
+                JSON::encode_json( { queue => $queue_name, status => $res } )
+            );
+        }
+    );
+}
+
+1;
+__END__
+
+=head1 NAME
+
+presque::ControlHandler - a redis based message queue
+
+=head1 DESCRIPTION
+
+=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