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};
77 my ( $self, $type, $data ) = @_;
79 Mojo::Util::encode($self->encoding, $data) if $self->encoding;
84 my ( $self, $data ) = @_;
86 warn "# <<<< ",dump($data);
87 my ($magic, $type, $len) = unpack( "a4NN", $data );
88 die "wrong magic [$magic]" unless $magic eq "\0RES";
93 my $len = length($data);
94 my $message = pack("a4NN", "\0REQ", 16, length $data ) . $data;
95 warn "# >>>> ",dump($data);
97 my $mqueue = $self->{_message_queue} ||= [];
98 my $cqueue = $self->{_cb_queue} ||= [];
101 push @$mqueue, $message;
104 $self->connect unless $self->{_connection};
105 $self->_send_next_message;
107 $self->ioloop->start;
115 $self->ioloop->start;
126 sub _send_next_message {
129 if ((my $c = $self->{_connection}) && !$self->{_connecting}) {
130 while (my $message = shift @{$self->{_message_queue}}) {
131 warn "# write ",dump($message);
132 $self->ioloop->write($c, $message);
138 my ($self, $ioloop, $id) = @_;
139 delete $self->{_connecting};
141 $ioloop->connection_timeout($id => $self->timeout);
143 $self->_send_next_message;
147 my ($self, $ioloop, $id, $error) = @_;
149 $self->error($error);
150 $self->_inform_queue;
152 $self->on_error->($self);
158 my ($self, $ioloop, $id) = @_;
160 $self->{error} ||= 'disconnected';
161 $self->_inform_queue;
163 delete $self->{_message_queue};
165 delete $self->{_connecting};
166 delete $self->{_connection};
172 for my $cb (@{$self->{_cb_queue}}) {
175 $self->{_queue} = [];
179 my ($self, $ioloop, $id, $data) = @_;
181 my $cb = shift @{$self->{_cb_queue}};
183 Mojo::Util::decode($self->encoding, $data) if $data;
184 warn "# on read callback with ", dump($data);
190 # Reset error after callback dispatching
199 MojoX::Gearman - asynchronous Gearman client for L<Mojolicious>.
205 my $gearman = MojoX::Gearman->new(server => '127.0.0.1:4730');
209 L<MojoX::Gearman> is an asynchronous client to Gearman for Mojo.
213 L<MojoX::Gearman> implements the following attributes.
217 my $server = $gearman->server;
218 $gearman = $gearman->server('127.0.0.1:4730');
220 C<Gearman> server connection string, defaults to '127.0.0.1:4730'.
224 my $ioloop = $gearman->ioloop;
225 $gearman = $gearman->ioloop(Mojo::IOLoop->new);
227 Loop object to use for io operations, by default a L<Mojo::IOLoop> singleton
232 my $timeout = $gearman->timeout;
233 $gearman = $gearman->timeout(100);
235 Maximum amount of time in seconds a connection can be inactive before being
236 dropped, defaults to C<300>.
240 my $encoding = $gearman->encoding;
241 $gearman = $gearman->encoding('UTF-8');
243 Encoding used for stored data, defaults to C<UTF-8>.
249 $gearman->req( $type, $data );
253 $gearman->execute("ping" => sub {
254 my ($gearman, $result) = @_;
255 die $gearman->error unless defined $result;
258 Returns error occured during command execution.
259 Note that this method returns error code just from current command and
260 can be used just in callback.
264 $gearman->on_error(sub{
266 warn 'Gearman error ', $gearman->error, "\n";
269 Executes if error occured. Called before commands callbacks.
275 Starts IOLoop. Shortcut for $gearman->ioloop->start;
279 L<Gearman::Client>, L<Mojolicious>, L<Mojo::IOLoop>
287 https://github.com/dpavlin/mojox-gearman
291 Dobrica Pavlinusic, C<dpavlin@rot13.org>.
293 =head1 COPYRIGHT AND LICENSE
295 Copyright (C) 2010, Dobrica Pavlinusic
297 This program is free software, you can gearmantribute it and/or modify it under
298 the terms of the Artistic License version 2.0.