File Coverage

blib/lib/POE/Component/DBIAgent/Queue.pm
Criterion Covered Total %
statement 9 53 16.9
branch 0 6 0.0
condition 0 2 0.0
subroutine 3 20 15.0
pod 11 11 100.0
total 23 92 25.0


line stmt bran cond sub pod time code
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
11             #### originally by Fletch
12             #### originally by Fletch
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             $VERSION = sprintf("%d.%02d", q$Revision: 0.02 $ =~ /(\d+)\.(\d+)/);
25              
26 1     1   984 use strict;
  1         3  
  1         48  
27              
28 1     1   5 use Carp qw/ croak carp /;
  1         2  
  1         102  
29              
30             use Class::MethodMaker
31 1         11 new_with_init => 'new',
32             new_hash_init => 'hash_init',
33             list => [ qw( _queue ) ],
34 1     1   1958 ;
  1         27153  
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             sub init {
50 0     0 1   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     0 1   sub add { $_[0]->_queue_push( $_[1] ) }
63              
64             =item clear
65              
66             Clear the queue
67              
68             =cut
69              
70 0     0 1   sub clear { $_[0]->_queue_clear }
71              
72             ## Internal use only
73             ## _find_by -- Return indicies in queue for which supplied predicate
74             ## returns true
75             ##
76             sub _find_by {
77 0     0     my( $self, $predicate ) = @_;
78 0           my $queue = $self->_queue;
79 0           my @ret = grep $predicate->( $queue->[ $_ ] ), 0..$#{$queue};
  0            
80 0 0         return wantarray ? @ret : $ret[0];
81             }
82              
83             =item find_by_pid
84              
85             Find the index of helper with specified pid
86              
87             =cut
88              
89             sub find_by_pid {
90 0     0 1   my( $self, $pid ) = @_;
91 0     0     return $self->_find_by( sub { $_[0]->PID == $pid } );
  0            
92             }
93              
94             =item find_by_wheelid
95              
96             Find the index of helper with specified wheel id
97              
98             =cut
99              
100             sub find_by_wheelid {
101 0     0 1   my( $self, $wheel_id ) = @_;
102 0     0     return $self->_find_by( sub { $_[0]->ID == $wheel_id } );
  0            
103             }
104              
105             ## Internal use only
106             ## _remove_by -- Remove first item from the queue for which supplied
107             ## predicate returns true
108             ##
109             sub _remove_by {
110 0     0     my( $self, $predicate ) = @_;
111 0           my $index = ( $self->_find_by( $predicate ) )[0];
112              
113 0 0         return splice( @{scalar $self->_queue}, $index, 1 ) if defined $index;
  0            
114              
115             return
116 0           }
117              
118             =item remove_by_pid
119              
120             Remove helper with specified pid
121              
122             =cut
123              
124             sub remove_by_pid {
125 0     0 1   my( $self, $pid ) = @_;
126 0     0     $self->_remove_by( sub { $_[0]->PID == $pid } );
  0            
127             }
128              
129             =item remove_by_wheelid
130              
131             Remove helper with specified wheel id
132              
133             =cut
134              
135             sub remove_by_wheelid {
136 0     0 1   my( $self, $wheel_id ) = @_;
137 0     0     $self->_remove_by( sub { $_[0]->ID == $wheel_id } );
  0            
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             sub next {
148 0     0 1   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             sub make_next {
161 0     0 1   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             sub exit_all {
174 0     0 1   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             sub kill_all {
187 0     0 1   my $self = shift;
188 0   0       my $sig = shift || 'TERM';
189              
190 0           my @helpers = map { $_->PID } $self->_queue;
  0            
191 0 0         if (@helpers) {
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;
207              
208             __END__