385d6b25f13f688396ff3eee796f4dafaf279ebe
[MojoX-Gearman.git] / lib / MojoX / Gearman.pm
1 package MojoX::Gearman;
2
3 use strict;
4 use warnings;
5
6 our $VERSION = 0.1;
7 use base 'Mojo::Base';
8
9 use Mojo::IOLoop;
10 use List::Util    ();
11 use Mojo::Util    ();
12 use Scalar::Util        ();
13 use Data::Dump qw(dump);
14 require Carp;
15
16 __PACKAGE__->attr(server   => '127.0.0.1:4730');
17 __PACKAGE__->attr(ioloop   => sub { Mojo::IOLoop->new });
18 __PACKAGE__->attr(error => undef);
19 __PACKAGE__->attr(timeout  => 300);
20 __PACKAGE__->attr(encoding => 'UTF-8');
21 __PACKAGE__->attr(
22         on_error => sub {
23                 sub {
24                         my $gearman = shift;
25                         warn "Gearman error: ", $gearman->error, "\n";
26                   }
27         }
28 );
29 __PACKAGE__->attr(res => undef);
30
31 sub DESTROY {
32         my $self = shift;
33
34         # Loop
35         return unless my $loop = $self->ioloop;
36
37         # Cleanup connection
38         $loop->drop($self->{_connection})
39           if $self->{_connection};
40 }
41
42 sub connect {
43         my $self = shift;
44
45         # drop old connection
46         if ($self->connected) {
47                 $self->ioloop->drop($self->{_connection});
48         }
49
50         $self->server =~ m{^([^:]+)(:(\d+))?};
51         my $address = $1;
52         my $port = $3 || 4730;
53
54         Scalar::Util::weaken $self;
55
56         # connect
57         $self->{_connecting} = 1;
58         $self->{_connection} = $self->ioloop->connect(
59                 {   address     => $address,
60                         port       => $port,
61                         on_connect => sub { $self->_on_connect(@_) },
62                         on_read => sub { $self->_on_read(@_) },
63                         on_error   => sub { $self->_on_error(@_) },
64                         on_hup   => sub { $self->_on_hup(@_) },
65                 }
66         );
67
68         warn "# using gearman server $address:$port\n";
69
70         return $self;
71 }
72
73 sub connected {
74         my $self = shift;
75
76         return $self->{_connection};
77 }
78
79 my $packet_type = {
80         CAN_DO => 1,
81
82         PRE_SLEEP => 4,
83
84         NOOP => 6,
85
86         SUBMIT_JOB => 7,
87         JOB_CREATED => 8,
88
89         GRAB_JOB => 9,
90         NO_JOB => 10,
91         JOB_ASSIGN => 11,
92
93         WORK_COMPLETE => 13,
94         WORK_FAIL => 14,
95
96         ECHO_REQ => 16,
97         ECHO_RES => 17,
98
99         ERROR => 19,
100 };
101
102 my $nr2type;
103 $nr2type->{ $packet_type->{$_} } = $_ foreach keys %$packet_type;
104
105
106 sub parse_packet {
107         my ($self,$data) = @_;
108         die "no data in packet" unless $data;
109         my ($magic, $type, $len) = unpack( "a4NN", $data );
110         die "wrong magic [$magic]" unless $magic eq "\0RES";
111         die "unsupported type [$type]" unless exists $nr2type->{$type};
112         die "ERROR" if $type == $packet_type->{ERROR};
113         return ( $type, split("\0", substr($data,12,$len)) );
114 }
115
116 sub req {
117         my $self = shift;
118 warn "XXX req ",dump(@_);
119         my $type = shift;
120         my $callback = pop @_ if ref $_[$#_] eq 'CODE';
121         my $data = join("\0", @_);
122
123         die "can't find packet type $type in ", dump $packet_type unless exists $packet_type->{$type};
124         Mojo::Util::encode($self->encoding, $data) if $self->encoding;
125
126         $self->{_res} = undef;
127
128         my $response;
129         my $cb = sub {
130                 my ( $self, $data ) = @_;
131                 my ( $type, @data ) = $self->parse_packet($data);
132                 warn "# <<<< ", $nr2type->{$type}, " ",dump(@data);
133
134                 if ( $type == $packet_type->{JOB_CREATED} ) {
135                         push @{ $self->{_cb_queue} }, sub {
136                                 my ( $self,$data ) = @_;
137                                 my ( $type, $handle, $out ) = $self->parse_packet($data);
138 warn "# <<<< ",$nr2type->{$type}, " ",dump $data;
139                                 if ( $type == $packet_type->{WORK_COMPLETE} ) {
140                                         warn "WORK_COMPLETE $handle ", dump $out;
141                                 } elsif ( $type == $packet_type->{WORK_FAIL} ) {
142                                         warn "WORK_FAIL $handle ", dump $out;
143                                 }
144                                 $self->res( $out );
145                                 $self->stop;
146                         };
147                 } elsif ( $type == $packet_type->{NO_JOB} ) {
148                         $self->req( 'PRE_SLEEP' );
149                         $self->stop;
150                 } elsif ( $type == $packet_type->{JOB_ASSIGN} ) {
151                         my ( $handle, $function, $workload ) = @data;
152                         my $callback = $self->{_function}->{$function};
153                         die "no $function callback" unless ref $callback eq 'CODE';
154                         my $out = $callback->( $workload );
155                         warn "## $data $callback = ", dump $out;
156                         $self->req( 'WORK_COMPLETE', $handle, $out );
157                         $self->req( 'GRAB_JOB' );
158                 } elsif ( $type == $packet_type->{NOOP} ) {
159                         $self->req( 'GRAB_JOB' );
160                 } else {
161                         $self->stop;
162                 }
163
164                 my $out = $#data == 0 ? $data[0] : [ @data ];
165                 $self->res( $out );
166
167         };
168
169 #       $data .= "\0" if $data;
170         my $len = length($data);
171         my $message = pack("a4NN", "\0REQ", $packet_type->{$type}, length $data ) . $data;
172         warn "# >>>> $type ",dump($message);
173
174         my $mqueue = $self->{_message_queue} ||= [];
175         my $cqueue = $self->{_cb_queue}   ||= [];
176
177         push @$mqueue, $message;
178         push @$cqueue, $cb;
179
180         $self->connect unless $self->{_connection};
181         $self->_send_next_message;
182
183         if ( $type eq 'CAN_DO' ) {
184                 $self->{_function}->{$data} = $callback;
185                 $self->res( $callback );
186                 warn "# installed $data callback $callback";
187         } elsif ( $type =~ m/^(ECHO_REQ|SUBMIT_JOB|GRAB_JOB)$/ ) { # sync commands
188                 $self->start;
189         }
190
191         $self->res;
192 }
193
194 sub start {
195         my ($self) = @_;
196         warn "# start";
197         $self->ioloop->start;
198         return $self;
199 }
200
201 sub stop {
202         my ($self) = @_;
203         warn "# stop";
204         $self->ioloop->stop;
205         return $self;
206 }
207
208 sub _send_next_message {
209         my ($self) = @_;
210
211         if ((my $c = $self->{_connection}) && !$self->{_connecting}) {
212                 while (my $message = shift @{$self->{_message_queue}}) {
213                         warn "# write ",dump($message);
214                         $self->ioloop->write($c, $message);
215                 }
216         }
217 }
218
219 sub _on_connect {
220         my ($self, $ioloop, $id) = @_;
221         delete $self->{_connecting};
222
223         $ioloop->connection_timeout($id => $self->timeout);
224
225         $self->_send_next_message;
226 }
227
228 sub _on_error {
229         my ($self, $ioloop, $id, $error) = @_;
230
231         warn "ERROR: $error";
232
233         $self->error($error);
234         $self->_inform_queue;
235
236         $self->on_error->($self);
237
238         $ioloop->drop($id);
239 }
240
241 sub _on_hup {
242         my ($self, $ioloop, $id) = @_;
243
244         $self->{error} ||= 'disconnected';
245         $self->_inform_queue;
246
247         delete $self->{_message_queue};
248
249         delete $self->{_connecting};
250         delete $self->{_connection};
251 }
252
253 sub _inform_queue {
254         my ($self) = @_;
255
256         for my $cb (@{$self->{_cb_queue}}) {
257                 $cb->($self) if $cb;
258         }
259         $self->{_queue} = [];
260 }
261
262 sub _on_read {
263         my ($self, $ioloop, $id, $data) = @_;
264
265         my $cb = shift @{$self->{_cb_queue}};
266         if ($cb) {
267                 Mojo::Util::decode($self->encoding, $data) if $data;
268                 warn "# on read callback with ", dump($data);
269                 $cb->($self, $data);
270         } else {
271                 warn "no callback";
272         }
273
274         # Reset error after callback dispatching
275         $self->error(undef);
276 }
277
278 1;
279 __END__
280
281 =head1 NAME
282
283 MojoX::Gearman - asynchronous Gearman client for L<Mojolicious>.
284
285 =head1 SYNOPSIS
286
287         use MojoX::Gearman;
288
289         my $gearman = MojoX::Gearman->new(server => '127.0.0.1:4730');
290
291 =head1 DESCRIPTION
292
293 L<MojoX::Gearman> is an asynchronous client to Gearman for Mojo.
294
295 =head1 ATTRIBUTES
296
297 L<MojoX::Gearman> implements the following attributes.
298
299 =head2 C<server>
300
301         my $server = $gearman->server;
302         $gearman         = $gearman->server('127.0.0.1:4730');
303
304 C<Gearman> server connection string, defaults to '127.0.0.1:4730'.
305
306 =head2 C<ioloop>
307
308         my $ioloop = $gearman->ioloop;
309         $gearman         = $gearman->ioloop(Mojo::IOLoop->new);
310
311 Loop object to use for io operations, by default a L<Mojo::IOLoop> singleton
312 object will be used.
313
314 =head2 C<timeout>
315
316         my $timeout = $gearman->timeout;
317         $gearman          = $gearman->timeout(100);
318
319 Maximum amount of time in seconds a connection can be inactive before being
320 dropped, defaults to C<300>.
321
322 =head2 C<encoding>
323
324         my $encoding = $gearman->encoding;
325         $gearman           = $gearman->encoding('UTF-8');
326
327 Encoding used for stored data, defaults to C<UTF-8>.
328
329 =head1 METHODS
330
331 =head2 C<req>
332
333         $gearman->req( $type, $data, ..., sub { # callback } );
334
335 =head2 C<error>
336
337         $gearman->execute("ping" => sub {
338                 my ($gearman, $result) = @_;
339                 die $gearman->error unless defined $result;
340         }
341
342 Returns error occured during command execution.
343 Note that this method returns error code just from current command and
344 can be used just in callback.
345
346 =head2 C<on_error>
347
348         $gearman->on_error(sub{
349                 my $gearman = shift;
350                 warn 'Gearman error ', $gearman->error, "\n";
351         });
352
353 Executes if error occured. Called before commands callbacks.
354
355 =head2 C<start>
356
357         $gearman->start;
358
359 Starts IOLoop. Shortcut for $gearman->ioloop->start;
360
361 =head1 SEE ALSO
362
363 L<Gearman::Client>, L<Mojolicious>, L<Mojo::IOLoop>
364
365 =head1 SUPPORT
366
367 =head1 DEVELOPMENT
368
369 =head2 Repository
370
371         https://github.com/dpavlin/mojox-gearman
372
373 =head1 AUTHOR
374
375 Dobrica Pavlinusic, C<dpavlin@rot13.org>.
376
377 =head1 COPYRIGHT AND LICENSE
378
379 Copyright (C) 2010, Dobrica Pavlinusic
380
381 This program is free software, you can gearmantribute it and/or modify it under
382 the terms of the Artistic License version 2.0.
383
384 =cut