1 package MojoX::Gearman;
13 use Data::Dump qw(dump);
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');
25 warn "Gearman error: ", $gearman->error, "\n";
29 __PACKAGE__->attr(res => undef);
35 return unless my $loop = $self->ioloop;
38 $loop->drop($self->{_connection})
39 if $self->{_connection};
46 if ($self->connected) {
47 $self->ioloop->drop($self->{_connection});
50 $self->server =~ m{^([^:]+)(:(\d+))?};
52 my $port = $3 || 4730;
54 Scalar::Util::weaken $self;
57 $self->{_connecting} = 1;
58 $self->{_connection} = $self->ioloop->connect(
59 { address => $address,
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(@_) },
68 warn "# using gearman server $address:$port\n";
76 return $self->{_connection};
102 $nr2type->{ $packet_type->{$_} } = $_ foreach keys %$packet_type;
106 my ($self,$data) = @_;
107 die "no data in packet" unless $data;
108 my ($magic, $type, $len) = unpack( "a4NN", $data );
109 die "wrong magic [$magic]" unless $magic eq "\0RES";
110 die "unsupported type [$type]" unless exists $nr2type->{$type};
111 die "ERROR" if $type == $packet_type->{ERROR};
112 return ( $type, split("\0", substr($data,12,$len)) );
117 warn "XXX req ",dump(@_);
119 my $callback = pop @_ if ref $_[$#_] eq 'CODE';
120 my $data = join("\0", @_);
122 die "can't find packet type $type in ", dump $packet_type unless exists $packet_type->{$type};
123 Mojo::Util::encode($self->encoding, $data) if $self->encoding;
125 $self->{_res} = undef;
129 my ( $self, $data ) = @_;
130 my ( $type, @data ) = $self->parse_packet($data);
131 warn "# <<<< ", $nr2type->{$type}, " ",dump(@data);
133 if ( $type == $packet_type->{JOB_CREATED} ) {
134 push @{ $self->{_cb_queue} }, sub {
135 my ( $self,$data ) = @_;
136 warn "# WORK_COMPLETE ",dump $data;
137 my ( $type, $handle, $out ) = $self->parse_packet($data);
138 die "not WORK_COMPLETE" unless $type == $packet_type->{WORK_COMPLETE};
142 } elsif ( $type == $packet_type->{NO_JOB} ) {
143 $self->req( 'PRE_SLEEP' );
145 } elsif ( $type == $packet_type->{JOB_ASSIGN} ) {
146 my ( $handle, $function, $workload ) = @data;
147 my $callback = $self->{_function}->{$function};
148 die "no $function callback" unless ref $callback eq 'CODE';
149 my $out = $callback->( $workload );
150 warn "## $data $callback = ", dump $out;
151 $self->req( 'WORK_COMPLETE', $handle, $out );
152 $self->req( 'GRAB_JOB' );
153 } elsif ( $type == $packet_type->{NOOP} ) {
154 $self->req( 'GRAB_JOB' );
159 my $out = $#data == 0 ? $data[0] : [ @data ];
164 # $data .= "\0" if $data;
165 my $len = length($data);
166 my $message = pack("a4NN", "\0REQ", $packet_type->{$type}, length $data ) . $data;
167 warn "# >>>> $type ",dump($message);
169 my $mqueue = $self->{_message_queue} ||= [];
170 my $cqueue = $self->{_cb_queue} ||= [];
172 push @$mqueue, $message;
175 $self->connect unless $self->{_connection};
176 $self->_send_next_message;
178 if ( $type eq 'CAN_DO' ) {
179 $self->{_function}->{$data} = $callback;
180 $self->res( $callback );
181 warn "# installed $data callback $callback";
182 } elsif ( $type =~ m/^(ECHO_REQ|SUBMIT_JOB|GRAB_JOB)$/ ) { # sync commands
192 $self->ioloop->start;
203 sub _send_next_message {
206 if ((my $c = $self->{_connection}) && !$self->{_connecting}) {
207 while (my $message = shift @{$self->{_message_queue}}) {
208 warn "# write ",dump($message);
209 $self->ioloop->write($c, $message);
215 my ($self, $ioloop, $id) = @_;
216 delete $self->{_connecting};
218 $ioloop->connection_timeout($id => $self->timeout);
220 $self->_send_next_message;
224 my ($self, $ioloop, $id, $error) = @_;
226 warn "ERROR: $error";
228 $self->error($error);
229 $self->_inform_queue;
231 $self->on_error->($self);
237 my ($self, $ioloop, $id) = @_;
239 $self->{error} ||= 'disconnected';
240 $self->_inform_queue;
242 delete $self->{_message_queue};
244 delete $self->{_connecting};
245 delete $self->{_connection};
251 for my $cb (@{$self->{_cb_queue}}) {
254 $self->{_queue} = [];
258 my ($self, $ioloop, $id, $data) = @_;
260 my $cb = shift @{$self->{_cb_queue}};
262 Mojo::Util::decode($self->encoding, $data) if $data;
263 warn "# on read callback with ", dump($data);
269 # Reset error after callback dispatching
278 MojoX::Gearman - asynchronous Gearman client for L<Mojolicious>.
284 my $gearman = MojoX::Gearman->new(server => '127.0.0.1:4730');
288 L<MojoX::Gearman> is an asynchronous client to Gearman for Mojo.
292 L<MojoX::Gearman> implements the following attributes.
296 my $server = $gearman->server;
297 $gearman = $gearman->server('127.0.0.1:4730');
299 C<Gearman> server connection string, defaults to '127.0.0.1:4730'.
303 my $ioloop = $gearman->ioloop;
304 $gearman = $gearman->ioloop(Mojo::IOLoop->new);
306 Loop object to use for io operations, by default a L<Mojo::IOLoop> singleton
311 my $timeout = $gearman->timeout;
312 $gearman = $gearman->timeout(100);
314 Maximum amount of time in seconds a connection can be inactive before being
315 dropped, defaults to C<300>.
319 my $encoding = $gearman->encoding;
320 $gearman = $gearman->encoding('UTF-8');
322 Encoding used for stored data, defaults to C<UTF-8>.
328 $gearman->req( $type, $data, ..., sub { # callback } );
332 $gearman->execute("ping" => sub {
333 my ($gearman, $result) = @_;
334 die $gearman->error unless defined $result;
337 Returns error occured during command execution.
338 Note that this method returns error code just from current command and
339 can be used just in callback.
343 $gearman->on_error(sub{
345 warn 'Gearman error ', $gearman->error, "\n";
348 Executes if error occured. Called before commands callbacks.
354 Starts IOLoop. Shortcut for $gearman->ioloop->start;
358 L<Gearman::Client>, L<Mojolicious>, L<Mojo::IOLoop>
366 https://github.com/dpavlin/mojox-gearman
370 Dobrica Pavlinusic, C<dpavlin@rot13.org>.
372 =head1 COPYRIGHT AND LICENSE
374 Copyright (C) 2010, Dobrica Pavlinusic
376 This program is free software, you can gearmantribute it and/or modify it under
377 the terms of the Artistic License version 2.0.