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};
79 my $data = join("\0", @_);
81 Mojo::Util::encode($self->encoding, $data) if $self->encoding;
86 my ( $self, $data ) = @_;
88 warn "# <<<< ",dump($data);
89 my ($magic, $type, $len) = unpack( "a4NN", $data );
90 die "wrong magic [$magic]" unless $magic eq "\0RES";
91 $ret = substr($data,12,$len);
94 my $len = length($data);
95 my $message = pack("a4NN", "\0REQ", $type, length $data ) . $data;
96 warn "# >>>> ",dump($data);
98 my $mqueue = $self->{_message_queue} ||= [];
99 my $cqueue = $self->{_cb_queue} ||= [];
102 push @$mqueue, $message;
105 $self->connect unless $self->{_connection};
106 $self->_send_next_message;
108 $self->ioloop->start;
116 $self->ioloop->start;
127 sub _send_next_message {
130 if ((my $c = $self->{_connection}) && !$self->{_connecting}) {
131 while (my $message = shift @{$self->{_message_queue}}) {
132 warn "# write ",dump($message);
133 $self->ioloop->write($c, $message);
139 my ($self, $ioloop, $id) = @_;
140 delete $self->{_connecting};
142 $ioloop->connection_timeout($id => $self->timeout);
144 $self->_send_next_message;
148 my ($self, $ioloop, $id, $error) = @_;
150 $self->error($error);
151 $self->_inform_queue;
153 $self->on_error->($self);
159 my ($self, $ioloop, $id) = @_;
161 $self->{error} ||= 'disconnected';
162 $self->_inform_queue;
164 delete $self->{_message_queue};
166 delete $self->{_connecting};
167 delete $self->{_connection};
173 for my $cb (@{$self->{_cb_queue}}) {
176 $self->{_queue} = [];
180 my ($self, $ioloop, $id, $data) = @_;
182 my $cb = shift @{$self->{_cb_queue}};
184 Mojo::Util::decode($self->encoding, $data) if $data;
185 warn "# on read callback with ", dump($data);
191 # Reset error after callback dispatching
200 MojoX::Gearman - asynchronous Gearman client for L<Mojolicious>.
206 my $gearman = MojoX::Gearman->new(server => '127.0.0.1:4730');
210 L<MojoX::Gearman> is an asynchronous client to Gearman for Mojo.
214 L<MojoX::Gearman> implements the following attributes.
218 my $server = $gearman->server;
219 $gearman = $gearman->server('127.0.0.1:4730');
221 C<Gearman> server connection string, defaults to '127.0.0.1:4730'.
225 my $ioloop = $gearman->ioloop;
226 $gearman = $gearman->ioloop(Mojo::IOLoop->new);
228 Loop object to use for io operations, by default a L<Mojo::IOLoop> singleton
233 my $timeout = $gearman->timeout;
234 $gearman = $gearman->timeout(100);
236 Maximum amount of time in seconds a connection can be inactive before being
237 dropped, defaults to C<300>.
241 my $encoding = $gearman->encoding;
242 $gearman = $gearman->encoding('UTF-8');
244 Encoding used for stored data, defaults to C<UTF-8>.
250 $gearman->req( $type, $data );
254 $gearman->execute("ping" => sub {
255 my ($gearman, $result) = @_;
256 die $gearman->error unless defined $result;
259 Returns error occured during command execution.
260 Note that this method returns error code just from current command and
261 can be used just in callback.
265 $gearman->on_error(sub{
267 warn 'Gearman error ', $gearman->error, "\n";
270 Executes if error occured. Called before commands callbacks.
276 Starts IOLoop. Shortcut for $gearman->ioloop->start;
280 L<Gearman::Client>, L<Mojolicious>, L<Mojo::IOLoop>
288 https://github.com/dpavlin/mojox-gearman
292 Dobrica Pavlinusic, C<dpavlin@rot13.org>.
294 =head1 COPYRIGHT AND LICENSE
296 Copyright (C) 2010, Dobrica Pavlinusic
298 This program is free software, you can gearmantribute it and/or modify it under
299 the terms of the Artistic License version 2.0.