Worker/Role/Worker.pm

filter: Statementstmt 84.4% 65 / 77 Branchbran 100% 4 / 4 Conditioncond 66.6% 2 / 3 MC/DCmcdc 50.0% 1 / 2 Subroutinesub 66.6% 16 / 24 Podpod 100% 5 / 5 totaltotal 80.8% 93 / 115 CC 4 SCAR 5.3
CC
4
Cov
85%
_poll
4.0
__ANON__
2.0
__ANON__
2.0
CRAP 1.7
« Worker/Manager/Prefork.pm ↑ Summary Worker/Types.pm »
1 package Cantella::Worker::Role::Worker;
2  
3 2 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use POE;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use MooseX::Types::Log::Dispatch qw(Logger);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Data::GUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = '0.001001';
9 2 $VERSION = eval $VERSION;
10  
11 2 requires qw/get_work work/;
12  
13 has logger => (
14 is => 'ro',
15 isa => Logger,
16 coerce => 1,
17 required => 1,
18 0 ▶ default => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
19 0 Log::Dispatch->new(outputs => [ ['Screen', min_level => 'debug' ] ]);
20 }
21 2 );
22  
23 has alias => (
24 is => 'ro',
25 isa => 'Str',
26 required => 1,
27 1 ▶ default => sub{ Data::GUID->new->as_string }
Subroutine __ANON__: called CC=1 SCAR=0.0
28 2 );
29  
30 has interval => (
31 is => 'rw',
32 isa => 'Num',
33 required => 1,
34 0 ▶ default => sub { 2 },
Subroutine __ANON__: not called CC=1 SCAR=6.9
35 2 );
36  
37 0 ▶sub BUILD {}
Subroutine BUILD: not called CC=1 SCAR=0.0
Pod documented
38  
39 1 ▶after BUILD => sub {
Subroutine __ANON__: called CC=1 SCAR=0.2
40 1 my ($self, $args) = @_;
41 POE::Session->create(
42 object_states => [
43 $self, {
44 poll => '_poll',
45 work => '_work',
46 _start => '_start',
47 _pause => '_pause',
48 _resume => '_resume',
49 shutdown => '_shutdown',
50 } ],
51 inline_states => {
52 0 ▶ _keep_alive => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
53 0 $_[KERNEL]->delay(_keep_alive => 1000);
54 },
55 1 ▶ sig_term => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
56 1 my ($kernel) = $_[KERNEL];
57 1 $kernel->yield('shutdown');
58 1 $kernel->sig_handled;
59 },
60 0 ▶ sig_int => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
61 0 my ($kernel) = $_[KERNEL];
62 0 $kernel->yield('shutdown');
63 0 $kernel->sig_handled;
64 },
65 1 ▶ sig_usr1 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
66 1 my ($kernel) = $_[KERNEL];
67 1 $kernel->yield('_pause');
68 1 $kernel->sig_handled;
69 },
70 1 ▶ sig_usr2 => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
71 1 my ($kernel) = $_[KERNEL];
72 1 $kernel->yield('_resume');
73 1 $kernel->sig_handled;
74 },
75 }
76 1 );
77 2 };
78  
79 1 ▶sub start {
Subroutine start: called CC=1 SCAR=0.0
Pod documented
80 1 $poe_kernel->run;
81 }
82  
83 0 ▶sub pause {
Subroutine pause: not called CC=1 SCAR=6.9
Pod documented
84 0 my ($self, $until) = @_;
85 0 $poe_kernel->call($self->alias, '_pause');
86 }
87  
88 0 ▶sub resume {
Subroutine resume: not called CC=1 SCAR=6.9
Pod documented
89 0 my ($self, $when) = @_;
90 0 $poe_kernel->call($self->alias, '_resume');
91 }
92  
93 0 ▶sub shutdown {
Subroutine shutdown: not called CC=1 SCAR=6.9
Pod documented
94 0 my ($self) = @_;
95 #$self->pause;
96 0 $poe_kernel->post($self->alias, 'shutdown');
97 }
98  
99 #--------#---------#---------#---------#---------#---------#---------#--------#
100  
101 11 ▶sub _poll {
Subroutine _poll: called CC=4 SCAR=13.9
102 11 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
103 11 ▶ return unless exists($heap->{poll}) && $heap->{poll};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $heap->{"poll"} and $heap->{"poll"} 3 8 11
Condition66%2/3
exists $heap->{"poll"} and $heap->{"poll"}
!ll&&!rl&&r
038
MC/DC50%1/2
exists $heap->{"poll"} and $heap->{"poll"}
exists $heap->{"poll"} $heap->{"poll"}
Truth table66%2/3
A and B
A= exists $heap->{"poll"}B= $heap->{"poll"}
ABresult
0X0
100
111
104  
105 8 ▶ if ( defined(my $work = $self->get_work()) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $work = $self->get_work)) 4 4 8
106 4 $kernel->yield(work => $work);
107 4 $kernel->yield('poll');
108 4 return;
109 }
110  
111 4 $kernel->delay(poll => $self->interval);
112 }
113  
114 4 ▶sub _work {
Subroutine _work: called CC=1 SCAR=0.0
115 4 my ($self, $kernel, $work) = @_[OBJECT, KERNEL, ARG0];
116 4 $self->work($work);
117 }
118  
119 #--------#---------#---------#---------#---------#---------#---------#--------#
120  
121 1 ▶sub _start {
Subroutine _start: called CC=1 SCAR=0.0
122 1 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
123 1 $kernel->sig(INT => 'sig_int');
124 1 $kernel->sig(TERM => 'sig_term');
125 1 $kernel->sig(USR1 => 'sig_usr1');
126 1 $kernel->sig(USR2 => 'sig_usr2');
127  
128 # set up polling
129 # set alias for ourselves and remember it
130 1 $heap->{poll} = 1;
131 1 $kernel->alias_set($self->alias);
132 1 $kernel->yield('poll');
133 }
134  
135 1 ▶sub _pause {
Subroutine _pause: called CC=1 SCAR=0.0
136 1 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
137 1 $self->logger->info("pausing worker ${$}");
138 1 $heap->{poll} = 0;
139 1 $kernel->delay('poll'); #clear any alarm, if it exists
140 1 $kernel->delay(_keep_alive => 1000); #keep the session alive
141 }
142  
143 1 ▶sub _resume {
Subroutine _resume: called CC=1 SCAR=0.0
144 1 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
145 1 $self->logger->info("resuming worker ${$}");
146 1 $heap->{poll} = 1;
147 1 $kernel->delay('poll'); #clear any alarm, if it exists
148 1 $kernel->yield('poll');
149 }
150  
151 1 ▶sub _shutdown {
Subroutine _shutdown: called CC=1 SCAR=0.0
152 1 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
153 1 $self->logger->info("shutting down worker with pid ${$}");
154  
155 1 $kernel->alarm_remove_all();
156 #cleaup heap, alias, alarms (no lingering refs n ish)
157 1 %$heap = ();
158 1 $kernel->alias_remove($self->alias);
159 }
160  
161 #--------#---------#---------#---------#---------#---------#---------#--------#
162  
163 2 1;
164  
165 __END__;
« Worker/Manager/Prefork.pm ↑ Summary Worker/Types.pm »