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
|
package AnyEvent::Gearman::Client::Connection;
use Any::Moose;
use Scalar::Util 'weaken';
extends 'AnyEvent::Gearman::Connection';
no Any::Moose;
sub add_task {
my ($self, $task, $on_complete, $on_error, $type) = @_;
$self->add_on_ready(
sub {
push @{ $self->_need_handle }, $task;
$self->handler->push_write( $task->pack_req($type) );
$on_complete->();
},
$on_error,
);
weaken($self);
return;
}
sub process_work { # common handler for WORK_*
my ($self, $len, $cb) = @_;
my $handle = $self->handler;
$handle->unshift_read( line => "\0", sub {
my $job_handle = $_[1];
$len -= length($job_handle) + 1;
$_[0]->unshift_read( chunk => $len, sub {
$cb->( $job_handle, $_[1] );
});
});
}
sub process_packet_8 { # JOB_CREATED
my ($self, $len) = @_;
my $handle = $self->handler;
$handle->unshift_read( chunk => $len, sub {
my $job_handle = $_[1];
my $task = shift @{ $self->_need_handle } or return;
$task->job_handle($job_handle);
$self->_job_handles->{ $job_handle } = $task;
$task->event( 'on_created' );
});
weaken $self;
}
sub process_packet_12 { # WORK_STATUS
my ($self, $len) = @_;
my $handle = $self->handler;
$handle->unshift_read( line => "\0", sub {
my $job_handle = $_[1];
$len -= length($_[1]) + 1;
$_[0]->unshift_read( line => "\0", sub {
my $numerator = $_[1];
$len -= length($_[1]) + 1;
$_[0]->unshift_read( chunk => $len, sub {
my $denominator = $_[1];
my $task = $self->_job_handles->{ $job_handle } or return;
$task->event( on_status => $numerator, $denominator );
});
});
});
weaken $self;
}
sub process_packet_13 { # WORK_COMPLETE
my ($self) = @_;
push @_, sub {
my ($job_handle, $data) = @_;
my $task = delete $self->_job_handles->{ $job_handle } or return;
$task->event( on_complete => $data );
};
weaken $self;
goto \&process_work;
}
sub process_packet_14 { # WORK_FAIL
my ($self, $len) = @_;
my $handle = $self->handler;
$handle->unshift_read( chunk => $len, sub {
my $job_handle = $_[1];
my $task = delete $self->_job_handles->{ $job_handle } or return;
$task->event('on_fail');
});
weaken $self;
}
sub process_packet_25 { # WORK_EXCEPTION
my ($self) = @_;
push @_, sub {
my ($job_handle, $data) = @_;
my $task = $self->_job_handles->{ $job_handle } or return;
$task->event( on_exception => $data );
};
Scalar::Util::weaken($self);
goto \&process_work;
}
sub process_packet_28 { # WORK_DATA
my ($self) = @_;
push @_, sub {
my ($job_handle, $data) = @_;
my $task = $self->_job_handles->{ $job_handle } or return;
$task->event( on_data => $data );
};
weaken $self;
goto \&process_work;
}
sub process_packet_29 { # WORK_WARNING
my ($self) = @_;
push @_, sub {
my ($job_handle, $data) = @_;
my $task = $self->_job_handles->{ $job_handle } or return;
$task->event( on_warning => $data );
};
weaken $self;
goto \&process_work;
}
__PACKAGE__->meta->make_immutable;
__END__
=head1 NAME
AnyEvent::Gearman::Client::Connection - connection class for client
=head1 METHODS
=head2 add_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
|