| 1 | package Bot::Cobalt::Plugin::Calc::Session; | ||||||||||||
| 2 | 1 | $Bot::Cobalt::Plugin::Calc::Session::VERSION = '0.004004'; | |||||||||||
| 3 | 1 | ▶ | use v5.10; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | |||||||||||||
| 5 | 1 | ▶ | use Config; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 1 | ▶ | use Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 8 | 1 | ▶ | use strictures 2; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 9 | |||||||||||||
| 10 | 1 | ▶ | use Time::HiRes (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | |||||||||||||
| 12 | 1 | ▶ | use POE 'Wheel::Run', 'Filter::Reference'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | sub TIMEOUT () { 0 } | ||||||||||||
| 15 | sub SESSID () { 1 } | ||||||||||||
| 16 | sub WHEELS () { 2 } | ||||||||||||
| 17 | sub REQUESTS () { 3 } | ||||||||||||
| 18 | sub TAG_BY_WID () { 4 } | ||||||||||||
| 19 | sub PENDING () { 5 } | ||||||||||||
| 20 | sub MAX_WORKERS () { 6 } | ||||||||||||
| 21 | sub RESULT_EVENT () { 7 } | ||||||||||||
| 22 | sub ERROR_EVENT () { 8 } | ||||||||||||
| 23 | |||||||||||||
| 24 | sub new { | ||||||||||||
| 25 | 1 | ▶ | my ($class, %params) = @_; | ||||||||||
|
Subroutine
new: called CC=5 SCAR=17.2
Pod
undocumented
| |||||||||||||
| 26 | |||||||||||||
| 27 | # note the Worker proc also has a RLIMIT_CPU in place (where supported): | ||||||||||||
| 28 | 1 | ▶ | my $timeout = $params{timeout} || 2; | ||||||||||
|
Condition50%1/2
$params{'timeout'} || 2
MC/DC0%0/1
$params{'timeout'} || 2
$params{'timeout'}
Truth table50%1/2
A
A= $params{'timeout'}
| |||||||||||||
| 29 | 1 | ▶ | my $maxwrk = $params{max_workers} || 4; | ||||||||||
|
Condition50%1/2
$params{'max_workers'} || 4
MC/DC0%0/1
$params{'max_workers'} || 4
$params{'max_workers'}
Truth table50%1/2
A
A= $params{'max_workers'}
| |||||||||||||
| 30 | |||||||||||||
| 31 | 1 | ▶ | my $result_event = $params{result_event} || 'calc_result'; | ||||||||||
|
Condition50%1/2
$params{'result_event'} || 'calc_result'
MC/DC0%0/1
$params{'result_event'} || 'calc_result'
$params{'result_event'}
Truth table50%1/2
A
A= $params{'result_event'}
| |||||||||||||
| 32 | 1 | ▶ | my $error_event = $params{error_event} || 'calc_error'; | ||||||||||
|
Condition50%1/2
$params{'error_event'} || 'calc_error'
MC/DC0%0/1
$params{'error_event'} || 'calc_error'
$params{'error_event'}
Truth table50%1/2
A
A= $params{'error_event'}
| |||||||||||||
| 33 | |||||||||||||
| 34 | 1 | bless [ | |||||||||||
| 35 | $timeout, # TIMEOUT | ||||||||||||
| 36 | undef, # SESSID | ||||||||||||
| 37 | +{}, # WHEELS | ||||||||||||
| 38 | +{}, # REQUESTS | ||||||||||||
| 39 | +{}, # TAG_BY_WID | ||||||||||||
| 40 | [], # PENDING | ||||||||||||
| 41 | $maxwrk, # MAX_WORKERS | ||||||||||||
| 42 | $result_event, # RESULT_EVENT | ||||||||||||
| 43 | $error_event, # ERROR_EVENT | ||||||||||||
| 44 | ], $class | ||||||||||||
| 45 | } | ||||||||||||
| 46 | |||||||||||||
| 47 | 2 | ▶ | sub session_id { shift->[SESSID] } | ||||||||||
|
Subroutine
session_id: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 48 | |||||||||||||
| 49 | 1 | ▶ | sub _wheels { shift->[WHEELS] } | ||||||||||
|
Subroutine
_wheels: called CC=1 SCAR=0.0
| |||||||||||||
| 50 | 1 | ▶ | sub _tag_by_wid { shift->[TAG_BY_WID] } | ||||||||||
|
Subroutine
_tag_by_wid: called CC=1 SCAR=0.0
| |||||||||||||
| 51 | 1 | ▶ | sub _requests { shift->[REQUESTS] } | ||||||||||
|
Subroutine
_requests: called CC=1 SCAR=0.0
| |||||||||||||
| 52 | |||||||||||||
| 53 | sub start { | ||||||||||||
| 54 | 1 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 55 | |||||||||||||
| 56 | 1 | my $sess = POE::Session->create( | |||||||||||
| 57 | object_states => [ | ||||||||||||
| 58 | $self => +{ | ||||||||||||
| 59 | _start => 'px_start', | ||||||||||||
| 60 | shutdown => 'px_shutdown', | ||||||||||||
| 61 | cleanup => 'px_cleanup', | ||||||||||||
| 62 | |||||||||||||
| 63 | calc => 'px_calc', | ||||||||||||
| 64 | push => 'px_push', | ||||||||||||
| 65 | |||||||||||||
| 66 | worker_timeout => 'px_worker_timeout', | ||||||||||||
| 67 | worker_input => 'px_worker_input', | ||||||||||||
| 68 | worker_stderr => 'px_worker_stderr', | ||||||||||||
| 69 | worker_sigchld => 'px_worker_sigchld', | ||||||||||||
| 70 | worker_closed => 'px_worker_closed', | ||||||||||||
| 71 | }, | ||||||||||||
| 72 | ], | ||||||||||||
| 73 | ); | ||||||||||||
| 74 | |||||||||||||
| 75 | 1 | $self->[SESSID] = $sess->ID; | |||||||||||
| 76 | |||||||||||||
| 77 | 1 | $self | |||||||||||
| 78 | } | ||||||||||||
| 79 | |||||||||||||
| 80 | |||||||||||||
| 81 | sub px_start { | ||||||||||||
| 82 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
px_start: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 83 | 1 | $kernel->refcount_increment( $_[SESSION]->ID, 'Waiting for requests' ); | |||||||||||
| 84 | } | ||||||||||||
| 85 | |||||||||||||
| 86 | sub px_shutdown { | ||||||||||||
| 87 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
px_shutdown: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 88 | 1 | $kernel->call( $_[SESSION], 'cleanup' ); | |||||||||||
| 89 | 1 | $kernel->refcount_decrement( $_[SESSION]->ID, 'Waiting for requests' ); | |||||||||||
| 90 | } | ||||||||||||
| 91 | |||||||||||||
| 92 | sub px_cleanup { | ||||||||||||
| 93 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
px_cleanup: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 94 | 1 | for my $pid (keys %{ $self->[WHEELS] }) { | |||||||||||
| 95 | 1 | ▶ | if (my $wheel = delete $self->[WHEELS]->{$pid}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 96 | 1 | $wheel->kill('TERM') | |||||||||||
| 97 | } | ||||||||||||
| 98 | } | ||||||||||||
| 99 | 1 | $self->[TAG_BY_WID] = +{}; | |||||||||||
| 100 | 1 | $self->[PENDING] = []; | |||||||||||
| 101 | } | ||||||||||||
| 102 | |||||||||||||
| 103 | sub px_calc { | ||||||||||||
| 104 | # calc => $expr, $hints | ||||||||||||
| 105 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
px_calc: called CC=5 SCAR=17.4
Pod
undocumented
| |||||||||||||
| 106 | 1 | my ($expr, $hints) = @_[ARG0, ARG1]; | |||||||||||
| 107 | 1 | my $sender_id = $_[SENDER]->ID; | |||||||||||
| 108 | |||||||||||||
| 109 | 1 | ▶ | unless (defined $expr) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 110 | 0 | warn "'calc' event expected an EXPR and optional hints scalar"; | |||||||||||
| 111 | 0 | ▶ | $kernel->post( $sender_id => $self->[ERROR_EVENT] => | ||||||||||
|
Condition0%0/2
$hints // {}
MC/DC0%0/1
$hints // {}
$hints
Truth table0%0/2
A
A= $hints
| |||||||||||||
| 112 | "EXPR not defined", | ||||||||||||
| 113 | $hints // +{} | ||||||||||||
| 114 | ); | ||||||||||||
| 115 | } | ||||||||||||
| 116 | |||||||||||||
| 117 | 1 | state $p = [ 'a' .. 'z', 1 .. 9 ]; | |||||||||||
| 118 | 1 | my $tag = join '', map {; $p->[rand @$p] } 1 .. 3; | |||||||||||
| 119 | 1 | ▶ | $tag .= $p->[rand @$p] while exists $self->[REQUESTS]->{$tag}; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 120 | |||||||||||||
| 121 | 1 | ▶ | my $pending = +{ | ||||||||||
|
Condition50%1/2
$hints // {}
MC/DC0%0/1
$hints // {}
$hints
Truth table50%1/2
A
A= $hints
| |||||||||||||
| 122 | expr => $expr, | ||||||||||||
| 123 | tag => $tag, | ||||||||||||
| 124 | hints => ($hints // +{}), | ||||||||||||
| 125 | sender_id => $sender_id, | ||||||||||||
| 126 | }; | ||||||||||||
| 127 | |||||||||||||
| 128 | 1 | $self->[REQUESTS]->{$tag} = $pending; | |||||||||||
| 129 | 1 | push @{ $self->[PENDING] }, $pending; | |||||||||||
| 130 | 1 | $kernel->yield('push'); | |||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | sub px_push { | ||||||||||||
| 134 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
px_push: called CC=3 SCAR=11.3
Pod
undocumented
| |||||||||||||
| 135 | |||||||||||||
| 136 | 1 | ▶ | return unless @{ $self->[PENDING] }; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 137 | |||||||||||||
| 138 | 1 | ▶ | if (keys %{ $self->[WHEELS] } >= $self->[MAX_WORKERS]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | 0 | $kernel->delay( push => 0.5 ); | |||||||||||
| 140 | return | ||||||||||||
| 141 | 0 | } | |||||||||||
| 142 | |||||||||||||
| 143 | 1 | my $wheel = $self->_create_wheel; | |||||||||||
| 144 | |||||||||||||
| 145 | 1 | my $next = shift @{ $self->[PENDING] }; | |||||||||||
| 146 | 1 | my $tag = $next->{tag}; | |||||||||||
| 147 | 1 | $self->[TAG_BY_WID]->{ $wheel->ID } = $tag; | |||||||||||
| 148 | |||||||||||||
| 149 | 1 | $kernel->delay( worker_timeout => $self->[TIMEOUT], $wheel ); | |||||||||||
| 150 | 1 | $wheel->put( [ $next->{tag}, $next->{expr} ] ); | |||||||||||
| 151 | } | ||||||||||||
| 152 | |||||||||||||
| 153 | sub _create_wheel { | ||||||||||||
| 154 | 1 | ▶ | my ($self) = @_; | ||||||||||
|
Subroutine
_create_wheel: called CC=4 SCAR=14.4
| |||||||||||||
| 155 | |||||||||||||
| 156 | 1 | my $ppath = $Config{perlpath}; | |||||||||||
| 157 | 1 | ▶ | if ($^O ne 'VMS') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 158 | 1 | ▶ | $ppath .= $Config{_exe} unless $ppath =~ m/$Config{_exe}$/i; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 159 | } | ||||||||||||
| 160 | |||||||||||||
| 161 | 1 | my $forkable; | |||||||||||
| 162 | 1 | ▶ | if ($^O eq 'MSWin32') { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 163 | 0 | require Bot::Cobalt::Plugin::Calc::Worker; | |||||||||||
| 164 | 0 | $forkable = \&Bot::Cobalt::Plugin::Calc::Worker::worker | |||||||||||
| 165 | } else { | ||||||||||||
| 166 | $forkable = [ | ||||||||||||
| 167 | $ppath, | ||||||||||||
| 168 | 1 | (map {; '-I'.$_ } @INC), | |||||||||||
| 169 | '-MBot::Cobalt::Plugin::Calc::Worker', | ||||||||||||
| 170 | '-e', | ||||||||||||
| 171 | 'Bot::Cobalt::Plugin::Calc::Worker->worker' | ||||||||||||
| 172 | ] | ||||||||||||
| 173 | } | ||||||||||||
| 174 | |||||||||||||
| 175 | 1 | my $wheel = POE::Wheel::Run->new( | |||||||||||
| 176 | Program => $forkable, | ||||||||||||
| 177 | StdioFilter => POE::Filter::Reference->new, | ||||||||||||
| 178 | StderrEvent => 'worker_stderr', | ||||||||||||
| 179 | StdoutEvent => 'worker_input', | ||||||||||||
| 180 | CloseEvent => 'worker_closed', | ||||||||||||
| 181 | ); | ||||||||||||
| 182 | |||||||||||||
| 183 | 1 | my $pid = $wheel->PID; | |||||||||||
| 184 | 1 | $poe_kernel->sig_child($pid, 'worker_sigchld'); | |||||||||||
| 185 | 1 | $self->[WHEELS]->{$pid} = $wheel; | |||||||||||
| 186 | |||||||||||||
| 187 | 1 | $wheel | |||||||||||
| 188 | } | ||||||||||||
| 189 | |||||||||||||
| 190 | sub px_worker_timeout { | ||||||||||||
| 191 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
px_worker_timeout: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 192 | 1 | my $wheel = $_[ARG0]; | |||||||||||
| 193 | 1 | $wheel->kill('INT'); | |||||||||||
| 194 | } | ||||||||||||
| 195 | |||||||||||||
| 196 | sub px_worker_input { | ||||||||||||
| 197 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
px_worker_input: called CC=2 SCAR=7.5
Pod
undocumented
| |||||||||||||
| 198 | |||||||||||||
| 199 | 1 | my ($input, $wid) = @_[ARG0, ARG1]; | |||||||||||
| 200 | 1 | my ($tag, $result) = @$input; | |||||||||||
| 201 | |||||||||||||
| 202 | 1 | my $req = delete $self->[REQUESTS]->{$tag}; | |||||||||||
| 203 | 1 | ▶ | unless ($req) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 204 | 0 | warn "BUG? worker input but no request found for tag '$tag'"; | |||||||||||
| 205 | return | ||||||||||||
| 206 | 0 | } | |||||||||||
| 207 | |||||||||||||
| 208 | $kernel->post( $req->{sender_id} => $self->[RESULT_EVENT] => | ||||||||||||
| 209 | $result, $req->{hints} | ||||||||||||
| 210 | ) | ||||||||||||
| 211 | 1 | } | |||||||||||
| 212 | |||||||||||||
| 213 | sub px_worker_stderr { | ||||||||||||
| 214 | 0 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
px_worker_stderr: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||
| 215 | 0 | my ($input, $wid) = @_[ARG0, ARG1]; | |||||||||||
| 216 | 0 | my $tag = $self->[TAG_BY_WID]->{$wid}; | |||||||||||
| 217 | 0 | ▶ | unless (defined $tag) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 218 | 0 | warn "BUG? px_worker_stderr but no tag for wheel ID '$wid'"; | |||||||||||
| 219 | } | ||||||||||||
| 220 | 0 | my $req = delete $self->[REQUESTS]->{$tag}; | |||||||||||
| 221 | 0 | ▶ | if (defined $req) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 222 | 0 | my $sender_id = $req->{sender_id}; | |||||||||||
| 223 | 0 | my $hints = $req->{hints}; | |||||||||||
| 224 | 0 | $kernel->post( $req->{sender_id} => $self->[ERROR_EVENT] => | |||||||||||
| 225 | "worker '$wid': $input", $hints | ||||||||||||
| 226 | ) | ||||||||||||
| 227 | } else { | ||||||||||||
| 228 | 0 | warn "stderr from worker but request unavailable: $input" | |||||||||||
| 229 | } | ||||||||||||
| 230 | } | ||||||||||||
| 231 | |||||||||||||
| 232 | sub px_worker_closed { | ||||||||||||
| 233 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
px_worker_closed: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 234 | 1 | my $wid = $_[ARG0]; | |||||||||||
| 235 | 1 | delete $self->[TAG_BY_WID]->{$wid}; | |||||||||||
| 236 | } | ||||||||||||
| 237 | |||||||||||||
| 238 | sub px_worker_sigchld { | ||||||||||||
| 239 | 1 | ▶ | my ($kernel, $self) = @_[KERNEL, OBJECT]; | ||||||||||
|
Subroutine
px_worker_sigchld: called CC=2 SCAR=8.4
Pod
undocumented
| |||||||||||||
| 240 | 1 | my $pid = $_[ARG1]; | |||||||||||
| 241 | 1 | ▶ | my $wheel = delete $self->[WHEELS]->{$pid} || return; | ||||||||||
|
Condition50%1/2
delete $self->[2]{$pid} || (return)
MC/DC0%0/1
delete $self->[2]{$pid} || (return)
delete $self->[2]{$pid}
Truth table50%1/2
A
A= delete $self->[2]{$pid}
| |||||||||||||
| 242 | 0 | delete $self->[TAG_BY_WID]->{ $wheel->ID }; | |||||||||||
| 243 | 0 | $kernel->yield('push') | |||||||||||
| 244 | } | ||||||||||||
| 245 | |||||||||||||
| 246 | |||||||||||||
| 247 | 1 | 1; | |||||||||||