diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/presque/IndexHandler.pm | 28 | ||||
-rw-r--r-- | lib/presque/JobQueueHandler.pm | 57 | ||||
-rw-r--r-- | lib/presque/RestQueueHandler.pm | 121 | ||||
-rw-r--r-- | lib/presque/StatusHandler.pm | 72 |
4 files changed, 260 insertions, 18 deletions
diff --git a/lib/presque/IndexHandler.pm b/lib/presque/IndexHandler.pm index 9a8e92e..faaebf2 100644 --- a/lib/presque/IndexHandler.pm +++ b/lib/presque/IndexHandler.pm @@ -4,11 +4,33 @@ use Moose; extends 'Tatsumaki::Handler'; __PACKAGE__->asynchronous(1); -use JSON; - sub get { my $self = shift; - # render template + $self->render('index.html'); } 1; +__END__ + +=head1 NAME + +presque::IndexHandler - 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 diff --git a/lib/presque/JobQueueHandler.pm b/lib/presque/JobQueueHandler.pm index 1e6d9e5..17a1e8a 100644 --- a/lib/presque/JobQueueHandler.pm +++ b/lib/presque/JobQueueHandler.pm @@ -4,10 +4,61 @@ use Moose; extends 'Tatsumaki::Handler'; __PACKAGE__->asynchronous(1); -use JSON; - sub get { - my ($self, $queue_name) = @_; + my ( $self, $queue_name ) = @_; + my $key = $queue_name . ':queue'; + $self->application->redis->lrange( + $key, 0, 9, + sub { + my $jobs = shift; + $self->application->redis->llen( + $key, + sub { + my $size = shift; + my $lkey = $queue_name . '*'; + $self->application->redis->keys( + $lkey, + sub { + my $total = shift; + $self->render( + 'job.html', + { + queue => $queue_name, + jobs => $jobs, + job_count => $size, + queue_size => scalar @$total + } + ); + } + ); + } + ); + } + ); } 1; +__END__ + +=head1 NAME + +presque::IndexHandler - 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 diff --git a/lib/presque/RestQueueHandler.pm b/lib/presque/RestQueueHandler.pm index d953505..b35b5c3 100644 --- a/lib/presque/RestQueueHandler.pm +++ b/lib/presque/RestQueueHandler.pm @@ -5,11 +5,23 @@ extends 'Tatsumaki::Handler'; __PACKAGE__->asynchronous(1); use JSON; -use YAML::Syck; + +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->finish( + JSON::encode_json( { error => 'queue name is missing' } ) ); + return; + } + my $lkey = $queue_name . ':queue'; + $self->application->redis->lpop( $lkey, sub { @@ -23,6 +35,8 @@ sub get { } ); }else{ + $self->response->code(404); + $self->finish(JSON::encode_json({error => "no job"})); } } @@ -32,8 +46,22 @@ sub get { sub post { my ( $self, $queue_name ) = @_; - my $p = $self->request->content; + if ( !$queue_name ) { + $self->finish( + JSON::encode_json( { error => 'queue name is missing' } ) ); + return; + } + if ( $self->request->header('Content-Type') ne 'application/json' ) { + $self->finish( + JSON::encode_json( + { error => 'content-type must be application/json' } + ) + ); + return; + } + + my $p = $self->request->content; $self->application->redis->incr( $queue_name . ':UUID', sub { @@ -44,19 +72,21 @@ sub post { $key, $p, sub { my $status_set = shift; - my $lkey = $queue_name . ':queue'; - if ($uuid == 1) { + my $lkey = $queue_name . ':queue'; + if ( $uuid == 1 ) { $self->application->redis->sadd( 'QUEUESET', $lkey, sub { my $ckey = 'queuestat:' . $queue_name; $self->application->redis->set( $ckey, 1 ); - $self->_finish_post($lkey, $key, $status_set); + $self->_finish_post( $lkey, $key, + $status_set ); } ); - }else{ - $self->_finish_post($lkey, $key, $status_set); + } + else { + $self->_finish_post( $lkey, $key, $status_set ); } } ); @@ -64,18 +94,85 @@ sub post { ); } +sub delete { + my ( $self, $queue_name ) = @_; + + if ( !$queue_name ) { + $self->finish( + JSON::encode_json( { error => 'queue name is missing' } ) ); + return; + } + + my $lkey = $queue_name . ':queue'; + $self->application->redis->delete( + $lkey, + sub { + my $res = shift; + $self->finish( + JSON::encode_json( { queue => $queue_name, status => $res } ) + ); + } + ); +} + sub _finish_post { my ($self, $lkey, $key, $result) = @_; $self->application->redis->rpush( $lkey, $key, sub { - $self->finish($result); + $self->finish({status => 'success'}); } ); } -sub delete { - my ($self, $queue_name) = @_; -} - 1; +__END__ + +=head1 NAME + +presque::IndexHandler - a redis based message queue + +=head1 DESCRIPTION + +=head1 METHODS + +=head2 get + +Get a JSON object out of the queue. + +=head2 post + +Insert a new job in the queue. The POST request must: + +=over 4 + +=item + +have the B<Content-Type> header of the request set to B<application/json> + +=item + +the B<body> of the request must be a valid JSON object + +=back + +=head2 delete + +Purge and delete the queue. + +=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 diff --git a/lib/presque/StatusHandler.pm b/lib/presque/StatusHandler.pm new file mode 100644 index 0000000..18be7cd --- /dev/null +++ b/lib/presque/StatusHandler.pm @@ -0,0 +1,72 @@ +package presque::StatusHandler; + +use Moose; +extends 'Tatsumaki::Handler'; +__PACKAGE__->asynchronous(1); + +use JSON; + +before [qw/get/] => sub { + my $self = shift; + $self->response->header('application/json'); +}; + +sub get { + my ( $self, $queue_name ) = @_; + + $self->response->header( 'Content-Type' => 'application/json' ); + + my $conf = $self->application->config->{redis}; + my $stats = { redis => $conf->{host} . ':' . $conf->{port}, }; + + if ($queue_name) { + my $key = $queue_name . ":queue"; + $self->application->redis->llen( + $key, + sub { + my $size = shift; + $stats->{queue} = $queue_name; + $stats->{size} = $size; + my $json = JSON::encode_json($stats); + $self->finish($json); + } + ); + } + else { + $self->application->redis->smembers( + 'QUEUESET', + sub { + my $res = shift; + $stats->{queues} = $res; + $stats->{size} = scalar @$res; + $self->finish( JSON::encode_json($stats) ); + } + ); + } +} + +1; +__END__ + +=head1 NAME + +presque::IndexHandler - 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 |