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(@_) },
74 return $self->{_connection};
90 $nr2type->{ $packet_type->{$_} } = $_ foreach keys %$packet_type;
94 my ($self,$data) = @_;
95 die "no data in packet" unless $data;
96 my ($magic, $type, $len) = unpack( "a4NN", $data );
97 die "wrong magic [$magic]" unless $magic eq "\0RES";
98 die "unsupported type [$type]" unless exists $nr2type->{$type};
99 die "ERROR" if $type == $packet_type->{ERROR};
100 return ( $type, split("\0", substr($data,12,$len)) );
106 my $data = join("\0", @_);
108 die "can't find packet type $type in ", dump $packet_type unless exists $packet_type->{$type};
109 Mojo::Util::encode($self->encoding, $data) if $self->encoding;
111 $self->{_res} = undef;
115 my ( $self, $data ) = @_;
116 my ( $type, @data ) = $self->parse_packet($data);
117 warn "# <<<< ", $nr2type->{$type}, " ",dump(@data);
119 if ( $type == $packet_type->{JOB_CREATED} ) {
120 push @{ $self->{_cb_queue} }, sub {
121 my ( $self,$data ) = @_;
122 warn "# WORK_COMPLETE ",dump $data;
123 my ( $type, $handle, $out ) = $self->parse_packet($data);
124 die "not WORK_COMPLETE" unless $type == $packet_type->{WORK_COMPLETE};
132 my $out = $#data == 0 ? $data[0] : [ @data ];
137 # $data .= "\0" if $data;
138 my $len = length($data);
139 my $message = pack("a4NN", "\0REQ", $packet_type->{$type}, length $data ) . $data;
140 warn "# >>>> $type ",dump($message);
142 my $mqueue = $self->{_message_queue} ||= [];
143 my $cqueue = $self->{_cb_queue} ||= [];
145 push @$mqueue, $message;
148 $self->connect unless $self->{_connection};
149 $self->_send_next_message;
159 $self->ioloop->start;
170 sub _send_next_message {
173 if ((my $c = $self->{_connection}) && !$self->{_connecting}) {
174 while (my $message = shift @{$self->{_message_queue}}) {
175 warn "# write ",dump($message);
176 $self->ioloop->write($c, $message);
182 my ($self, $ioloop, $id) = @_;
183 delete $self->{_connecting};
185 $ioloop->connection_timeout($id => $self->timeout);
187 $self->_send_next_message;
191 my ($self, $ioloop, $id, $error) = @_;
193 warn "ERROR: $error";
195 $self->error($error);
196 $self->_inform_queue;
198 $self->on_error->($self);
204 my ($self, $ioloop, $id) = @_;
206 $self->{error} ||= 'disconnected';
207 $self->_inform_queue;
209 delete $self->{_message_queue};
211 delete $self->{_connecting};
212 delete $self->{_connection};
218 for my $cb (@{$self->{_cb_queue}}) {
221 $self->{_queue} = [];
225 my ($self, $ioloop, $id, $data) = @_;
227 my $cb = shift @{$self->{_cb_queue}};
229 Mojo::Util::decode($self->encoding, $data) if $data;
230 warn "# on read callback with ", dump($data);
236 # Reset error after callback dispatching
245 MojoX::Gearman - asynchronous Gearman client for L<Mojolicious>.
251 my $gearman = MojoX::Gearman->new(server => '127.0.0.1:4730');
255 L<MojoX::Gearman> is an asynchronous client to Gearman for Mojo.
259 L<MojoX::Gearman> implements the following attributes.
263 my $server = $gearman->server;
264 $gearman = $gearman->server('127.0.0.1:4730');
266 C<Gearman> server connection string, defaults to '127.0.0.1:4730'.
270 my $ioloop = $gearman->ioloop;
271 $gearman = $gearman->ioloop(Mojo::IOLoop->new);
273 Loop object to use for io operations, by default a L<Mojo::IOLoop> singleton
278 my $timeout = $gearman->timeout;
279 $gearman = $gearman->timeout(100);
281 Maximum amount of time in seconds a connection can be inactive before being
282 dropped, defaults to C<300>.
286 my $encoding = $gearman->encoding;
287 $gearman = $gearman->encoding('UTF-8');
289 Encoding used for stored data, defaults to C<UTF-8>.
295 $gearman->req( $type, $data, ..., sub { # callback } );
299 $gearman->execute("ping" => sub {
300 my ($gearman, $result) = @_;
301 die $gearman->error unless defined $result;
304 Returns error occured during command execution.
305 Note that this method returns error code just from current command and
306 can be used just in callback.
310 $gearman->on_error(sub{
312 warn 'Gearman error ', $gearman->error, "\n";
315 Executes if error occured. Called before commands callbacks.
321 Starts IOLoop. Shortcut for $gearman->ioloop->start;
325 L<Gearman::Client>, L<Mojolicious>, L<Mojo::IOLoop>
333 https://github.com/dpavlin/mojox-gearman
337 Dobrica Pavlinusic, C<dpavlin@rot13.org>.
339 =head1 COPYRIGHT AND LICENSE
341 Copyright (C) 2010, Dobrica Pavlinusic
343 This program is free software, you can gearmantribute it and/or modify it under
344 the terms of the Artistic License version 2.0.