| 1 | package POE::Component::DBIAgent::Queue; | ||||||||||||
| 2 | |||||||||||||
| 3 | =head1 NAME | ||||||||||||
| 4 | |||||||||||||
| 5 | POE::Component::DBIAgent::Queue -- Helper class for managing a | ||||||||||||
| 6 | round-robin queue of Po:Co:DBIAgent:Helper's. | ||||||||||||
| 7 | |||||||||||||
| 8 | =cut | ||||||||||||
| 9 | |||||||||||||
| 10 | #### originally by Fletch <fletch@phydeaux.org> | ||||||||||||
| 11 | #### originally by Fletch <fletch@phydeaux.org> | ||||||||||||
| 12 | #### originally by Fletch <fletch@phydeaux.org> | ||||||||||||
| 13 | #### See the credits in the AUTHOR section of the POD. | ||||||||||||
| 14 | |||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||
| 16 | |||||||||||||
| 17 | |||||||||||||
| 18 | |||||||||||||
| 19 | =head1 DESCRIPTION | ||||||||||||
| 20 | |||||||||||||
| 21 | |||||||||||||
| 22 | =cut | ||||||||||||
| 23 | |||||||||||||
| 24 | 1 | $VERSION = sprintf("%d.%02d", q$Revision: 0.02 $ =~ /(\d+)\.(\d+)/); | |||||||||||
| 25 | |||||||||||||
| 26 | 1 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 27 | |||||||||||||
| 28 | 1 | ▶ | use Carp qw/ croak carp /; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 29 | |||||||||||||
| 30 | use Class::MethodMaker | ||||||||||||
| 31 | 1 | ▶ | new_with_init => 'new', | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 32 | new_hash_init => 'hash_init', | ||||||||||||
| 33 | list => [ qw( _queue ) ], | ||||||||||||
| 34 | 1 | ; | |||||||||||
| 35 | |||||||||||||
| 36 | |||||||||||||
| 37 | =head2 Methods | ||||||||||||
| 38 | |||||||||||||
| 39 | This are the methods we recognize: | ||||||||||||
| 40 | |||||||||||||
| 41 | =over 4 | ||||||||||||
| 42 | |||||||||||||
| 43 | =item init | ||||||||||||
| 44 | |||||||||||||
| 45 | init the queue (currently noop) | ||||||||||||
| 46 | |||||||||||||
| 47 | =cut | ||||||||||||
| 48 | |||||||||||||
| 49 | 0 | ▶ | sub init { | ||||||||||
|
Subroutine
init: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 50 | 0 | my $self = shift; | |||||||||||
| 51 | |||||||||||||
| 52 | 0 | return $self; | |||||||||||
| 53 | } | ||||||||||||
| 54 | |||||||||||||
| 55 | |||||||||||||
| 56 | =item add | ||||||||||||
| 57 | |||||||||||||
| 58 | append argument to the queue | ||||||||||||
| 59 | |||||||||||||
| 60 | =cut | ||||||||||||
| 61 | |||||||||||||
| 62 | 0 | ▶ | sub add { $_[0]->_queue_push( $_[1] ) } | ||||||||||
|
Subroutine
add: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 63 | |||||||||||||
| 64 | =item clear | ||||||||||||
| 65 | |||||||||||||
| 66 | Clear the queue | ||||||||||||
| 67 | |||||||||||||
| 68 | =cut | ||||||||||||
| 69 | |||||||||||||
| 70 | 0 | ▶ | sub clear { $_[0]->_queue_clear } | ||||||||||
|
Subroutine
clear: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 71 | |||||||||||||
| 72 | ## Internal use only | ||||||||||||
| 73 | ## _find_by -- Return indicies in queue for which supplied predicate | ||||||||||||
| 74 | ## returns true | ||||||||||||
| 75 | ## | ||||||||||||
| 76 | 0 | ▶ | sub _find_by { | ||||||||||
|
Subroutine
_find_by: not called CC=2 SCAR=17.9
| |||||||||||||
| 77 | 0 | my( $self, $predicate ) = @_; | |||||||||||
| 78 | 0 | my $queue = $self->_queue; | |||||||||||
| 79 | 0 | my @ret = grep $predicate->( $queue->[ $_ ] ), 0..$#{$queue}; | |||||||||||
| 80 | 0 | ▶ | return wantarray ? @ret : $ret[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | =item find_by_pid | ||||||||||||
| 84 | |||||||||||||
| 85 | Find the index of helper with specified pid | ||||||||||||
| 86 | |||||||||||||
| 87 | =cut | ||||||||||||
| 88 | |||||||||||||
| 89 | 0 | ▶ | sub find_by_pid { | ||||||||||
|
Subroutine
find_by_pid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 90 | 0 | my( $self, $pid ) = @_; | |||||||||||
| 91 | 0 | ▶ | return $self->_find_by( sub { $_[0]->PID == $pid } ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | =item find_by_wheelid | ||||||||||||
| 95 | |||||||||||||
| 96 | Find the index of helper with specified wheel id | ||||||||||||
| 97 | |||||||||||||
| 98 | =cut | ||||||||||||
| 99 | |||||||||||||
| 100 | 0 | ▶ | sub find_by_wheelid { | ||||||||||
|
Subroutine
find_by_wheelid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 101 | 0 | my( $self, $wheel_id ) = @_; | |||||||||||
| 102 | 0 | ▶ | return $self->_find_by( sub { $_[0]->ID == $wheel_id } ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 103 | } | ||||||||||||
| 104 | |||||||||||||
| 105 | ## Internal use only | ||||||||||||
| 106 | ## _remove_by -- Remove first item from the queue for which supplied | ||||||||||||
| 107 | ## predicate returns true | ||||||||||||
| 108 | ## | ||||||||||||
| 109 | 0 | ▶ | sub _remove_by { | ||||||||||
|
Subroutine
_remove_by: not called CC=2 SCAR=17.9
| |||||||||||||
| 110 | 0 | my( $self, $predicate ) = @_; | |||||||||||
| 111 | 0 | my $index = ( $self->_find_by( $predicate ) )[0]; | |||||||||||
| 112 | |||||||||||||
| 113 | 0 | ▶ | return splice( @{scalar $self->_queue}, $index, 1 ) if defined $index; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 114 | |||||||||||||
| 115 | return | ||||||||||||
| 116 | 0 | } | |||||||||||
| 117 | |||||||||||||
| 118 | =item remove_by_pid | ||||||||||||
| 119 | |||||||||||||
| 120 | Remove helper with specified pid | ||||||||||||
| 121 | |||||||||||||
| 122 | =cut | ||||||||||||
| 123 | |||||||||||||
| 124 | 0 | ▶ | sub remove_by_pid { | ||||||||||
|
Subroutine
remove_by_pid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 125 | 0 | my( $self, $pid ) = @_; | |||||||||||
| 126 | 0 | ▶ | $self->_remove_by( sub { $_[0]->PID == $pid } ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 127 | } | ||||||||||||
| 128 | |||||||||||||
| 129 | =item remove_by_wheelid | ||||||||||||
| 130 | |||||||||||||
| 131 | Remove helper with specified wheel id | ||||||||||||
| 132 | |||||||||||||
| 133 | =cut | ||||||||||||
| 134 | |||||||||||||
| 135 | 0 | ▶ | sub remove_by_wheelid { | ||||||||||
|
Subroutine
remove_by_wheelid: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 136 | 0 | my( $self, $wheel_id ) = @_; | |||||||||||
| 137 | 0 | ▶ | $self->_remove_by( sub { $_[0]->ID == $wheel_id } ); | ||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||
| 138 | } | ||||||||||||
| 139 | |||||||||||||
| 140 | =item next | ||||||||||||
| 141 | |||||||||||||
| 142 | Get next helper off the head of the queue (and put it back on the end | ||||||||||||
| 143 | (round robin)) | ||||||||||||
| 144 | |||||||||||||
| 145 | =cut | ||||||||||||
| 146 | |||||||||||||
| 147 | 0 | ▶ | sub next { | ||||||||||
|
Subroutine
next: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 148 | 0 | my $self = shift; | |||||||||||
| 149 | 0 | my $ret = $self->_queue_shift; | |||||||||||
| 150 | 0 | $self->_queue_push( $ret ); | |||||||||||
| 151 | 0 | return $ret | |||||||||||
| 152 | } | ||||||||||||
| 153 | |||||||||||||
| 154 | =item make_next | ||||||||||||
| 155 | |||||||||||||
| 156 | Force the helper with the specified wheel id to the head of the queue. | ||||||||||||
| 157 | |||||||||||||
| 158 | =cut | ||||||||||||
| 159 | |||||||||||||
| 160 | 0 | ▶ | sub make_next { | ||||||||||
|
Subroutine
make_next: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||
| 161 | 0 | my $self = shift; | |||||||||||
| 162 | 0 | my $id = shift; | |||||||||||
| 163 | 0 | my $ret = $self->remove_by_wheelid( $id ); | |||||||||||
| 164 | 0 | $self->_queue_unshift( $ret ); | |||||||||||
| 165 | } | ||||||||||||
| 166 | |||||||||||||
| 167 | =item exit_all | ||||||||||||
| 168 | |||||||||||||
| 169 | Tell all our helpers to exit gracefully. | ||||||||||||
| 170 | |||||||||||||
| 171 | =cut | ||||||||||||
| 172 | |||||||||||||
| 173 | 0 | ▶ | sub exit_all { | ||||||||||
|
Subroutine
exit_all: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||
| 174 | 0 | my $self = shift; | |||||||||||
| 175 | #++ modified command to stop POE::Filter::Reference moaning | ||||||||||||
| 176 | 0 | $_->put({query => "EXIT"}) foreach $self->_queue; | |||||||||||
| 177 | } | ||||||||||||
| 178 | |||||||||||||
| 179 | |||||||||||||
| 180 | =item kill_all | ||||||||||||
| 181 | |||||||||||||
| 182 | Send the specified signal (default SIGTERM) to all helper processes | ||||||||||||
| 183 | |||||||||||||
| 184 | =cut | ||||||||||||
| 185 | |||||||||||||
| 186 | 0 | ▶ | sub kill_all { | ||||||||||
|
Subroutine
kill_all: not called CC=3 SCAR=24.8
Pod
documented
| |||||||||||||
| 187 | 0 | my $self = shift; | |||||||||||
| 188 | 0 | ▶ | my $sig = shift || 'TERM'; | ||||||||||
|
Condition0%0/2
shift() || 'TERM'
MC/DC0%0/1
shift() || 'TERM'
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||
| 189 | |||||||||||||
| 190 | 0 | my @helpers = map { $_->PID } $self->_queue; | |||||||||||
| 191 | 0 | ▶ | if (@helpers) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 192 | 0 | kill $sig => @helpers; | |||||||||||
| 193 | } | ||||||||||||
| 194 | |||||||||||||
| 195 | # Causes @helpers to be empty on subsequent kill_all() calls. This | ||||||||||||
| 196 | # was here already; I'm just commenting it. | ||||||||||||
| 197 | 0 | $self->_queue_clear; | |||||||||||
| 198 | |||||||||||||
| 199 | return | ||||||||||||
| 200 | 0 | } | |||||||||||
| 201 | |||||||||||||
| 202 | =back | ||||||||||||
| 203 | |||||||||||||
| 204 | =cut | ||||||||||||
| 205 | |||||||||||||
| 206 | 1 | 1; | |||||||||||
| 207 | |||||||||||||
| 208 | __END__ | ||||||||||||