| 1 | # $Id: JobQueue.pm 29 2009-07-28 06:33:37Z rcaputo $ | |||||||||||||||||||||||||||||||||
| 2 | # License and documentation are after __END__. | |||||||||||||||||||||||||||||||||
| 3 | ||||||||||||||||||||||||||||||||||
| 4 | package POE::Component::JobQueue; | |||||||||||||||||||||||||||||||||
| 5 | ||||||||||||||||||||||||||||||||||
| 6 | 1 | ▶ | use strict; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 7 | ||||||||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use vars qw($VERSION); | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 9 | 1 | $VERSION = '0.571'; | ||||||||||||||||||||||||||||||||
| 10 | ||||||||||||||||||||||||||||||||||
| 11 | 1 | ▶ | use Carp qw (croak); | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 12 | ||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use POE::Session; | |||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 14 | ||||||||||||||||||||||||||||||||||
| 15 | sub DEBUG () { 0 }; | |||||||||||||||||||||||||||||||||
| 16 | ||||||||||||||||||||||||||||||||||
| 17 | # Spawn a new PoCo::JobQueue session. This basically is a | |||||||||||||||||||||||||||||||||
| 18 | # constructor, but it isn't named "new" because it doesn't create a | |||||||||||||||||||||||||||||||||
| 19 | # usable object. Instead, it spawns the object off as a session. | |||||||||||||||||||||||||||||||||
| 20 | ||||||||||||||||||||||||||||||||||
| 21 | 2 | ▶ | sub spawn { | |||||||||||||||||||||||||||||||
|
Subroutine
spawn: called CC=23 SCAR=37.5
Pod
undocumented
| ||||||||||||||||||||||||||||||||||
| 22 | 2 | my $type = shift; | ||||||||||||||||||||||||||||||||
| 23 | ||||||||||||||||||||||||||||||||||
| 24 | 2 | ▶ | croak "$type requires an even number of parameters" if @_ % 2; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 25 | ||||||||||||||||||||||||||||||||||
| 26 | 2 | my %params = @_; | ||||||||||||||||||||||||||||||||
| 27 | ||||||||||||||||||||||||||||||||||
| 28 | ### Parameters that are common to both types of job queue. | |||||||||||||||||||||||||||||||||
| 29 | ||||||||||||||||||||||||||||||||||
| 30 | 2 | my $alias = delete $params{Alias}; | ||||||||||||||||||||||||||||||||
| 31 | 2 | ▶ | $alias = 'queuer' unless defined $alias and length $alias; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $alias and length $alias
MC/DC0%0/2
defined $alias and length $alias
defined $alias
length $alias
Truth table33%1/3
A and B
A= defined $aliasB= length $alias
| ||||||||||||||||||||||||||||||||||
| 32 | ||||||||||||||||||||||||||||||||||
| 33 | 2 | my $worker = delete $params{Worker}; | ||||||||||||||||||||||||||||||||
| 34 | 2 | ▶ | croak "$type requires a coderef Worker parameter" | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $worker and ref $worker eq "CODE"
MC/DC0%0/2
defined $worker and ref $worker eq "CODE"
defined $worker
ref $worker eq "CODE"
Truth table33%1/3
A and B
A= defined $workerB= ref $worker eq "CODE"
| ||||||||||||||||||||||||||||||||||
| 35 | unless defined $worker and ref($worker) eq 'CODE'; | |||||||||||||||||||||||||||||||||
| 36 | ||||||||||||||||||||||||||||||||||
| 37 | 2 | my $worker_limit = delete $params{WorkerLimit}; | ||||||||||||||||||||||||||||||||
| 38 | 2 | ▶ | $worker_limit = 8 unless defined $worker_limit and $worker_limit > 0; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $worker_limit and $worker_limit > 0
MC/DC0%0/2
defined $worker_limit and $worker_limit > 0
defined $worker_limit
$worker_limit > 0
Truth table33%1/3
A and B
A= defined $worker_limitB= $worker_limit > 0
| ||||||||||||||||||||||||||||||||||
| 39 | ||||||||||||||||||||||||||||||||||
| 40 | croak "$type requires either an Active or a Passive parameter block" | |||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | unless defined($params{Active}) xor defined($params{Passive}); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%2/4
defined $params{"Active"} xor defined $params{"Passive"}
MC/DC0%0/2
defined $params{"Active"} xor defined $params{"Passive"}
defined $params{"Active"}
defined $params{"Passive"}
Truth table50%2/4
A xor B
A= defined $params{"Active"}B= defined $params{"Passive"}
| ||||||||||||||||||||||||||||||||||
| 42 | ||||||||||||||||||||||||||||||||||
| 43 | ### Parameters and states that are common to both types of queue. | |||||||||||||||||||||||||||||||||
| 44 | ||||||||||||||||||||||||||||||||||
| 45 | 2 | my @args = ( $alias, $worker_limit, $worker ); | ||||||||||||||||||||||||||||||||
| 46 | my %states = | |||||||||||||||||||||||||||||||||
| 47 | ( _child => \&poco_jobqueue_both_child, | |||||||||||||||||||||||||||||||||
| 48 | stop => \&poco_jobqueue_both_stop, | |||||||||||||||||||||||||||||||||
| 49 | 2 | ▶ | _stop => sub {}, | |||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| ||||||||||||||||||||||||||||||||||
| 50 | 2 | ); | ||||||||||||||||||||||||||||||||
| 51 | ||||||||||||||||||||||||||||||||||
| 52 | ### Modal parameters and states go here. | |||||||||||||||||||||||||||||||||
| 53 | ||||||||||||||||||||||||||||||||||
| 54 | # Set up for an active queue. | |||||||||||||||||||||||||||||||||
| 55 | 2 | ▶ | if (exists $params{Active}) { | |||||||||||||||||||||||||||||||
|
Branch: 2 branches
| ||||||||||||||||||||||||||||||||||
| 56 | 1 | my $active = delete $params{Active}; | ||||||||||||||||||||||||||||||||
| 57 | ||||||||||||||||||||||||||||||||||
| 58 | 1 | my $poll_interval = delete $active->{PollInterval}; | ||||||||||||||||||||||||||||||||
| 59 | 1 | ▶ | $poll_interval = undef | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $poll_interval and $poll_interval > 0
MC/DC0%0/2
defined $poll_interval and $poll_interval > 0
defined $poll_interval
$poll_interval > 0
Truth table33%1/3
A and B
A= defined $poll_intervalB= $poll_interval > 0
| ||||||||||||||||||||||||||||||||||
| 60 | unless defined $poll_interval and $poll_interval > 0; | |||||||||||||||||||||||||||||||||
| 61 | ||||||||||||||||||||||||||||||||||
| 62 | 1 | my $ack_alias = delete $active->{AckAlias}; | ||||||||||||||||||||||||||||||||
| 63 | 1 | ▶ | $ack_alias = undef unless defined $ack_alias and length $ack_alias; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ack_alias and length $ack_alias
MC/DC0%0/2
defined $ack_alias and length $ack_alias
defined $ack_alias
length $ack_alias
Truth table33%1/3
A and B
A= defined $ack_aliasB= length $ack_alias
| ||||||||||||||||||||||||||||||||||
| 64 | ||||||||||||||||||||||||||||||||||
| 65 | 1 | my $ack_state = delete $active->{AckState}; | ||||||||||||||||||||||||||||||||
| 66 | 1 | ▶ | $ack_state = undef unless defined $ack_state and length $ack_state; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $ack_state and length $ack_state
MC/DC0%0/2
defined $ack_state and length $ack_state
defined $ack_state
length $ack_state
Truth table33%1/3
A and B
A= defined $ack_stateB= length $ack_state
| ||||||||||||||||||||||||||||||||||
| 67 | ||||||||||||||||||||||||||||||||||
| 68 | 1 | ▶ | croak "$type must have neither or both AckAlias and AckState" | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition25%1/4
defined $ack_alias xor defined $ack_state
MC/DC0%0/2
defined $ack_alias xor defined $ack_state
defined $ack_alias
defined $ack_state
Truth table25%1/4
A xor B
A= defined $ack_aliasB= defined $ack_state
| ||||||||||||||||||||||||||||||||||
| 69 | if defined($ack_alias) xor defined($ack_state); | |||||||||||||||||||||||||||||||||
| 70 | ||||||||||||||||||||||||||||||||||
| 71 | 1 | $states{_start} = \&poco_jobqueue_active_start; | ||||||||||||||||||||||||||||||||
| 72 | 1 | $states{dequeue} = \&poco_jobqueue_active_dequeue; | ||||||||||||||||||||||||||||||||
| 73 | ||||||||||||||||||||||||||||||||||
| 74 | 1 | push @args, $poll_interval, $ack_alias, $ack_state; | ||||||||||||||||||||||||||||||||
| 75 | } | |||||||||||||||||||||||||||||||||
| 76 | ||||||||||||||||||||||||||||||||||
| 77 | # Set up for a passive queue. | |||||||||||||||||||||||||||||||||
| 78 | elsif (exists $params{Passive}) { | |||||||||||||||||||||||||||||||||
| 79 | 1 | my $passive = delete $params{Passive}; | ||||||||||||||||||||||||||||||||
| 80 | ||||||||||||||||||||||||||||||||||
| 81 | 1 | my $prioritizer = delete $passive->{Prioritizer}; | ||||||||||||||||||||||||||||||||
| 82 | 1 | ▶ | $prioritizer = sub { 1 } unless defined $prioritizer; | |||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 83 | ||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | croak( "$type doesn't know these Passive parameters: ", | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 85 | join(', ', sort keys %$passive) | |||||||||||||||||||||||||||||||||
| 86 | ) if scalar keys %$passive; | |||||||||||||||||||||||||||||||||
| 87 | ||||||||||||||||||||||||||||||||||
| 88 | 1 | $states{_start} = \&poco_jobqueue_passive_start; | ||||||||||||||||||||||||||||||||
| 89 | 1 | $states{dequeue} = \&poco_jobqueue_passive_dequeue; | ||||||||||||||||||||||||||||||||
| 90 | 1 | $states{enqueue} = \&poco_jobqueue_passive_enqueue; | ||||||||||||||||||||||||||||||||
| 91 | ||||||||||||||||||||||||||||||||||
| 92 | 1 | push @args, $prioritizer; | ||||||||||||||||||||||||||||||||
| 93 | } | |||||||||||||||||||||||||||||||||
| 94 | ||||||||||||||||||||||||||||||||||
| 95 | 2 | ▶ | croak( "$type doesn't know these parameters: ", | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 96 | join(', ', sort keys %params) | |||||||||||||||||||||||||||||||||
| 97 | ) if scalar keys %params; | |||||||||||||||||||||||||||||||||
| 98 | ||||||||||||||||||||||||||||||||||
| 99 | # Spawn whichever queue we've built. | |||||||||||||||||||||||||||||||||
| 100 | 2 | POE::Session->create | ||||||||||||||||||||||||||||||||
| 101 | ( inline_states => \%states, | |||||||||||||||||||||||||||||||||
| 102 | args => \@args, | |||||||||||||||||||||||||||||||||
| 103 | ); | |||||||||||||||||||||||||||||||||
| 104 | ||||||||||||||||||||||||||||||||||
| 105 | 2 | undef; | ||||||||||||||||||||||||||||||||
| 106 | } | |||||||||||||||||||||||||||||||||
| 107 | ||||||||||||||||||||||||||||||||||
| 108 | # Helper function for active job queues. | |||||||||||||||||||||||||||||||||
| 109 | ||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | sub poco_jobqueue_active_meta_postback { | |||||||||||||||||||||||||||||||
|
Subroutine
poco_jobqueue_active_meta_postback: not called CC=1 SCAR=6.9
Pod
undocumented
| ||||||||||||||||||||||||||||||||||
| 111 | 0 | die "unimplemented bit"; | ||||||||||||||||||||||||||||||||
| 112 | } | |||||||||||||||||||||||||||||||||
| 113 | ||||||||||||||||||||||||||||||||||
| 114 | # Start an active job queue. This type of queue polls for new jobs. | |||||||||||||||||||||||||||||||||
| 115 | ||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | sub poco_jobqueue_active_start { | |||||||||||||||||||||||||||||||
|
Subroutine
poco_jobqueue_active_start: called CC=1 SCAR=0.0
Pod
undocumented
| ||||||||||||||||||||||||||||||||||
| 117 | 1 | my ( $kernel, $heap, | ||||||||||||||||||||||||||||||||
| 118 | $alias, $worker_limit, $worker_ref, | |||||||||||||||||||||||||||||||||
| 119 | $poll_interval, $ack_alias, $ack_state | |||||||||||||||||||||||||||||||||
| 120 | ) = @_[KERNEL, HEAP, ARG0..ARG5]; | |||||||||||||||||||||||||||||||||
| 121 | ||||||||||||||||||||||||||||||||||
| 122 | # Common parameters. | |||||||||||||||||||||||||||||||||
| 123 | 1 | $heap->{alias} = $alias; | ||||||||||||||||||||||||||||||||
| 124 | 1 | $heap->{worker_limit} = $worker_limit; | ||||||||||||||||||||||||||||||||
| 125 | 1 | $heap->{worker_ref} = $worker_ref; | ||||||||||||||||||||||||||||||||
| 126 | ||||||||||||||||||||||||||||||||||
| 127 | # Active queue parameters. | |||||||||||||||||||||||||||||||||
| 128 | 1 | $heap->{poll_interval} = $poll_interval; | ||||||||||||||||||||||||||||||||
| 129 | $heap->{meta_postback} = | |||||||||||||||||||||||||||||||||
| 130 | 10 | ▶ | sub { | |||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=8.4
| ||||||||||||||||||||||||||||||||||
| 131 | 10 | my @job = @_; | ||||||||||||||||||||||||||||||||
| 132 | 10 | my $session = $kernel->alias_resolve( $ack_alias ); | ||||||||||||||||||||||||||||||||
| 133 | 10 | ▶ | return $session->postback( $ack_state, @job ) if defined $session; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | return sub { 1 }; | |||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| ||||||||||||||||||||||||||||||||||
| 135 | 1 | }; | ||||||||||||||||||||||||||||||||
| 136 | ||||||||||||||||||||||||||||||||||
| 137 | # State variables. Pending polls starts at 1 because we're going to | |||||||||||||||||||||||||||||||||
| 138 | # fake an initial poll to get things started. | |||||||||||||||||||||||||||||||||
| 139 | 1 | $heap->{worker_count} = 0; | ||||||||||||||||||||||||||||||||
| 140 | 1 | $heap->{pending_polls} = 0; | ||||||||||||||||||||||||||||||||
| 141 | 1 | $heap->{latest_worker} = 0; | ||||||||||||||||||||||||||||||||
| 142 | ||||||||||||||||||||||||||||||||||
| 143 | # Register an alias. | |||||||||||||||||||||||||||||||||
| 144 | 1 | $kernel->alias_set($alias); | ||||||||||||||||||||||||||||||||
| 145 | ||||||||||||||||||||||||||||||||||
| 146 | # Start an initial set of workers. | |||||||||||||||||||||||||||||||||
| 147 | 1 | $kernel->yield( 'dequeue' ); | ||||||||||||||||||||||||||||||||
| 148 | } | |||||||||||||||||||||||||||||||||
| 149 | ||||||||||||||||||||||||||||||||||
| 150 | # Start a passive job queue. This type of queue waits for something | |||||||||||||||||||||||||||||||||
| 151 | # else to enqueue jobs. | |||||||||||||||||||||||||||||||||
| 152 | ||||||||||||||||||||||||||||||||||
| 153 | 1 | ▶ | sub poco_jobqueue_passive_start { | |||||||||||||||||||||||||||||||
|
Subroutine
poco_jobqueue_passive_start: called CC=1 SCAR=0.0
Pod
undocumented
| ||||||||||||||||||||||||||||||||||
| 154 | 1 | my ( $kernel, $heap, | ||||||||||||||||||||||||||||||||
| 155 | $alias, $worker_limit, $worker_ref, | |||||||||||||||||||||||||||||||||
| 156 | $prioritizer | |||||||||||||||||||||||||||||||||
| 157 | ) = @_[KERNEL, HEAP, ARG0..ARG3]; | |||||||||||||||||||||||||||||||||
| 158 | ||||||||||||||||||||||||||||||||||
| 159 | # Common parameters. | |||||||||||||||||||||||||||||||||
| 160 | 1 | $heap->{alias} = $alias; | ||||||||||||||||||||||||||||||||
| 161 | 1 | $heap->{worker_limit} = $worker_limit; | ||||||||||||||||||||||||||||||||
| 162 | 1 | $heap->{worker_ref} = $worker_ref; | ||||||||||||||||||||||||||||||||
| 163 | ||||||||||||||||||||||||||||||||||
| 164 | # Active queue parameters. | |||||||||||||||||||||||||||||||||
| 165 | 1 | $heap->{prioritizer} = $prioritizer; | ||||||||||||||||||||||||||||||||
| 166 | ||||||||||||||||||||||||||||||||||
| 167 | # State variables. | |||||||||||||||||||||||||||||||||
| 168 | 1 | $heap->{worker_count} = 0; | ||||||||||||||||||||||||||||||||
| 169 | 1 | $heap->{job_queue} = [ ]; | ||||||||||||||||||||||||||||||||
| 170 | ||||||||||||||||||||||||||||||||||
| 171 | # Register an alias. | |||||||||||||||||||||||||||||||||
| 172 | 1 | $kernel->alias_set($alias); | ||||||||||||||||||||||||||||||||
| 173 | } | |||||||||||||||||||||||||||||||||
| 174 | ||||||||||||||||||||||||||||||||||
| 175 | # A worker either has come or gone. Track the number of running | |||||||||||||||||||||||||||||||||
| 176 | # workers, and spawn new ones if appropriate. | |||||||||||||||||||||||||||||||||
| 177 | ||||||||||||||||||||||||||||||||||
| 178 | 40 | ▶ | sub poco_jobqueue_both_child { | |||||||||||||||||||||||||||||||
|
Subroutine
poco_jobqueue_both_child: called CC=6 SCAR=18.1
Pod
undocumented
| ||||||||||||||||||||||||||||||||||
| 179 | 40 | my ($kernel, $heap, $operation) = @_[KERNEL, HEAP, ARG0]; | ||||||||||||||||||||||||||||||||
| 180 | ||||||||||||||||||||||||||||||||||
| 181 | # A worker has begun its job. Count it so we know how many exist. | |||||||||||||||||||||||||||||||||
| 182 | ||||||||||||||||||||||||||||||||||
| 183 | 40 | ▶ | if ($operation eq 'gain' or $operation eq 'create') { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$operation eq "gain" or $operation eq "create"
MC/DC50%1/2
$operation eq "gain" or $operation eq "create"
$operation eq "gain"
$operation eq "create"
Truth table66%2/3
A or B
A= $operation eq "gain"B= $operation eq "create"
| ||||||||||||||||||||||||||||||||||
| 184 | 20 | DEBUG and warn "JQ: job queue $heap->{alias} got a new worker"; | ||||||||||||||||||||||||||||||||
| 185 | 20 | $heap->{worker_count}++; | ||||||||||||||||||||||||||||||||
| 186 | } | |||||||||||||||||||||||||||||||||
| 187 | ||||||||||||||||||||||||||||||||||
| 188 | # A worker has finished. Decrement our worker count, and try to | |||||||||||||||||||||||||||||||||
| 189 | # start another worker to take its place. | |||||||||||||||||||||||||||||||||
| 190 | ||||||||||||||||||||||||||||||||||
| 191 | else { | |||||||||||||||||||||||||||||||||
| 192 | 20 | DEBUG and warn "JQ: job queue $heap->{alias} lost a worker"; | ||||||||||||||||||||||||||||||||
| 193 | warn( "worker count ($heap->{worker_count}) exceeded the limit (", | |||||||||||||||||||||||||||||||||
| 194 | $heap->{worker_limit}, ")" | |||||||||||||||||||||||||||||||||
| 195 | 20 | ▶ | ) if $heap->{worker_count} > $heap->{worker_limit}; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 196 | 20 | $heap->{worker_count}--; | ||||||||||||||||||||||||||||||||
| 197 | $kernel->yield('dequeue') unless ( | |||||||||||||||||||||||||||||||||
| 198 | $heap->{latest_worker} or $heap->{stopped} | |||||||||||||||||||||||||||||||||
| 199 | 20 | ▶ | ); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$heap->{"latest_worker"} or $heap->{"stopped"}
MC/DC50%1/2
$heap->{"latest_worker"} or $heap->{"stopped"}
$heap->{"latest_worker"}
$heap->{"stopped"}
Truth table66%2/3
A or B
A= $heap->{"latest_worker"}B= $heap->{"stopped"}
| ||||||||||||||||||||||||||||||||||
| 200 | } | |||||||||||||||||||||||||||||||||
| 201 | } | |||||||||||||||||||||||||||||||||
| 202 | ||||||||||||||||||||||||||||||||||
| 203 | # Remove the alias, stop active polling and delete outstanding job queue | |||||||||||||||||||||||||||||||||
| 204 | ||||||||||||||||||||||||||||||||||
| 205 | 0 | ▶ | sub poco_jobqueue_both_stop { | |||||||||||||||||||||||||||||||
|
Subroutine
poco_jobqueue_both_stop: not called CC=3 SCAR=24.8
Pod
undocumented
| ||||||||||||||||||||||||||||||||||
| 206 | 0 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||||||||||||||||||||||||||||||||
| 207 | ||||||||||||||||||||||||||||||||||
| 208 | 0 | $kernel->alias_remove($heap->{alias}); | ||||||||||||||||||||||||||||||||
| 209 | 0 | $kernel->alarm_remove_all(); | ||||||||||||||||||||||||||||||||
| 210 | ||||||||||||||||||||||||||||||||||
| 211 | 0 | ▶ | delete $heap->{pollinterval} if ($heap->{pollinterval}); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 212 | 0 | ▶ | delete $heap->{job_queue} if ($heap->{job_queue}); | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 213 | ||||||||||||||||||||||||||||||||||
| 214 | 0 | $heap->{stopped} = 1; | ||||||||||||||||||||||||||||||||
| 215 | } | |||||||||||||||||||||||||||||||||
| 216 | ||||||||||||||||||||||||||||||||||
| 217 | # Attempt to fill empty worker slots. | |||||||||||||||||||||||||||||||||
| 218 | ||||||||||||||||||||||||||||||||||
| 219 | # This is a token for ARG0 that signifies this was a timed event. | |||||||||||||||||||||||||||||||||
| 220 | sub TIMED () { 31415 } | |||||||||||||||||||||||||||||||||
| 221 | ||||||||||||||||||||||||||||||||||
| 222 | 10 | ▶ | sub poco_jobqueue_active_dequeue { | |||||||||||||||||||||||||||||||
|
Subroutine
poco_jobqueue_active_dequeue: called CC=10 SCAR=27.8
Pod
undocumented
| ||||||||||||||||||||||||||||||||||
| 223 | 10 | my ($kernel, $heap, $is_timed) = @_[KERNEL, HEAP, ARG0]; | ||||||||||||||||||||||||||||||||
| 224 | ||||||||||||||||||||||||||||||||||
| 225 | # If this is a poll from a timed event, then decrement the pending | |||||||||||||||||||||||||||||||||
| 226 | # polls count. The pending polls count is just to ensure that | |||||||||||||||||||||||||||||||||
| 227 | # redundant delays are not set, because each redundant delay would | |||||||||||||||||||||||||||||||||
| 228 | # force the existing one forward in time. They would delay polling | |||||||||||||||||||||||||||||||||
| 229 | # past the hard polling interval, which would probably be bad (and | |||||||||||||||||||||||||||||||||
| 230 | # could stave off polling indefinitely in some instances). I think | |||||||||||||||||||||||||||||||||
| 231 | # this is a bit of a hack, and something better should replace it. | |||||||||||||||||||||||||||||||||
| 232 | ||||||||||||||||||||||||||||||||||
| 233 | 10 | ▶ | if (defined $is_timed and $is_timed == TIMED) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $is_timed and $is_timed == 31415
MC/DC0%0/2
defined $is_timed and $is_timed == 31415
defined $is_timed
$is_timed == 31415
Truth table33%1/3
A and B
A= defined $is_timedB= $is_timed == 31415
| ||||||||||||||||||||||||||||||||||
| 234 | # Decrement the number of pending polls. There can be only one, | |||||||||||||||||||||||||||||||||
| 235 | # so throw in a die for assertion testing. | |||||||||||||||||||||||||||||||||
| 236 | die "pending polls should now be zero (not $heap->{pending_polls})" | |||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | if --$heap->{pending_polls}; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 238 | } | |||||||||||||||||||||||||||||||||
| 239 | ||||||||||||||||||||||||||||||||||
| 240 | # Attempt to fill the empty worker slots. | |||||||||||||||||||||||||||||||||
| 241 | 10 | ▶ | while ( | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $heap->{"stopped"} and $heap->{"worker_count"} < $heap->{"worker_limit"}
MC/DC50%1/2
not $heap->{"stopped"} and $heap->{"worker_count"} < $heap->{"worker_limit"}
not $heap->{"stopped"}
$heap->{"worker_count"} < $heap->{"worker_limit"}
Truth table66%2/3
not(A) and B
A= not $heap->{"stopped"}B= $heap->{"worker_count"} < $heap->{"worker_limit"}
| ||||||||||||||||||||||||||||||||||
| 242 | not $heap->{stopped} | |||||||||||||||||||||||||||||||||
| 243 | and $heap->{worker_count} < $heap->{worker_limit} | |||||||||||||||||||||||||||||||||
| 244 | ) { | |||||||||||||||||||||||||||||||||
| 245 | ||||||||||||||||||||||||||||||||||
| 246 | # Call the worker to fetch a new job and spawn a session. | |||||||||||||||||||||||||||||||||
| 247 | 14 | my $previous_worker_count = $heap->{worker_count}; | ||||||||||||||||||||||||||||||||
| 248 | 14 | $heap->{worker_ref}->( $heap->{meta_postback} ); | ||||||||||||||||||||||||||||||||
| 249 | ||||||||||||||||||||||||||||||||||
| 250 | # If the worker count hasn't changed, then we've run out of jobs. | |||||||||||||||||||||||||||||||||
| 251 | # Begin polling, if applicable, and exit the spawn loop. | |||||||||||||||||||||||||||||||||
| 252 | 14 | ▶ | if ($heap->{worker_count} == $previous_worker_count) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 253 | 4 | ▶ | if (defined $heap->{poll_interval} and !$heap->{pending_polls}) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $heap->{"poll_interval"} and not $heap->{"pending_polls"}
MC/DC0%0/2
defined $heap->{"poll_interval"} and not $heap->{"pending_polls"}
defined $heap->{"poll_interval"}
not $heap->{"pending_polls"}
Truth table33%1/3
A and not(B)
A= defined $heap->{"poll_interval"}B= not $heap->{"pending_polls"}
| ||||||||||||||||||||||||||||||||||
| 254 | 0 | $heap->{pending_polls}++; | ||||||||||||||||||||||||||||||||
| 255 | 0 | $kernel->delay( dequeue => $heap->{poll_interval} => TIMED ); | ||||||||||||||||||||||||||||||||
| 256 | } | |||||||||||||||||||||||||||||||||
| 257 | 4 | ▶ | $heap->{latest_worker}++ unless defined $heap->{poll_interval}; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 258 | 4 | last; | ||||||||||||||||||||||||||||||||
| 259 | } | |||||||||||||||||||||||||||||||||
| 260 | } | |||||||||||||||||||||||||||||||||
| 261 | } | |||||||||||||||||||||||||||||||||
| 262 | ||||||||||||||||||||||||||||||||||
| 263 | # Attempt to fill empty worker slots. | |||||||||||||||||||||||||||||||||
| 264 | ||||||||||||||||||||||||||||||||||
| 265 | 20 | ▶ | sub poco_jobqueue_passive_dequeue { | |||||||||||||||||||||||||||||||
|
Subroutine
poco_jobqueue_passive_dequeue: called CC=4 SCAR=13.9
Pod
undocumented
| ||||||||||||||||||||||||||||||||||
| 266 | 20 | my ($kernel, $heap) = @_[KERNEL, HEAP]; | ||||||||||||||||||||||||||||||||
| 267 | ||||||||||||||||||||||||||||||||||
| 268 | # Attempt to fill the empty worker slots. | |||||||||||||||||||||||||||||||||
| 269 | 20 | ▶ | while ( | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $heap->{"stopped"} and $heap->{"worker_count"} < $heap->{"worker_limit"}
MC/DC50%1/2
not $heap->{"stopped"} and $heap->{"worker_count"} < $heap->{"worker_limit"}
not $heap->{"stopped"}
$heap->{"worker_count"} < $heap->{"worker_limit"}
Truth table66%2/3
not(A) and B
A= not $heap->{"stopped"}B= $heap->{"worker_count"} < $heap->{"worker_limit"}
| ||||||||||||||||||||||||||||||||||
| 270 | not $heap->{stopped} | |||||||||||||||||||||||||||||||||
| 271 | and $heap->{worker_count} < $heap->{worker_limit} | |||||||||||||||||||||||||||||||||
| 272 | ) { | |||||||||||||||||||||||||||||||||
| 273 | ||||||||||||||||||||||||||||||||||
| 274 | # Try to fetch another job from the queue. | |||||||||||||||||||||||||||||||||
| 275 | 15 | my $next_job = shift @{ $heap->{job_queue} }; | ||||||||||||||||||||||||||||||||
| 276 | 15 | ▶ | last unless defined $next_job; | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 277 | ||||||||||||||||||||||||||||||||||
| 278 | 10 | DEBUG and | ||||||||||||||||||||||||||||||||
| 279 | warn "JQ: job queue $heap->{alias} is starting a new worker"; | |||||||||||||||||||||||||||||||||
| 280 | ||||||||||||||||||||||||||||||||||
| 281 | # Start a new session with the job. | |||||||||||||||||||||||||||||||||
| 282 | 10 | $heap->{worker_ref}->( @$next_job ); | ||||||||||||||||||||||||||||||||
| 283 | } | |||||||||||||||||||||||||||||||||
| 284 | ||||||||||||||||||||||||||||||||||
| 285 | # Avoid accidentally returning something. | |||||||||||||||||||||||||||||||||
| 286 | 20 | undef; | ||||||||||||||||||||||||||||||||
| 287 | } | |||||||||||||||||||||||||||||||||
| 288 | ||||||||||||||||||||||||||||||||||
| 289 | # Enqueue a job in a passive queue. | |||||||||||||||||||||||||||||||||
| 290 | ||||||||||||||||||||||||||||||||||
| 291 | 10 | ▶ | sub poco_jobqueue_passive_enqueue { | |||||||||||||||||||||||||||||||
|
Subroutine
poco_jobqueue_passive_enqueue: called CC=5 SCAR=16.6
Pod
undocumented
| ||||||||||||||||||||||||||||||||||
| 292 | 10 | my ($kernel, $sender, $heap, $return_state, @job) = | ||||||||||||||||||||||||||||||||
| 293 | @_[KERNEL, SENDER, HEAP, ARG0..$#_]; | |||||||||||||||||||||||||||||||||
| 294 | ||||||||||||||||||||||||||||||||||
| 295 | 10 | ▶ | if ($heap->{stopped}) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 296 | 0 | DEBUG and warn( | ||||||||||||||||||||||||||||||||
| 297 | "JQ: $heap->{alias} can not enqueue new jobs after 'stop'\n" | |||||||||||||||||||||||||||||||||
| 298 | ); | |||||||||||||||||||||||||||||||||
| 299 | ||||||||||||||||||||||||||||||||||
| 300 | 0 | return; | ||||||||||||||||||||||||||||||||
| 301 | } | |||||||||||||||||||||||||||||||||
| 302 | ||||||||||||||||||||||||||||||||||
| 303 | 10 | DEBUG and warn "JQ: job queue $heap->{alias} enqueuing a new job"; | ||||||||||||||||||||||||||||||||
| 304 | ||||||||||||||||||||||||||||||||||
| 305 | 10 | my $postback; | ||||||||||||||||||||||||||||||||
| 306 | 10 | ▶ | if (defined $return_state) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 307 | 10 | $postback = $sender->postback( $return_state, @job ); | ||||||||||||||||||||||||||||||||
| 308 | } | |||||||||||||||||||||||||||||||||
| 309 | ||||||||||||||||||||||||||||||||||
| 310 | # Add the job to the queue. Use the prioritizer to find the right | |||||||||||||||||||||||||||||||||
| 311 | # place to put it. | |||||||||||||||||||||||||||||||||
| 312 | ||||||||||||||||||||||||||||||||||
| 313 | 10 | my $queue_index = @{ $heap->{job_queue} }; | ||||||||||||||||||||||||||||||||
| 314 | 10 | ▶ | while ($queue_index--) { | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 315 | last if | |||||||||||||||||||||||||||||||||
| 316 | 9 | ▶ | $heap->{prioritizer}->( $heap->{job_queue}->[$queue_index], | |||||||||||||||||||||||||||||||
|
Branch: 1 branch
| ||||||||||||||||||||||||||||||||||
| 317 | \@job, | |||||||||||||||||||||||||||||||||
| 318 | ) >= 0; | |||||||||||||||||||||||||||||||||
| 319 | } | |||||||||||||||||||||||||||||||||
| 320 | ||||||||||||||||||||||||||||||||||
| 321 | # Place the new job after the index we found. | |||||||||||||||||||||||||||||||||
| 322 | 10 | splice( @{$heap->{job_queue}}, $queue_index+1, 0, [ $postback, @job ] ); | ||||||||||||||||||||||||||||||||
| 323 | ||||||||||||||||||||||||||||||||||
| 324 | # Dequeue a new event. | |||||||||||||||||||||||||||||||||
| 325 | 10 | $kernel->yield( 'dequeue' ); | ||||||||||||||||||||||||||||||||
| 326 | } | |||||||||||||||||||||||||||||||||
| 327 | ||||||||||||||||||||||||||||||||||
| 328 | 1 | 1; | ||||||||||||||||||||||||||||||||
| 329 | ||||||||||||||||||||||||||||||||||
| 330 | __END__ | |||||||||||||||||||||||||||||||||