| 1 | package Cantella::Worker::Role::Beanstalk; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use Moose::Role; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 1 | ▶ | use List::Util 'shuffle'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 1 | ▶ | use MooseX::Types::Common::Numeric qw/PositiveInt/; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 1 | our $VERSION = '0.001000'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | 1 | has beanstalk_clients => ( | |||||||||||||
| 10 | is => 'ro', | ||||||||||||||
| 11 | isa => 'ArrayRef[Beanstalk::Client]', | ||||||||||||||
| 12 | required => 1, | ||||||||||||||
| 13 | ); | ||||||||||||||
| 14 | |||||||||||||||
| 15 | 1 | has reserve_timeout => ( | |||||||||||||
| 16 | is => 'ro', | ||||||||||||||
| 17 | isa => PositiveInt, | ||||||||||||||
| 18 | predicate => 'has_reserve_timeout', | ||||||||||||||
| 19 | ); | ||||||||||||||
| 20 | |||||||||||||||
| 21 | 1 | has max_tries => ( | |||||||||||||
| 22 | is => 'ro', | ||||||||||||||
| 23 | isa => PositiveInt, | ||||||||||||||
| 24 | predicate => 'has_max_tries', | ||||||||||||||
| 25 | ); | ||||||||||||||
| 26 | |||||||||||||||
| 27 | has delete_on_max_tries => ( | ||||||||||||||
| 28 | is => 'ro', | ||||||||||||||
| 29 | isa => 'Bool', | ||||||||||||||
| 30 | required => 1, | ||||||||||||||
| 31 | 0 | ▶ | default => sub{ 0 }, | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 32 | 1 | ); | |||||||||||||
| 33 | |||||||||||||||
| 34 | 0 | ▶ | after _start => sub { | ||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||
| 35 | 0 | my $self = shift; | |||||||||||||
| 36 | 0 | for my $client ( @{ $self->beanstalk_clients } ){ | |||||||||||||
| 37 | 0 | $client->disconnect; | |||||||||||||
| 38 | 0 | ▶ | unless( $client->connect ){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 39 | 0 | $self->error( $client->error ); | |||||||||||||
| 40 | } | ||||||||||||||
| 41 | } | ||||||||||||||
| 42 | 1 | }; | |||||||||||||
| 43 | |||||||||||||||
| 44 | 0 | ▶ | sub get_work { | ||||||||||||
|
Subroutine
get_work: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||
| 45 | 0 | my($self) = @_; | |||||||||||||
| 46 | 0 | for my $client ( shuffle @{ $self->beanstalk_clients }){ | |||||||||||||
| 47 | 0 | ▶ | if( my $job = $client->reserve($self->has_reserve_timeout ? $self->reserve_timeout : ())){ | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 48 | 0 | ▶ | if( $self->has_max_tries ){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 49 | 0 | my $stats = $job->stats; | |||||||||||||
| 50 | 0 | ▶ | if( $stats->reserves > $self->max_tries ){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | 0 | my $job_id = $job->id; | |||||||||||||
| 52 | 0 | my $tube = $stats->tube; | |||||||||||||
| 53 | 0 | my $args = join( ', ', map { "'$_'" } $job->args); | |||||||||||||
| 54 | 0 | ▶ | if( $self->delete_on_max_tries ){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 0 | $self->logger->notice("Job exceeds max-tries. Deleting job ${job_id} from tube '$tube' with args: $args"); | |||||||||||||
| 56 | 0 | $job->delete; | |||||||||||||
| 57 | } else { | ||||||||||||||
| 58 | 0 | $self->logger->notice("Job exceeds max-tries. Burying job ${job_id} from tube '$tube' with args: $args"); | |||||||||||||
| 59 | 0 | $job->bury; | |||||||||||||
| 60 | } | ||||||||||||||
| 61 | 0 | redo; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | } | ||||||||||||||
| 64 | 0 | return $job; | |||||||||||||
| 65 | } else { | ||||||||||||||
| 66 | 0 | my $error = $client->error; | |||||||||||||
| 67 | 0 | ▶ | if( $error ne 'TIMED_OUT'){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | 0 | $self->logger->error( $error ); | |||||||||||||
| 69 | } | ||||||||||||||
| 70 | } | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | 0 | return; | |||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 1 | 1; | |||||||||||||
| 76 | |||||||||||||||
| 77 | __END__; | ||||||||||||||