blib/lib/Bot/Cobalt/Plugin/RDB/AsyncSearch.pm untested

filter: Statementstmt 0.0% 0 / 115 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 11 totaltotal 0.0% 0 / 177 CC 12 SCAR 25.8
CC
12
Cov
0%
push_pending
42.0
spawn
12.0
search_rdb
6.0
CRAP 13.2
1 package Bot::Cobalt::Plugin::RDB::AsyncSearch;
2 $Bot::Cobalt::Plugin::RDB::AsyncSearch::VERSION = '0.021003';
3 use Carp;
4 use strictures 2;
5  
6 use Config;
7  
8 use POE qw/
9 Wheel::Run
10 Filter::Reference
11 /;
12  
13 sub new { bless [], shift }
14  
15 sub spawn {
16 my $self = shift;
17 $self = $self->new unless ref $self;
18 my %args = @_;
19 $args{lc $_} = delete $args{$_} for keys %args;
20
21 unless ($args{errorevent} && $args{resultevent}) {
22 croak "Need an ErrorEvent and ResultEvent in spawn()"
23 }
24  
25 my $maxworkers = $args{maxworkers} || 5;
26  
27 my $sess = POE::Session->create(
28 heap => {
29 ErrorEvent => delete $args{errorevent},
30 ResultEvent => delete $args{resultevent},
31 MaxWorkers => $maxworkers,
32
33 Requests => {},
34 Pending => [],
35
36 Wheels => {
37 PID => {},
38 },
39 },
40  
41 object_states => [
42 $self => [
43 '_start',
44 '_stop',
45
46 'shutdown',
47  
48 'search_rdb',
49
50 'push_pending',
51
52 'reap_all',
53
54 'worker_input',
55 'worker_stderr',
56 'worker_sigchld',
57 ],
58 ],
59
60 );
61  
62 return $sess->ID()
63 }
64  
65 sub _start {
66 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
67  
68 $kernel->refcount_increment(
69 $_[SESSION]->ID(), 'Waiting for requests'
70 );
71 }
72  
73 sub _stop {
74 my ($self, $kernel) = @_[OBJECT, KERNEL];
75 $kernel->call( $_[SESSION], 'reap_all' );
76 }
77  
78 sub shutdown {
79 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL];
80  
81 $kernel->call( $_[SESSION], 'reap_all' );
82  
83 $kernel->refcount_decrement(
84 $_[SESSION]->ID(), 'Waiting for requests'
85 );
86 }
87  
88 sub search_rdb {
89 ## search_rdb( $dbpath, $regex, $hints_hash )
90 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
91 my ($dbpath, $regex, $hints) = @_[ARG0, ARG1, ARG2];
92  
93 my $sender = $_[SENDER];
94
95 unless ($dbpath && $regex) {
96 carp "search_rdb posted but no path or regex specified";
97 return
98 }
99
100 my @p = ( 'a' .. 'z', 1 .. 9 );
101 my $unique = join '', map { $p[rand@p] } 1 .. 6;
102 $unique .= $p[rand@p] while exists $heap->{Requests}->{$unique};
103
104 my $item = {
105 Path => $dbpath,
106 Tag => $unique,
107 Regex => $regex,
108 Hints => $hints,
109 SenderID => $sender->ID(),
110 };
111
112 $heap->{Requests}->{$unique} = $item;
113
114 push(@{ $heap->{Pending} }, $item );
115
116 $kernel->yield('push_pending');
117 }
118  
119 sub push_pending {
120 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
121
122 return unless @{ $heap->{Pending} };
123  
124 my $running = keys %{ $heap->{Wheels}->{PID} };
125 if ($running >= $heap->{MaxWorkers} ) {
126 $kernel->alarm('push_pending', time + 1);
127 return
128 }
129
130 ## try to spawn a new wheel
131
132 my $perlpath = $Config{perlpath};
133 if ($^O ne 'VMS') {
134 $perlpath .= $Config{_exe}
135 unless $perlpath =~ m/$Config{_exe}$/i;
136 }
137  
138 ## FIXME workers could be genericized to be used with Info3 also . . .
139 ## would speed up a dsearch
140
141 my $forkable;
142 if ($^O eq 'MSWin32') {
143 ## May drop Win32 support here entirely . . .
144 require Bot::Cobalt::Plugin::RDB::AsyncSearch::Worker;
145 $forkable = \&Bot::Cobalt::Plugin::RDB::AsyncSearch::Worker::worker;
146 } else {
147 $forkable = [
148 $perlpath, (map { "-I$_" } @INC),
149 '-MBot::Cobalt::Plugin::RDB::AsyncSearch::Worker', '-e',
150 'Bot::Cobalt::Plugin::RDB::AsyncSearch::Worker->worker()'
151 ];
152 }
153  
154 my $wheel = POE::Wheel::Run->new(
155 Program => $forkable,
156 StdioFilter => POE::Filter::Reference->new(),
157 ErrorEvent => 'worker_err',
158 StderrEvent => 'worker_stderr',
159 StdoutEvent => 'worker_input',
160 );
161  
162 my $wid = $wheel->ID;
163 my $pid = $wheel->PID;
164
165 $kernel->sig_child($pid, 'worker_sigchld');
166
167 $heap->{Wheels}->{PID}->{$pid} = $wheel;
168  
169 my $next_item = shift @{ $heap->{Pending} };
170
171 my $tag = $next_item->{Tag};
172 $heap->{RequestsByWID}->{$wid} = $tag;
173
174 $wheel->put(
175 [ $next_item->{Path}, $next_item->{Tag}, $next_item->{Regex} ]
176 );
177 }
178  
179 sub reap_all {
180 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
181  
182 for my $pid (keys %{ $heap->{Wheels}->{PID} }) {
183 my $wheel = delete $heap->{Wheels}->{PID}->{$pid};
184 if (ref $wheel) {
185 $wheel->kill('TERM');
186 }
187 }
188
189 $heap->{Wheels} = { PID => {}, WID => {} };
190 $heap->{RequestsByWID} = {};
191 }
192  
193 sub worker_input {
194 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
195 my ($input, $wid) = @_[ARG0, ARG1];
196
197 my ($dbpath, $tag, @results) = @$input;
198
199 my $request = delete $heap->{Requests}->{$tag};
200
201 unless ($request) {
202 carp "severe oddness: results from a request tag we don't know";
203 return
204 }
205
206 my $sender_id = $request->{SenderID};
207 my $hints = $request->{Hints};
208  
209 ## Returns: resultset as arrayref, original hints hash
210 ## (passed in via search)
211 my $event = $heap->{ResultEvent};
212 $kernel->post( $sender_id, $event, \@results, $hints );
213 }
214  
215 sub worker_sigchld {
216 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
217 my $pid = $_[ARG1];
218
219 my $wheel = delete $heap->{Wheels}->{PID}->{$pid};
220  
221 my $wid = $wheel->ID;
222  
223 delete $heap->{RequestsByWID}->{$wid};
224  
225 $kernel->yield( 'push_pending' );
226 }
227  
228 sub worker_stderr {
229 my ($self, $kernel, $heap) = @_[OBJECT, KERNEL, HEAP];
230 my ($input, $wid) = @_[ARG0, ARG1];
231
232 my $request_id = $heap->{RequestsByWID}->{$wid};
233  
234 my $request = delete $heap->{Requests}->{$request_id};
235  
236 if ($request) {
237 my $sender_id = $request->{SenderID};
238 my $hints = $request->{Hints};
239  
240 my $event = $heap->{ErrorEvent};
241  
242 $kernel->post( $sender_id, $event, $input, $hints );
243 } else {
244 carp "stderr from non-avail request: $input"
245 }
246  
247 ## These should sigchld and go away.
248 }
249  
250 1;
251 __END__