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};
88 $nr2type->{ $packet_type->{$_} } = $_ foreach keys %$packet_type;
92 my ($self,$data) = @_;
93 my ($magic, $type, $len) = unpack( "a4NN", $data );
94 die "wrong magic [$magic]" unless $magic eq "\0RES";
95 die "unsupported type [$type]" unless exists $nr2type->{$type};
96 die "ERROR" if $type == $packet_type->{ERROR};
97 return ( $type, split("\0", substr($data,12,$len)) );
101 my ( $self, $value ) = @_;
102 if ( defined $value ) {
103 warn "# ++ res = ",dump $value;
104 $self->{_res} ||= $value;
106 return $self->{_res};
112 my $data = join("\0", @_);
114 Mojo::Util::encode($self->encoding, $data) if $self->encoding;
116 $self->{_res} = undef;
120 my ( $self, $data ) = @_;
122 warn "# <<<< ",dump($data);
123 my ( $type, @data ) = $self->parse_packet($data);
125 if ( $type == $packet_type->{JOB_CREATED} ) {
126 push @{ $self->{_cb_queue} }, sub {
127 my ( $self,$data ) = @_;
128 warn "# WORK_COMPLETE ",dump $data;
129 my ( $type, $handle, $ret ) = $self->parse_packet($data);
130 die "not WORK_COMPLETE" unless $type == $packet_type->{WORK_COMPLETE};
134 $self->start; # FIXME sync client
137 $self->res( $#data == 0 ? $data[0] : [ @data ] );
140 my $len = length($data);
141 my $message = pack("a4NN", "\0REQ", $type, length $data ) . $data;
142 warn "# >>>> ",dump($data);
144 my $mqueue = $self->{_message_queue} ||= [];
145 my $cqueue = $self->{_cb_queue} ||= [];
147 push @$mqueue, $message;
150 $self->connect unless $self->{_connection};
151 $self->_send_next_message;
153 $self->ioloop->start;
161 $self->ioloop->start;
172 sub _send_next_message {
175 if ((my $c = $self->{_connection}) && !$self->{_connecting}) {
176 while (my $message = shift @{$self->{_message_queue}}) {
177 warn "# write ",dump($message);
178 $self->ioloop->write($c, $message);
184 my ($self, $ioloop, $id) = @_;
185 delete $self->{_connecting};
187 $ioloop->connection_timeout($id => $self->timeout);
189 $self->_send_next_message;
193 my ($self, $ioloop, $id, $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 );
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.