/usr/share/perl5/AnyEvent/Gearman/Client.pm is in libanyevent-gearman-perl 0.10-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | package AnyEvent::Gearman::Client;
use Any::Moose;
use AnyEvent::Gearman::Types;
use AnyEvent::Gearman::Task;
use AnyEvent::Gearman::Client::Connection;
has job_servers => (
is => 'rw',
isa => 'AnyEvent::Gearman::Client::Connections',
required => 1,
coerce => 1,
);
has prefix => (
is => 'rw',
isa => 'Str',
default => '',
);
no Any::Moose;
sub add_task {
my $self = shift;
return $self->_add_task('', @_)
}
sub add_task_bg {
my $self = shift;
return $self->_add_task('bg', @_)
}
sub _add_task {
my ($self, $type, $function, $workload, %cb) = @_;
$function = $self->prefix . "\t" . $function
if $self->prefix;
my $task = AnyEvent::Gearman::Task->new( $function, $workload, %cb );
my $retry; ($retry = sub {
my @js = grep { $_->alive } @{ $self->job_servers };
unless (@js) {
$task->event( on_fail => 'no server available' );
undef $retry;
return;
}
# TODO: hashed server selector
my $js = @js[int rand @js];
$js->add_task(
$task,
# task added successfully
sub {
undef $retry;
},
# on error
$retry,
# task type
$type,
);
})->();
$task;
}
__PACKAGE__->meta->make_immutable;
__END__
=for stopwords Str namespace
=head1 NAME
AnyEvent::Gearman::Client - Gearman client for AnyEvent application
=head1 SYNOPSIS
use AnyEvent::Gearman::Client;
# create greaman client
my $gearman = AnyEvent::Gearman::Client->new(
job_servers => ['127.0.0.1', '192.168.0.1:123'],
);
# start job
$gearman->add_task(
$function => $workload,
on_complete => sub {
my $res = $_[1];
},
on_fail => sub {
# job failed
},
);
# start background job
$gearman->add_task_bg(
$function => $workload,
);
=head1 DESCRIPTION
This is Gearman client module for AnyEvent applications.
=head1 SEE ALSO
L<Gearman::Client::Async>, this module provides same functionality for L<Danga::Socket> applications.
=head1 METHODS
=head2 new(%options)
Create gearman client object.
my $gearman = AnyEvent::Gearman::Client->new(
job_servers => ['127.0.0.1', '192.168.0.1:123'],
);
Available options are:
=over 4
=item job_servers => 'ArrayRef',
List of gearman servers. 'host:port' or just 'host' formats are allowed.
In latter case, gearman default port 4730 will be used.
You should set at least one job_server.
=item prefix => 'Str',
Sets the namespace / prefix for the function names. This is useful for sharing job servers between different applications or different instances of the same application (different development sandboxes for example).
The namespace is currently implemented as a simple tab separated concatenation of the prefix and the function name.
=back
=head2 add_task($function, $workload, %callbacks)
Start new job and wait results in C<%callbacks>
$gearman->add_task(
$function => $workload,
on_complete => sub {
my $result = $_[1],
},
on_fail => sub {
# job failled
},
);
C<$function> is a worker function name, and C<$workload> is a data that will be passed to worker.
C<%callbacks> is set of callbacks called by job events. Available callbacks are:
=over 4
=item on_complete => $cb->($self, $result)
Called when the job is completed. C<$result> is some results data which is set by C<< $job->complete($result) >> in worker.
=item on_fail => $cb->($self, $reason)
Called when the job is failed. C<$reason> is empty if its threw by worker. I don't know why but gearman spec say so. Considering to use C<on_warning> below for some failing notify.
=item on_warning => $cb->($self, $warning)
Called when C<< $job->warning($warning) >> called in worker.
=item on_data => $cb->($self, $data)
Called when C<< $job->data($data) >> called in worker.
=item on_status => $cb->($self, $numerator, $denominator)
Called when C<< $job->status($numerator, $denominator) >> called in worker
=item on_created => $cb->($self)
Called when the servers reports that the task was created successfully.
Updates the Task object with the server assigned C<job_handle>.
=back
You should to set C<on_complete> and C<on_fail> at least.
=head2 add_task_bg($function, $workload, %callbacks)
Starts a new background job. The parameters are the same as
L<add_task($function, $workload, %callbacks)|add_task()>, but the only
callback that is called is C<on_created>.
$gearman->add_task_bg(
$function => $workload,
on_created => sub {
my ($task) = @_;
},
);
=head1 AUTHOR
Daisuke Murase <typester@cpan.org>
Pedro Melo <melo@cpan.org>
=head1 COPYRIGHT AND LICENSE
Copyright (c) 2009 by KAYAC Inc.
This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.
The full text of the license can be found in the
LICENSE file included with this module.
=cut
|