about summary refs log tree commit diff
path: root/lib/presque/WorkerHandler.pm
blob: aad800a9d73217a24077219e1d83432b49c8d90b (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
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package presque::WorkerHandler;

use JSON;
use Moose;
extends 'Tatsumaki::Handler';
with
    'presque::Role::Error',
    'presque::Role::Response',
    'presque::Role::Queue::Names',
    'presque::Role::Queue::WithQueueName' => {methods => [qw/delete post/]};

__PACKAGE__->asynchronous(1);

sub get {
    my $self = shift;

    my $input      = $self->request->parameters;
    my $worker_id  = $input->{worker_id} if $input && $input->{worker_id};
    my $queue_name = $input->{queue_name} if $input && $input->{queue_name};

    if ($queue_name) {
        $self->_get_stats_for_queue($queue_name);
    }
    elsif ($worker_id) {
        $self->_get_stats_for_worker($worker_id);
    }
    else {
        $self->_get_stats_for_workers();
    }
}

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

    my $worker_id = $self->request->header('X-presque-workerid');

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

    $self->application->redis->sadd($self->_workers_list, $worker_id);
    $self->application->redis->sadd($self->_workers_on_queue($queue_name), $worker_id);

    $self->application->redis->hset($self->_workers_processed, $worker_id, 0);
    $self->application->redis->hset($self->_workers_failed,    $worker_id, 0);

    $self->response->code(201);
    $self->finish();
}

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

    my $worker_id = $self->request->header('X-presque-workerid');

    return $self->http_error('worker_id is missing') unless $worker_id;

    $self->application->redis->srem($self->_workers_list, $worker_id);
    $self->application->redis->srem($self->_workers_on_queue($queue_name), $worker_id);

    $self->application->redis->hdel($self->_workers_processed, $worker_id);
    $self->application->redis->hdel($self->_workers_failed,    $worker_id);

    $self->response->code(204);
    $self->finish();
}

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

    my $desc = {queue_name => $queue_name};

    $self->application->redis->smembers(
        $self->_workers_on_queue($queue_name),
        sub {
            my $list = shift;
            $desc->{workers_list} = $list;
            $self->application->redis->hget(
                $self->_queue_processed,
                $queue_name,
                sub {
                    my $processed = shift;
                    $desc->{processed} = $processed;
                    $self->application->redis->hget(
                        $self->_queue_failed,
                        $queue_name,
                        sub {
                            my $failed = shift;
                            $desc->{failed} = $failed;
                            $self->entity($desc);
                        }
                    );
                }
            );
        }
    );
}

sub _get_stats_for_worker {
    my ($self, $worker_id) = @_;

    my $desc = {worker_id => $worker_id};

    $self->application->redis->hget(
        $self->_worker_processed,
        $worker_id,
        sub {
            my $processed = shift;
            $desc->{processed} = $processed;
            $self->application->redis->hget(
                $self->_worker_failed,
                $worker_id,
                sub {
                    my $failed = shift;
                    $desc->{failed} = $failed;
                    $self->entity($desc);
                }
            );
        }
    );
}

sub _get_stats_for_workers {
    my $self = shift;

    $self->application->redis->smembers(
        $self->_workers_list,
        sub {
            my $list = shift;
            $self->entity($list);
        }
    );
}

1;

=head1 NAME

presque::WorkerHandler

=head1 SYNOPSIS

    # fetch some informations about a worker
    curl "http://localhost:5000/w/?worker_id=worker_1" | json_xs -f json -t json-pretty

    {
        "worker_id" : "worker_1",
        "started_at" : 1273923534,
        "processed" : "0",
        "failed" : "0"
    }

    # to register the worker "worker_1" on the queue "queuename"
    curl -H 'Content-Type: appplication/json' -H 'X-presque-workerid: worker_1' http://localhost:5000/w/queuename

    # to unreg a worker
    curl -X DELETE -H 'X-presque-workerid: worker_1' http://localhost:5000/w/queuename

=head1 DESCRIPTION

It's possible for a worker to register itself against presque. This is not required. The main purpose of registering workers is to collect informations about your workers : what are they doing right now, how many jobs have they failed, how many jobs have they processed, ...

=head2 GET

=over 4

=item path

/w/:queue_name

=item request

query : worker_id OR queue_name OR none

=item response

http code : 200

content_type : application/json

=back

If the query parameter is B<worker_id>, stats about this worker are returned. If the query parameter is B<queue_name>, stats about the workers on this queue are returned. If no query parameter is set, stats about the queue are returned.

=head2 DELETE

=over 4

=item path

/w/:queue_name

=item headers

X-presque-workerid: worker's ID (optional)

=item response

code : 204

content : null

=back

When a worker has finished to work, it should unregister itself. The response HTTP code is 204, and no content is returned.

=head2 POST

Register a worker on a queue.

=over 4

=item path

/w/:queue_name

=item headers

X-presque-workerid: worker's ID

=item response

http code : 201

content : null

=back

To register a worker, a POST request must be made. The header 'X-presque-workerid' must be set, and the value is the worker's ID.

The HTTP response is 201, and no content is returned.

=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