blib/lib/Sque.pm

filter: Statementstmt 92.1% 59 / 64 Branchbran 43.7% 7 / 16 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 93.7% 15 / 16 Podpod 100% 4 / 4 totaltotal 77.1% 88 / 114 CC 13 SCAR 15.9
CC
13
Cov
78%
new_job
10.4
push
9.8
__ANON__
3.0
CRAP 4.9
« blib/lib/App/Sque/Command/work.pm ↑ Summary blib/lib/Sque/Worker.pm »
1 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Sque;
4 6 $Sque::VERSION = '0.010';
5 6 ▶use Any::Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Any::Moose '::Util::TypeConstraints';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use Net::Stomp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 6 ▶use Sque::Job;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 6 ▶use Sque::Worker;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # ABSTRACT: Background job processing based on Resque, using Stomp
13  
14 6 subtype 'Sugar::Stomp' => as class_type('Net::Stomp');
15  
16 coerce 'Sugar::Stomp'
17 => from 'Str'
18 1 ▶ => via {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 my ( $host, $port ) = split /:/;
20 1 my $stomp = Net::Stomp->new({ hostname => $host, port => $port });
21 1 $stomp->connect;
22 1 return $stomp;
23 6 };
24  
25 coerce 'Sugar::Stomp'
26 => from 'ArrayRef[Str]'
27 2 ▶ => via {
Subroutine __ANON__: called CC=3 SCAR=11.0
28 2 my ($a) = @_;
29 2 my $hosts = [];
30 2 for ( @$a ) {
31 3 my ( $host, $port ) = split /:/;
32 3 push @$hosts, { hostname => $host, port => $port };
33 }
34  
35 my $stomp = @$hosts > 1
36 ? Net::Stomp->new({ hosts => $hosts })
37 : Net::Stomp->new({
38 hostname => $hosts->[0]{hostname},
39 port => $hosts->[0]{port}
40 2 ▶ });
Branch: 1 branch
BranchTrueFalseTotal
@$hosts > 1 ? : 1 1 2
41  
42 2 $stomp->connect;
43 2 return $stomp;
44 6 };
45  
46 has stomp => (
47 is => 'ro',
48 lazy => 1,
49 coerce => 1,
50 isa => 'Sugar::Stomp',
51 0 ▶ default => sub { Net::Stomp->new->connect },
Subroutine __ANON__: not called CC=1 SCAR=6.9
52 6 );
53  
54 6 ▶has namespace => ( is => 'rw', default => sub { 'sque' });
Subroutine __ANON__: called CC=1 SCAR=0.0
55  
56 has worker => (
57 is => 'ro',
58 lazy => 1,
59 5 ▶ default => sub { Sque::Worker->new( sque => $_[0] ) },
Subroutine __ANON__: called CC=1 SCAR=0.0
60 6 );
61  
62 2 ▶sub push {
Subroutine push: called CC=6 SCAR=22.9
Pod documented
63 2 my ( $self, $queue, $job ) = @_;
64 2 ▶ confess "Can't push an empty job." unless ( $job || ref $queue );
Branch: 1 branch
BranchTrueFalseTotal
unless $job or ref $queue 0 2 2
Condition33%1/3
$job or ref $queue
l!l&&r!l&&!r
200
MC/DC0%0/2
$job or ref $queue
$job ref $queue
Truth table33%1/3
A or B
A= $jobB= ref $queue
ABresult
1X1
011
000
65 2 ▶ if( ref $queue ){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $queue) { } 0 2 2
66 0 ▶ $job = $self->new_job($queue) unless ref $queue eq 'Sque::Job';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $queue eq "Sque::Job" 0 0 0
67 0 $queue = $job->queue;
68 } else {
69 2 ▶ $job = $self->new_job($job) unless ref $job eq 'Sque::Job';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $job eq "Sque::Job" 2 0 2
70 }
71  
72 $self->stomp->send( {
73 persistent => 'true',
74 destination => $self->key( $queue ),
75 body => $job->encode,
76 2 %{ $job->headers },
77 } );
78 }
79  
80 2 ▶sub pop {
Subroutine pop: called CC=2 SCAR=7.0
Pod documented
81 2 my ( $self ) = @_;
82 2 my $frame = $self->stomp->receive_frame;
83 2 ▶ return unless $frame;
Branch: 1 branch
BranchTrueFalseTotal
unless $frame 0 2 2
84  
85 2 $self->new_job({
86 frame => $frame,
87 queue => $frame->destination,
88 });
89 }
90  
91 4 ▶sub new_job {
Subroutine new_job: called CC=5 SCAR=23.4
Pod documented
92 4 my ( $self, $job ) = @_;
93  
94 4 ▶ if ( $job && ref $job && ref $job eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($job and ref $job and ref $job eq 'HASH') { } 4 0 4
elsif ($job) { } 0 0 0
Condition33%2/6
$job and ref $job and ref $job eq "HASH"
!ll&&!rl&&r
004
$job and ref $job
!ll&&!rl&&r
004
MC/DC0%0/3
$job and ref $job and ref $job eq "HASH"
$job ref $job ref $job eq "HASH"
Truth table25%1/4
A and B and C
A= $jobB= ref $jobC= ref $job eq "HASH"
ABCresult
0XX0
10X0
1100
1111
95 4 return Sque::Job->new({ sque => $self, %$job });
96 }
97 elsif ( $job ) {
98 0 return Sque::Job->new({ sque => $self, payload => $job });
99 }
100 0 confess "Can't build an empty Sque::Job object.";
101 }
102  
103 7 ▶sub key {
Subroutine key: called CC=1 SCAR=0.0
Pod documented
104 7 my $self = shift;
105 7 '/queue/' . $self->namespace . '/' . shift;
106 }
107  
108 6 __PACKAGE__->meta->make_immutable();
109  
110 6 1;
111  
112 __END__
« blib/lib/App/Sque/Command/work.pm ↑ Summary blib/lib/Sque/Worker.pm »