Gearman/Client.pm

filter: Statementstmt 33.3% 1 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 50.0% 2 / 4 CC 1 SCAR 2.6
CC
1
Cov
33%
BEGIN
1.3
CRAP 1.3
« Gearman/Types.pm ↑ Summary Gearman/Worker.pm »
1 package AnyEvent::Gearman::Client;
2 0 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=2.6
3  
4 use AnyEvent::Gearman::Types;
5 use AnyEvent::Gearman::Task;
6 use AnyEvent::Gearman::Client::Connection;
7  
8 has job_servers => (
9 is => 'rw',
10 isa => 'AnyEvent::Gearman::Client::Connections',
11 required => 1,
12 coerce => 1,
13 );
14  
15 has prefix => (
16 is => 'rw',
17 isa => 'Str',
18 default => '',
19 );
20  
21 no Any::Moose;
22  
23 sub add_task {
24 my $self = shift;
25
26 return $self->_add_task('', @_)
27 }
28  
29 sub add_task_bg {
30 my $self = shift;
31
32 return $self->_add_task('bg', @_)
33 }
34  
35 sub _add_task {
36 my ($self, $type, $function, $workload, %cb) = @_;
37  
38 $function = $self->prefix . "\t" . $function
39 if $self->prefix;
40  
41 my $task = AnyEvent::Gearman::Task->new( $function, $workload, %cb );
42  
43 my $retry; ($retry = sub {
44 my @js = grep { $_->alive } @{ $self->job_servers };
45  
46 unless (@js) {
47 $task->event( on_fail => 'no server available' );
48 undef $retry;
49 return;
50 }
51  
52 # TODO: hashed server selector
53 my $js = @js[int rand @js];
54 $js->add_task(
55 $task,
56  
57 # task added successfully
58 sub {
59 undef $retry;
60 },
61  
62 # on error
63 $retry,
64
65 # task type
66 $type,
67 );
68 })->();
69  
70 $task;
71 }
72  
73 __PACKAGE__->meta->make_immutable;
74  
75 __END__
« Gearman/Types.pm ↑ Summary Gearman/Worker.pm »