blib/lib/Sque/Worker.pm

filter: Statementstmt 88.6% 47 / 53 Branchbran 30.0% 3 / 10 Conditioncond 33.3% 1 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 86.6% 13 / 15 Podpod 100% 6 / 6 totaltotal 78.6% 70 / 89 CC 9 SCAR 12.4
CC
9
Cov
77%
work
12.0
add_queues
4.1
log
3.7
CRAP 3.5
« blib/lib/Sque.pm ↑ Summary blib/lib/App/Sque.pm »
1 package Sque::Worker;
2 6 $Sque::Worker::VERSION = '0.010';
3 6 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use Any::Moose '::Util::TypeConstraints';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 6 with 'Sque::Encoder';
8  
9 # ABSTRACT: Does the hard work of babysitting Sque::Job's
10  
11 6 has _dying => (is => 'rw', default => 0);
12  
13 6 has logger => (is => 'rw');
14  
15 6 has sque => (
16 is => 'ro',
17 required => 1,
18 handles => [qw/ stomp key /]
19 );
20  
21 has queues => (
22 is => 'rw',
23 isa => 'HashRef',
24 lazy => 1,
25 1 ▶ default => sub {{}}
Subroutine __ANON__: called CC=1 SCAR=0.0
26 6 );
27  
28 5 ▶has verbose => ( is => 'rw', default => sub {0} );
Subroutine __ANON__: called CC=1 SCAR=0.0
29  
30 5 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod documented
31 5 my ($self) = @_;
32  
33 1 ▶ my $die_handler = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
34 1 my ($signal) = @_;
35 1 $self->log("Received $signal signal, dying...");
36 1 $self->_dying(1);
37 5 };
38  
39 # Setup die handlers
40 5 $SIG{$_} = $die_handler for qw(INT TERM KILL QUIT)
41 }
42  
43 0 ▶sub work {
Subroutine work: not called CC=3 SCAR=24.8
Pod documented
44 0 my ( $self ) = @_;
45 0 ▶ while( my $job = $self->sque->pop ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $job = $self->sque->pop) 0 0 0
46 0 $job->worker($self);
47 0 my $reval = $self->perform($job);
48 0 ▶ exit 0 if $self->_dying;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_dying 0 0 0
49 }
50 }
51  
52 3 ▶sub perform {
Subroutine perform: called CC=1 SCAR=0.0
Pod documented
53 3 my ( $self, $job ) = @_;
54 3 my $ret;
55 3 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 3 $ret = $job->perform;
57 3 $self->log( sprintf( "done: %s", $job->stringify ) );
58 0 ▶ } catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
59 0 $self->log( sprintf( "%s failed: %s", $job->stringify, $_ ) );
60 3 };
61 3 $self->stomp->ack({ frame => $job->frame });
62 3 return $ret;
63 }
64  
65 2 ▶sub reserve {
Subroutine reserve: called CC=1 SCAR=0.0
Pod documented
66 2 my ( $self ) = @_;
67 2 return $self->sque->pop;
68 }
69  
70 2 ▶sub add_queues {
Subroutine add_queues: called CC=4 SCAR=14.0
Pod documented
71 2 my $self = shift;
72 2 ▶ return unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 2 2
73 2 for my $q ( @_ ) {
74 3 ▶ if(!$self->queues->{$q}){
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->queues->{$q}) 3 0 3
75 3 $self->queues->{$q} = 1;
76 3 my $queue = $self->sque->key( $q );
77 3 $self->_subscribe_queue( $queue );
78 }
79 }
80 2 return $self;
81 }
82  
83 4 ▶sub log {
Subroutine log: called CC=3 SCAR=13.1
Pod documented
84 4 my $self = shift;
85 4 ▶ $self->logger->DEBUG(@_) if $self->verbose and $self->logger;
Branch: 1 branch
BranchTrueFalseTotal
if $self->verbose and $self->logger 0 4 4
Condition33%1/3
$self->verbose and $self->logger
!ll&&!rl&&r
400
MC/DC0%0/2
$self->verbose and $self->logger
$self->verbose $self->logger
Truth table33%1/3
A and B
A= $self->verboseB= $self->logger
ABresult
0X0
100
111
86 }
87  
88 3 ▶sub _subscribe_queue {
Subroutine _subscribe_queue: called CC=1 SCAR=0.0
89 3 my ( $self, $q ) = @_;
90 3 $self->stomp->subscribe( {
91 destination => $q,
92 ack => 'client',
93 } );
94 };
95  
96 6 __PACKAGE__->meta->make_immutable();
97  
98 6 1;
99  
100 __END__
« blib/lib/Sque.pm ↑ Summary blib/lib/App/Sque.pm »