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};
103 $nr2type->{ $packet_type->{$_} } = $_ foreach keys %$packet_type;
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)) );
118 warn "XXX req ",dump(@_);
120 my $callback = pop @_ if ref $_[$#_] eq 'CODE';
121 my $data = join("\0", @_);
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;
126 $self->{_res} = undef;
130 my ( $self, $data ) = @_;
131 my ( $type, @data ) = $self->parse_packet($data);
132 warn "# <<<< ", $nr2type->{$type}, " ",dump(@data);
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;
147 } elsif ( $type == $packet_type->{NO_JOB} ) {
148 $self->req( 'PRE_SLEEP' );
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' );
164 my $out = $#data == 0 ? $data[0] : [ @data ];
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);
174 my $mqueue = $self->{_message_queue} ||= [];
175 my $cqueue = $self->{_cb_queue} ||= [];
177 push @$mqueue, $message;
180 $self->connect unless $self->{_connection};
181 $self->_send_next_message;
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
197 $self->ioloop->start;
208 sub _send_next_message {
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);
220 my ($self, $ioloop, $id) = @_;
221 delete $self->{_connecting};
223 $ioloop->connection_timeout($id => $self->timeout);
225 $self->_send_next_message;
229 my ($self, $ioloop, $id, $error) = @_;
231 warn "ERROR: $error";
233 $self->error($error);
234 $self->_inform_queue;
236 $self->on_error->($self);
242 my ($self, $ioloop, $id) = @_;
244 $self->{error} ||= 'disconnected';
245 $self->_inform_queue;
247 delete $self->{_message_queue};
249 delete $self->{_connecting};
250 delete $self->{_connection};
256 for my $cb (@{$self->{_cb_queue}}) {
259 $self->{_queue} = [];
263 my ($self, $ioloop, $id, $data) = @_;
265 my $cb = shift @{$self->{_cb_queue}};
267 Mojo::Util::decode($self->encoding, $data) if $data;
268 warn "# on read callback with ", dump($data);
274 # Reset error after callback dispatching
283 MojoX::Gearman - asynchronous Gearman client for L<Mojolicious>.
289 my $gearman = MojoX::Gearman->new(server => '127.0.0.1:4730');
293 L<MojoX::Gearman> is an asynchronous client to Gearman for Mojo.
297 L<MojoX::Gearman> implements the following attributes.
301 my $server = $gearman->server;
302 $gearman = $gearman->server('127.0.0.1:4730');
304 C<Gearman> server connection string, defaults to '127.0.0.1:4730'.
308 my $ioloop = $gearman->ioloop;
309 $gearman = $gearman->ioloop(Mojo::IOLoop->new);
311 Loop object to use for io operations, by default a L<Mojo::IOLoop> singleton
316 my $timeout = $gearman->timeout;
317 $gearman = $gearman->timeout(100);
319 Maximum amount of time in seconds a connection can be inactive before being
320 dropped, defaults to C<300>.
324 my $encoding = $gearman->encoding;
325 $gearman = $gearman->encoding('UTF-8');
327 Encoding used for stored data, defaults to C<UTF-8>.
333 $gearman->req( $type, $data, ..., sub { # callback } );
337 $gearman->execute("ping" => sub {
338 my ($gearman, $result) = @_;
339 die $gearman->error unless defined $result;
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.
348 $gearman->on_error(sub{
350 warn 'Gearman error ', $gearman->error, "\n";
353 Executes if error occured. Called before commands callbacks.
359 Starts IOLoop. Shortcut for $gearman->ioloop->start;
363 L<Gearman::Client>, L<Mojolicious>, L<Mojo::IOLoop>
371 https://github.com/dpavlin/mojox-gearman
375 Dobrica Pavlinusic, C<dpavlin@rot13.org>.
377 =head1 COPYRIGHT AND LICENSE
379 Copyright (C) 2010, Dobrica Pavlinusic
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.