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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
|
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
=head1 SYNOPSIS
# stop a queue
curl -X POST -H 'Content-Type: application/json' -d '{"status":"stop"}' http://localhost:5000/control/queuename
# start a queue
curl -X POST -H 'Content-Type: application/json' -d '{"status":"start"}' http://localhost:5000/control/queuename
# fetch the status of a queue
curl http://localhost:5000/control/queuename
=head1 DESCRIPTION
By default, when a queue is created, the status is set to 'open'. When a queue is set to 'stop', no job will be fetched from the queue.
=head1 METHODS
=head2 GET
=over 4
=item path
/control/queuename
=item request
=item response
content-type : application/json
code : 200
content : {"status":"0","queue":"foo"}
=back
=head2 POST
=over 4
=item path
/control/queuename
=item request
content-type : application/json
content : {"status":"stop"}
=item response
content-type : application/json
content : {"response":"updated","queue":"foo"}
=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
|