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->singleton });
18 __PACKAGE__->attr(error => undef);
19 __PACKAGE__->attr(timeout => 300);
20 __PACKAGE__->attr(encoding => 'UTF-8');
25 warn "Gearman error: ", $gearman->error, "\n";
34 return unless my $loop = $self->ioloop;
37 $loop->drop($self->{_connection})
38 if $self->{_connection};
45 if ($self->connected) {
46 $self->ioloop->drop($self->{_connection});
49 $self->server =~ m{^([^:]+)(:(\d+))?};
51 my $port = $3 || 4730;
53 Scalar::Util::weaken $self;
56 $self->{_connecting} = 1;
57 $self->{_connection} = $self->ioloop->connect(
58 { address => $address,
60 on_connect => sub { $self->_on_connect(@_) },
61 on_read => sub { $self->_on_read(@_) },
62 on_error => sub { $self->_on_error(@_) },
63 on_hup => sub { $self->_on_hup(@_) },
73 return $self->{_connection};
89 $nr2type->{ $packet_type->{$_} } = $_ foreach keys %$packet_type;
93 my ($self,$data) = @_;
94 my ($magic, $type, $len) = unpack( "a4NN", $data );
95 die "wrong magic [$magic]" unless $magic eq "\0RES";
96 die "unsupported type [$type]" unless exists $nr2type->{$type};
97 die "ERROR" if $type == $packet_type->{ERROR};
98 return ( $type, split("\0", substr($data,12,$len)) );
102 my ( $self, $value ) = @_;
103 if ( defined $value ) {
104 warn "# ++ res = ",dump $value;
105 $self->{_res} ||= $value;
107 return $self->{_res};
113 my $data = join("\0", @_);
115 die "can't find packet type $type in ", dump $packet_type unless exists $packet_type->{$type};
116 Mojo::Util::encode($self->encoding, $data) if $self->encoding;
118 $self->{_res} = undef;
122 my ( $self, $data ) = @_;
124 warn "# <<<< ",dump($data);
125 my ( $type, @data ) = $self->parse_packet($data);
127 if ( $type == $packet_type->{JOB_CREATED} ) {
128 push @{ $self->{_cb_queue} }, sub {
129 my ( $self,$data ) = @_;
130 warn "# WORK_COMPLETE ",dump $data;
131 my ( $type, $handle, $out ) = $self->parse_packet($data);
132 die "not WORK_COMPLETE" unless $type == $packet_type->{WORK_COMPLETE};
136 $self->start; # FIXME sync client
139 my $out = $#data == 0 ? $data[0] : [ @data ];
144 $data .= "\0" if $data;
145 my $len = length($data);
146 my $message = pack("a4NN", "\0REQ", $packet_type->{$type}, length $data ) . $data;
147 warn "# >>>> ",dump($message);
149 my $mqueue = $self->{_message_queue} ||= [];
150 my $cqueue = $self->{_cb_queue} ||= [];
152 push @$mqueue, $message;
155 $self->connect unless $self->{_connection};
156 $self->_send_next_message;
158 $self->ioloop->start;
166 $self->ioloop->start;
177 sub _send_next_message {
180 if ((my $c = $self->{_connection}) && !$self->{_connecting}) {
181 while (my $message = shift @{$self->{_message_queue}}) {
182 warn "# write ",dump($message);
183 $self->ioloop->write($c, $message);
189 my ($self, $ioloop, $id) = @_;
190 delete $self->{_connecting};
192 $ioloop->connection_timeout($id => $self->timeout);
194 $self->_send_next_message;
198 my ($self, $ioloop, $id, $error) = @_;
200 $self->error($error);
201 $self->_inform_queue;
203 $self->on_error->($self);
209 my ($self, $ioloop, $id) = @_;
211 $self->{error} ||= 'disconnected';
212 $self->_inform_queue;
214 delete $self->{_message_queue};
216 delete $self->{_connecting};
217 delete $self->{_connection};
223 for my $cb (@{$self->{_cb_queue}}) {
226 $self->{_queue} = [];
230 my ($self, $ioloop, $id, $data) = @_;
232 my $cb = shift @{$self->{_cb_queue}};
234 Mojo::Util::decode($self->encoding, $data) if $data;
235 warn "# on read callback with ", dump($data);
241 # Reset error after callback dispatching
250 MojoX::Gearman - asynchronous Gearman client for L<Mojolicious>.
256 my $gearman = MojoX::Gearman->new(server => '127.0.0.1:4730');
260 L<MojoX::Gearman> is an asynchronous client to Gearman for Mojo.
264 L<MojoX::Gearman> implements the following attributes.
268 my $server = $gearman->server;
269 $gearman = $gearman->server('127.0.0.1:4730');
271 C<Gearman> server connection string, defaults to '127.0.0.1:4730'.
275 my $ioloop = $gearman->ioloop;
276 $gearman = $gearman->ioloop(Mojo::IOLoop->new);
278 Loop object to use for io operations, by default a L<Mojo::IOLoop> singleton
283 my $timeout = $gearman->timeout;
284 $gearman = $gearman->timeout(100);
286 Maximum amount of time in seconds a connection can be inactive before being
287 dropped, defaults to C<300>.
291 my $encoding = $gearman->encoding;
292 $gearman = $gearman->encoding('UTF-8');
294 Encoding used for stored data, defaults to C<UTF-8>.
300 $gearman->req( $type, $data, ..., sub { # callback } );
304 $gearman->execute("ping" => sub {
305 my ($gearman, $result) = @_;
306 die $gearman->error unless defined $result;
309 Returns error occured during command execution.
310 Note that this method returns error code just from current command and
311 can be used just in callback.
315 $gearman->on_error(sub{
317 warn 'Gearman error ', $gearman->error, "\n";
320 Executes if error occured. Called before commands callbacks.
326 Starts IOLoop. Shortcut for $gearman->ioloop->start;
330 L<Gearman::Client>, L<Mojolicious>, L<Mojo::IOLoop>
338 https://github.com/dpavlin/mojox-gearman
342 Dobrica Pavlinusic, C<dpavlin@rot13.org>.
344 =head1 COPYRIGHT AND LICENSE
346 Copyright (C) 2010, Dobrica Pavlinusic
348 This program is free software, you can gearmantribute it and/or modify it under
349 the terms of the Artistic License version 2.0.