diff options
author | franck cuny <franck@lumberjaph.net> | 2010-05-09 14:53:45 +0200 |
---|---|---|
committer | franck cuny <franck@lumberjaph.net> | 2010-05-09 14:53:45 +0200 |
commit | 2582e39d9116afba352de8e5851f1ae5f820bc96 (patch) | |
tree | 60c867033f72d4e48996a50eb790dd02c7afb317 | |
parent | new role to handle error and set content type (diff) | |
download | presque-2582e39d9116afba352de8e5851f1ae5f820bc96.tar.gz |
use roles
-rw-r--r-- | lib/presque/ControlHandler.pm | 21 | ||||
-rw-r--r-- | lib/presque/JobQueueHandler.pm | 14 | ||||
-rw-r--r-- | lib/presque/RestQueueHandler.pm | 44 | ||||
-rw-r--r-- | lib/presque/StatusHandler.pm | 8 |
4 files changed, 20 insertions, 67 deletions
diff --git a/lib/presque/ControlHandler.pm b/lib/presque/ControlHandler.pm index 02136d1..4014e2c 100644 --- a/lib/presque/ControlHandler.pm +++ b/lib/presque/ControlHandler.pm @@ -2,23 +2,15 @@ package presque::ControlHandler; use Moose; extends 'Tatsumaki::Handler'; -with qw/presque::Role::QueueName/; +with + qw/presque::Role::QueueName presque::Role::Error presque::Role::Response/; __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; - } + return $self->http_error_queue if !$queue_name; my $key = $self->_queue_stat($queue_name); $self->application->redis->get( @@ -39,11 +31,7 @@ sub get { sub post { my ( $self, $queue_name ) = @_; - if ( !$queue_name ) { - $self->response->code(404); - $self->finish("queue name is missing"); - return; - } + return $self->http_error_queue if !$queue_name; my $content = JSON::decode_json( $self->request->input ); if ( $content->{status} eq 'start' ) { @@ -64,6 +52,7 @@ sub post { sub _set_status { my ( $self, $queue_name, $status ) = @_; + my $key = $self->_queue_stat($queue_name); $self->application->redis->set( diff --git a/lib/presque/JobQueueHandler.pm b/lib/presque/JobQueueHandler.pm index b6a7b44..217388f 100644 --- a/lib/presque/JobQueueHandler.pm +++ b/lib/presque/JobQueueHandler.pm @@ -2,23 +2,15 @@ package presque::JobQueueHandler; use Moose; extends 'Tatsumaki::Handler'; -with qw/presque::Role::QueueName/; +with + qw/presque::Role::QueueName presque::Role::Error presque::Role::Response/; __PACKAGE__->asynchronous(1); -before [qw/get/] => 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({error => 'queue name is missing'}); - return; - } + $self->http_error_queue if (!$queue_name); my $key = $self->_queue($queue_name); diff --git a/lib/presque/RestQueueHandler.pm b/lib/presque/RestQueueHandler.pm index a7842f5..115b3e5 100644 --- a/lib/presque/RestQueueHandler.pm +++ b/lib/presque/RestQueueHandler.pm @@ -1,26 +1,17 @@ package presque::RestQueueHandler; +use JSON; use Moose; extends 'Tatsumaki::Handler'; -with 'presque::Role::QueueName'; +with + qw/presque::Role::QueueName presque::Role::Error presque::Role::Response/; __PACKAGE__->asynchronous(1); -use JSON; - -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; - } + return $self->http_error_queue if ( !$queue_name ); my $dkey = $self->_queue_delayed($queue_name); my $lkey = $self->_queue($queue_name); @@ -58,9 +49,7 @@ sub get { ); } else { - $self->response->code(404); - $self->finish( - JSON::encode_json( { error => "no job" } ) ); + $self->http_error('no job', 404); } } ); @@ -72,20 +61,11 @@ sub get { sub post { my ( $self, $queue_name ) = @_; - if ( !$queue_name ) { - $self->finish( - JSON::encode_json( { error => 'queue name is missing' } ) ); - return; - } + return $self->http_error_queue if ( !$queue_name ); - if ( $self->request->header('Content-Type') ne 'application/json' ) { - $self->finish( - JSON::encode_json( - { error => 'content-type must be application/json' } - ) - ); - return; - } + return $self->http_error_content_type + if (!$self->request->header('Content-Type') + || $self->request->header('Content-Type') ne 'application/json' ); my $input = $self->request->parameters; my $delayed = $input->{delayed}; @@ -128,11 +108,7 @@ sub post { sub delete { my ( $self, $queue_name ) = @_; - if ( !$queue_name ) { - $self->finish( - JSON::encode_json( { error => 'queue name is missing' } ) ); - return; - } + return $self->http_error_queue if ( !$queue_name ); # delete delayed queue my $lkey = $self->_queue($queue_name); diff --git a/lib/presque/StatusHandler.pm b/lib/presque/StatusHandler.pm index 84a75de..6e846d2 100644 --- a/lib/presque/StatusHandler.pm +++ b/lib/presque/StatusHandler.pm @@ -2,17 +2,13 @@ package presque::StatusHandler; use Moose; extends 'Tatsumaki::Handler'; -with qw/presque::Role::QueueName/; +with + qw/presque::Role::QueueName presque::Role::Error presque::Role::Response/; __PACKAGE__->asynchronous(1); use JSON; -before [qw/get/] => sub { - my $self = shift; - $self->response->header('application/json'); -}; - sub get { my ( $self, $queue_name ) = @_; |