| 1 | package Venus::Process; | ||
| 2 | |||
| 3 | use 5.018; | ||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use overload ( | ||
| 9 | '""' => 'explain', | ||
| 10 | '~~' => 'explain', | ||
| 11 | fallback => 1, | ||
| 12 | ); | ||
| 13 | |||
| 14 | use Venus::Class 'attr', 'base', 'with'; | ||
| 15 | |||
| 16 | base 'Venus::Kind::Utility'; | ||
| 17 | |||
| 18 | with 'Venus::Role::Valuable'; | ||
| 19 | with 'Venus::Role::Buildable'; | ||
| 20 | with 'Venus::Role::Accessible'; | ||
| 21 | with 'Venus::Role::Explainable'; | ||
| 22 | |||
| 23 | require Config; | ||
| 24 | require Cwd; | ||
| 25 | require File::Spec; | ||
| 26 | require POSIX; | ||
| 27 | |||
| 28 | our $CWD = Cwd->getcwd; | ||
| 29 | our $MAPSIG = {%SIG}; | ||
| 30 | our $PATH = $CWD; | ||
| 31 | our $PID = $$; | ||
| 32 | our $PPID; | ||
| 33 | |||
| 34 | # ATTRIBUTES | ||
| 35 | |||
| 36 | attr 'alarm'; | ||
| 37 | |||
| 38 | # HOOKS | ||
| 39 | |||
| 40 | sub _alarm { | ||
| 41 | CORE::alarm(shift); | ||
| 42 | } | ||
| 43 | |||
| 44 | sub _chdir { | ||
| 45 | CORE::chdir(shift); | ||
| 46 | } | ||
| 47 | |||
| 48 | sub _exit { | ||
| 49 | POSIX::_exit(shift); | ||
| 50 | } | ||
| 51 | |||
| 52 | sub _exitcode { | ||
| 53 | $? >> 8; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub _fork { | ||
| 57 | CORE::fork(); | ||
| 58 | } | ||
| 59 | |||
| 60 | sub _forkable { | ||
| 61 | $Config::Config{d_pseudofork} ? 0 : 1; | ||
| 62 | } | ||
| 63 | |||
| 64 | sub _serve { | ||
| 65 | true; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub _kill { | ||
| 69 | CORE::kill(@_); | ||
| 70 | } | ||
| 71 | |||
| 72 | sub _open { | ||
| 73 | CORE::open(shift, shift, shift); | ||
| 74 | } | ||
| 75 | |||
| 76 | sub _ping { | ||
| 77 | _kill(0, @_); | ||
| 78 | } | ||
| 79 | |||
| 80 | sub _setsid { | ||
| 81 | POSIX::setsid(); | ||
| 82 | } | ||
| 83 | |||
| 84 | sub _time { | ||
| 85 | CORE::time(); | ||
| 86 | } | ||
| 87 | |||
| 88 | sub _waitpid { | ||
| 89 | CORE::waitpid(shift, shift); | ||
| 90 | } | ||
| 91 | |||
| 92 | # BUILD | ||
| 93 | |||
| 94 | sub build_self { | ||
| 95 | my ($self, $data) = @_; | ||
| 96 | |||
| 97 | $PID = $self->value if $self->value; | ||
| 98 | |||
| 99 | return $self; | ||
| 100 | } | ||
| 101 | |||
| 102 | # METHODS | ||
| 103 | |||
| 104 | sub assertion { | ||
| 105 | my ($self) = @_; | ||
| 106 | |||
| 107 | my $assertion = $self->SUPER::assertion; | ||
| 108 | |||
| 109 | $assertion->match('string')->format(sub{ | ||
| 110 | (ref $self || $self)->new($_) | ||
| 111 | }); | ||
| 112 | |||
| 113 | return $assertion; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub async { | ||
| 117 | my ($self, $code, @args) = @_; | ||
| 118 | |||
| 119 | require Venus::Path; | ||
| 120 | |||
| 121 | my $path = $PATH; | ||
| 122 | |||
| 123 | $PATH = Venus::Path->mktemp_dir->absolute->value; | ||
| 124 | |||
| 125 | my $parent = $self->class->new; | ||
| 126 | |||
| 127 | $parent->{directory} = $PATH; | ||
| 128 | |||
| 129 | $parent->register; | ||
| 130 | |||
| 131 | my $pid = $parent->work(sub{ | ||
| 132 | my ($process) = @_; | ||
| 133 | |||
| 134 | $process->untrap; | ||
| 135 | |||
| 136 | $process->{directory} = $PATH; | ||
| 137 | |||
| 138 | $process->register; | ||
| 139 | |||
| 140 | $process->watch($process->ppid); | ||
| 141 | |||
| 142 | $process->ppid(undef); | ||
| 143 | |||
| 144 | my $error; | ||
| 145 | |||
| 146 | my $result = $process->try($code, @args)->error(\$error)->result; | ||
| 147 | |||
| 148 | if (defined $error) { | ||
| 149 | require Scalar::Util; | ||
| 150 | require Venus::Error; | ||
| 151 | $error = Venus::Error->new($error) if !Scalar::Util::blessed($error); | ||
| 152 | $result = $error; | ||
| 153 | } | ||
| 154 | |||
| 155 | $process->sendall($result) if defined $result; | ||
| 156 | |||
| 157 | return; | ||
| 158 | }); | ||
| 159 | |||
| 160 | $parent->watch($pid); | ||
| 161 | |||
| 162 | $parent->trap(int => sub { | ||
| 163 | $parent->killall; | ||
| 164 | }); | ||
| 165 | |||
| 166 | $parent->trap(term => sub { | ||
| 167 | $parent->killall; | ||
| 168 | }); | ||
| 169 | |||
| 170 | $PATH = $path; | ||
| 171 | |||
| 172 | return $parent; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub await { | ||
| 176 | my ($self, $timeout) = @_; | ||
| 177 | |||
| 178 | my $path = $PATH; | ||
| 179 | |||
| 180 | $PATH = $self->{directory}; | ||
| 181 | |||
| 182 | my $result = []; | ||
| 183 | |||
| 184 | if (defined $timeout) { | ||
| 185 | if ($timeout == 0) { | ||
| 186 | (my $error, @{$result}) = $self->catch('recvall'); | ||
| 187 | } | ||
| 188 | else { | ||
| 189 | (my $error, @{$result}) = $self->catch('poll', $timeout, 'recvall'); | ||
| 190 | } | ||
| 191 | } | ||
| 192 | else { | ||
| 193 | do{@{$result} = $self->recvall} while !@{$result}; | ||
| 194 | } | ||
| 195 | |||
| 196 | $self->check($_) for ($self->watchlist); | ||
| 197 | |||
| 198 | $PATH = $path; | ||
| 199 | |||
| 200 | return wantarray ? (@{$result}) : $result; | ||
| 201 | } | ||
| 202 | |||
| 203 | sub chdir { | ||
| 204 | my ($self, $path) = @_; | ||
| 205 | |||
| 206 | $path ||= $CWD; | ||
| 207 | |||
| 208 | _chdir($path) or $self->error({ | ||
| 209 | throw => 'error_on_chdir', | ||
| 210 | path => $path, | ||
| 211 | pid => $PID, | ||
| 212 | error => $! | ||
| 213 | }); | ||
| 214 | |||
| 215 | return $self; | ||
| 216 | } | ||
| 217 | |||
| 218 | sub check { | ||
| 219 | my ($self, $pid) = @_; | ||
| 220 | |||
| 221 | my $result = _waitpid($pid, POSIX::WNOHANG()); | ||
| 222 | |||
| 223 | return wantarray ? ($result, _exitcode) : $result; | ||
| 224 | } | ||
| 225 | |||
| 226 | sub count { | ||
| 227 | my ($self, $code, @args) = @_; | ||
| 228 | |||
| 229 | $code ||= 'watchlist'; | ||
| 230 | |||
| 231 | my @result = $self->$code(@args); | ||
| 232 | |||
| 233 | my $count = (@result == 1 && ref $result[0] eq 'ARRAY') ? @{$result[0]} : @result; | ||
| 234 | |||
| 235 | return $count; | ||
| 236 | } | ||
| 237 | |||
| 238 | sub daemon { | ||
| 239 | my ($self) = @_; | ||
| 240 | |||
| 241 | if (my $process = $self->fork) { | ||
| 242 | return $process->disengage->do('setsid'); | ||
| 243 | } | ||
| 244 | else { | ||
| 245 | return $self->exit; | ||
| 246 | } | ||
| 247 | } | ||
| 248 | |||
| 249 | sub data { | ||
| 250 | my ($self, @args) = @_; | ||
| 251 | |||
| 252 | my @pids = @args ? @args : ($self->watchlist); | ||
| 253 | |||
| 254 | return 0 if !@pids; | ||
| 255 | |||
| 256 | my $result = 0; | ||
| 257 | |||
| 258 | require Venus::Path; | ||
| 259 | |||
| 260 | my $path = Venus::Path->new($PATH); | ||
| 261 | |||
| 262 | $path = $path->child($self->exchange) if $self->exchange; | ||
| 263 | |||
| 264 | for my $pid (@pids) { | ||
| 265 | next if !(my $pdir = $path->child($self->recv_key($pid)))->exists; | ||
| 266 | |||
| 267 | $result += 1 for CORE::glob($pdir->child('*.data')->absolute); | ||
| 268 | } | ||
| 269 | |||
| 270 | return $result; | ||
| 271 | } | ||
| 272 | |||
| 273 | sub decode { | ||
| 274 | my ($self, $data) = @_; | ||
| 275 | |||
| 276 | require Venus::Dump; | ||
| 277 | |||
| 278 | return Venus::Dump->new->decode($data); | ||
| 279 | } | ||
| 280 | |||
| 281 | sub default { | ||
| 282 | return $PID; | ||
| 283 | } | ||
| 284 | |||
| 285 | sub disengage { | ||
| 286 | my ($self) = @_; | ||
| 287 | |||
| 288 | $self->chdir(File::Spec->rootdir); | ||
| 289 | |||
| 290 | $self->$_(File::Spec->devnull) for qw(stdin stdout stderr); | ||
| 291 | |||
| 292 | return $self; | ||
| 293 | } | ||
| 294 | |||
| 295 | sub encode { | ||
| 296 | my ($self, $data) = @_; | ||
| 297 | |||
| 298 | require Venus::Dump; | ||
| 299 | |||
| 300 | return Venus::Dump->new($data)->encode; | ||
| 301 | } | ||
| 302 | |||
| 303 | sub engage { | ||
| 304 | my ($self) = @_; | ||
| 305 | |||
| 306 | $self->chdir; | ||
| 307 | |||
| 308 | $self->$_ for qw(stdin stdout stderr); | ||
| 309 | |||
| 310 | return $self; | ||
| 311 | } | ||
| 312 | |||
| 313 | sub exchange { | ||
| 314 | my ($self, $name) = @_; | ||
| 315 | |||
| 316 | return $name ? ($self->{exchange} = $name) : $self->{exchange}; | ||
| 317 | } | ||
| 318 | |||
| 319 | sub exit { | ||
| 320 | my ($self, $code) = @_; | ||
| 321 | |||
| 322 | return _exit($code // 0); | ||
| 323 | } | ||
| 324 | |||
| 325 | sub explain { | ||
| 326 | my ($self) = @_; | ||
| 327 | |||
| 328 | return $self->get; | ||
| 329 | } | ||
| 330 | |||
| 331 | sub followers { | ||
| 332 | my ($self) = @_; | ||
| 333 | |||
| 334 | my $leader = $self->leader; | ||
| 335 | |||
| 336 | my $result = [sort grep {$_ != $leader} $self->others_active, $self->pid]; | ||
| 337 | |||
| 338 | return wantarray ? @{$result} : $result; | ||
| 339 | } | ||
| 340 | |||
| 341 | sub fork { | ||
| 342 | my ($self, $code, @args) = @_; | ||
| 343 | |||
| 344 | if (not(_forkable())) { | ||
| 345 | $self->error({throw => 'error_on_fork_support', pid => $PID}); | ||
| 346 | } | ||
| 347 | if (defined(my $pid = _fork())) { | ||
| 348 | if ($pid) { | ||
| 349 | $self->watch($pid); | ||
| 350 | return wantarray ? (undef, $pid) : undef; | ||
| 351 | } | ||
| 352 | |||
| 353 | $PPID = $PID; | ||
| 354 | $PID = $$; | ||
| 355 | my $process = $self->class->new; | ||
| 356 | my $orig_seed = srand; | ||
| 357 | my $self_seed = substr(((time ^ $$) ** 2), 0, length($orig_seed)); | ||
| 358 | srand $self_seed; | ||
| 359 | |||
| 360 | _alarm($self->alarm) if defined $self->alarm; | ||
| 361 | |||
| 362 | if ($code) { | ||
| 363 | local $_ = $process; | ||
| 364 | $process->$code(@args); | ||
| 365 | } | ||
| 366 | |||
| 367 | return wantarray ? ($process, $PID) : $process; | ||
| 368 | } | ||
| 369 | else { | ||
| 370 | $self->error({throw => 'error_on_fork_process', error => $!, pid => $PID}); | ||
| 371 | } | ||
| 372 | } | ||
| 373 | |||
| 374 | sub forks { | ||
| 375 | my ($self, $count, $code, @args) = @_; | ||
| 376 | |||
| 377 | my $pid; | ||
| 378 | my @pids; | ||
| 379 | my $process; | ||
| 380 | |||
| 381 | for (my $i = 1; $i <= ($count || 0); $i++) { | ||
| 382 | ($process, $pid) = $self->fork($code, @args, $i); | ||
| 383 | if (!$process) { | ||
| 384 | push @pids, $pid; | ||
| 385 | } | ||
| 386 | if ($process) { | ||
| 387 | last; | ||
| 388 | } | ||
| 389 | } | ||
| 390 | |||
| 391 | return wantarray ? ($process ? ($process, []) : ($process, [@pids]) ) : $process; | ||
| 392 | } | ||
| 393 | |||
| 394 | sub future { | ||
| 395 | my ($self, $code, @args) = @_; | ||
| 396 | |||
| 397 | my $async = $self->async($code, @args); | ||
| 398 | |||
| 399 | require Venus::Future; | ||
| 400 | |||
| 401 | my $retry = 0; | ||
| 402 | |||
| 403 | my $future = Venus::Future->new(sub{ | ||
| 404 | my ($resolve, $reject) = @_; | ||
| 405 | my ($result) = $async->try('await', 0)->error(\my $error)->result; | ||
| 406 | if (defined $error) { | ||
| 407 | return $reject->result($error); | ||
| 408 | } | ||
| 409 | if (defined $result) { | ||
| 410 | if (UNIVERSAL::isa($result, 'Venus::Error')) { | ||
| 411 | return $reject->result($result); | ||
| 412 | } | ||
| 413 | else { | ||
| 414 | return $resolve->result($result); | ||
| 415 | } | ||
| 416 | } | ||
| 417 | if ($retry++ > 1 && !$async->ping($async->watchlist)) { | ||
| 418 | return $reject->result($async->catch('error', { | ||
| 419 | throw => 'error_on_ping', | ||
| 420 | pid => ($async->watchlist)[0] | ||
| 421 | })); | ||
| 422 | } | ||
| 423 | }); | ||
| 424 | |||
| 425 | return $future; | ||
| 426 | } | ||
| 427 | |||
| 428 | sub is_dyadic { | ||
| 429 | my ($self) = @_; | ||
| 430 | |||
| 431 | my $directory = $self->{directory}; | ||
| 432 | |||
| 433 | my $path = $PATH; | ||
| 434 | |||
| 435 | my $temporary = $PATH = $directory if $directory; | ||
| 436 | |||
| 437 | my $is_dyadic = $directory && ($temporary) && $self->is_registered && ($self->count == 1) ? true : false; | ||
| 438 | |||
| 439 | $PATH = $path; | ||
| 440 | |||
| 441 | return $is_dyadic; | ||
| 442 | } | ||
| 443 | |||
| 444 | sub is_leader { | ||
| 445 | my ($self) = @_; | ||
| 446 | |||
| 447 | return $self->leader == $self->pid ? true : false; | ||
| 448 | } | ||
| 449 | |||
| 450 | sub is_follower { | ||
| 451 | my ($self) = @_; | ||
| 452 | |||
| 453 | return $self->is_leader ? false : true; | ||
| 454 | } | ||
| 455 | |||
| 456 | sub is_registered { | ||
| 457 | my ($self) = @_; | ||
| 458 | |||
| 459 | return (grep {$_ == $self->pid} $self->registrants) ? true : false; | ||
| 460 | } | ||
| 461 | |||
| 462 | sub is_unregistered { | ||
| 463 | my ($self) = @_; | ||
| 464 | |||
| 465 | return $self->is_registered ? false : true; | ||
| 466 | } | ||
| 467 | |||
| 468 | sub join { | ||
| 469 | my ($self, $name) = @_; | ||
| 470 | |||
| 471 | $self->exchange($name) if $name; | ||
| 472 | |||
| 473 | $self->register; | ||
| 474 | |||
| 475 | @{$self->watchlist} = (); | ||
| 476 | |||
| 477 | return $self; | ||
| 478 | } | ||
| 479 | |||
| 480 | sub kill { | ||
| 481 | my ($self, $name, @pids) = @_; | ||
| 482 | |||
| 483 | return _kill(uc($name), @pids); | ||
| 484 | } | ||
| 485 | |||
| 486 | sub killall { | ||
| 487 | my ($self, $name, @pids) = @_; | ||
| 488 | |||
| 489 | $name ||= 'INT'; | ||
| 490 | |||
| 491 | my $result = [map $self->kill($name, $_), (@pids ? @pids : $self->watchlist)]; | ||
| 492 | |||
| 493 | return wantarray ? @{$result} : $result; | ||
| 494 | } | ||
| 495 | |||
| 496 | sub leader { | ||
| 497 | my ($self) = @_; | ||
| 498 | |||
| 499 | my $leader = (sort $self->others_active, $self->pid)[0]; | ||
| 500 | |||
| 501 | return $leader; | ||
| 502 | } | ||
| 503 | |||
| 504 | sub leave { | ||
| 505 | my ($self, $name) = @_; | ||
| 506 | |||
| 507 | $self->unregister; | ||
| 508 | |||
| 509 | delete $self->{exchange}; | ||
| 510 | delete $self->{watchlist}; | ||
| 511 | |||
| 512 | return $self; | ||
| 513 | } | ||
| 514 | |||
| 515 | sub limit { | ||
| 516 | my ($self, $count) = @_; | ||
| 517 | |||
| 518 | if ($self->count >= $count) { | ||
| 519 | $self->prune while $self->count >= $count; | ||
| 520 | return true; | ||
| 521 | } | ||
| 522 | else { | ||
| 523 | return false; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | |||
| 527 | sub others { | ||
| 528 | my ($self) = @_; | ||
| 529 | |||
| 530 | my $pid = $self->pid; | ||
| 531 | |||
| 532 | my $result = [grep {$_ != $pid} $self->registrants]; | ||
| 533 | |||
| 534 | return wantarray ? @{$result} : $result; | ||
| 535 | } | ||
| 536 | |||
| 537 | sub others_active { | ||
| 538 | my ($self) = @_; | ||
| 539 | |||
| 540 | my $pid = $self->pid; | ||
| 541 | |||
| 542 | my $result = [grep $self->ping($_), $self->others]; | ||
| 543 | |||
| 544 | return wantarray ? @{$result} : $result; | ||
| 545 | } | ||
| 546 | |||
| 547 | sub others_inactive { | ||
| 548 | my ($self) = @_; | ||
| 549 | |||
| 550 | my $pid = $self->pid; | ||
| 551 | |||
| 552 | my $result = [grep !$self->ping($_), $self->others]; | ||
| 553 | |||
| 554 | return wantarray ? @{$result} : $result; | ||
| 555 | } | ||
| 556 | |||
| 557 | sub pid { | ||
| 558 | my ($self, @data) = @_; | ||
| 559 | |||
| 560 | return $self->value if !@data; | ||
| 561 | |||
| 562 | return $self->value((($PID) = @data)); | ||
| 563 | } | ||
| 564 | |||
| 565 | sub pids { | ||
| 566 | my ($self) = @_; | ||
| 567 | |||
| 568 | my $result = [$self->pid, $self->watchlist]; | ||
| 569 | |||
| 570 | return wantarray ? @{$result} : $result; | ||
| 571 | } | ||
| 572 | |||
| 573 | sub ppid { | ||
| 574 | my ($self, @data) = @_; | ||
| 575 | |||
| 576 | my $pid = @data ? (($PPID) = @data) : $PPID; | ||
| 577 | |||
| 578 | return $pid; | ||
| 579 | } | ||
| 580 | |||
| 581 | sub ping { | ||
| 582 | my ($self, @pids) = @_; | ||
| 583 | |||
| 584 | return _ping(@pids); | ||
| 585 | } | ||
| 586 | |||
| 587 | sub poll { | ||
| 588 | my ($self, $timeout, $code, @args) = @_; | ||
| 589 | |||
| 590 | if (!$code) { | ||
| 591 | $code = 'recvall'; | ||
| 592 | } | ||
| 593 | |||
| 594 | if (!$timeout) { | ||
| 595 | $timeout = 0; | ||
| 596 | } | ||
| 597 | |||
| 598 | my $result = []; | ||
| 599 | |||
| 600 | my $time = _time(); | ||
| 601 | my $then = $time + $timeout; | ||
| 602 | my $seen = 0; | ||
| 603 | |||
| 604 | while (time <= $then) { | ||
| 605 | last if $seen = (@{$result} = grep defined, $self->$code(@args)); | ||
| 606 | } | ||
| 607 | |||
| 608 | if (!$seen) { | ||
| 609 | $self->error({ | ||
| 610 | throw => 'error_on_timeout_poll', | ||
| 611 | timeout => $timeout, | ||
| 612 | code => $code | ||
| 613 | }); | ||
| 614 | } | ||
| 615 | |||
| 616 | return wantarray ? @{$result} : $result; | ||
| 617 | } | ||
| 618 | |||
| 619 | sub pool { | ||
| 620 | my ($self, $count, $timeout) = @_; | ||
| 621 | |||
| 622 | if (!$count) { | ||
| 623 | $count = 1; | ||
| 624 | } | ||
| 625 | |||
| 626 | if (!$timeout) { | ||
| 627 | $timeout = 0; | ||
| 628 | } | ||
| 629 | |||
| 630 | my @pids; | ||
| 631 | my $time = _time(); | ||
| 632 | my $then = $time + $timeout; | ||
| 633 | my $seen = 0; | ||
| 634 | |||
| 635 | while (time <= $then) { | ||
| 636 | last if ($seen = (@pids = $self->others_active)) >= $count; | ||
| 637 | } | ||
| 638 | |||
| 639 | if ($seen < $count) { | ||
| 640 | $self->error({ | ||
| 641 | throw => 'error_on_timeout_pool', | ||
| 642 | timeout => $timeout, | ||
| 643 | pool_size => $count | ||
| 644 | }); | ||
| 645 | } | ||
| 646 | |||
| 647 | @{$self->watchlist} = @pids; | ||
| 648 | |||
| 649 | return $self; | ||
| 650 | } | ||
| 651 | |||
| 652 | sub prune { | ||
| 653 | my ($self) = @_; | ||
| 654 | |||
| 655 | $self->unwatch($self->stopped); | ||
| 656 | |||
| 657 | return $self; | ||
| 658 | } | ||
| 659 | |||
| 660 | sub read { | ||
| 661 | my ($self, $key) = @_; | ||
| 662 | |||
| 663 | require Venus::Path; | ||
| 664 | |||
| 665 | my $path = Venus::Path->new($PATH); | ||
| 666 | |||
| 667 | $path = $path->child($self->exchange) if $self->exchange; | ||
| 668 | |||
| 669 | $path = $path->child($key); | ||
| 670 | |||
| 671 | $path->catch('mkdirs') if !$path->exists; | ||
| 672 | |||
| 673 | my $file = (CORE::glob($path->child('*.data')->absolute))[0]; | ||
| 674 | |||
| 675 | return undef if !$file; | ||
| 676 | |||
| 677 | $path = Venus::Path->new($file); | ||
| 678 | |||
| 679 | my $data = $path->read; | ||
| 680 | |||
| 681 | $path->unlink; | ||
| 682 | |||
| 683 | return $data; | ||
| 684 | } | ||
| 685 | |||
| 686 | sub recall { | ||
| 687 | my ($self, $pid) = @_; | ||
| 688 | |||
| 689 | ($pid) = grep {$_ == $pid} $self->others_inactive if $pid; | ||
| 690 | |||
| 691 | return undef if !$pid; | ||
| 692 | |||
| 693 | my $key = $self->send_key($pid); | ||
| 694 | |||
| 695 | my $string = $self->read($key); | ||
| 696 | |||
| 697 | return undef if !defined $string; | ||
| 698 | |||
| 699 | my $data = $self->decode($string); | ||
| 700 | |||
| 701 | return $data; | ||
| 702 | } | ||
| 703 | |||
| 704 | sub recallall { | ||
| 705 | my ($self) = @_; | ||
| 706 | |||
| 707 | my $result = []; | ||
| 708 | |||
| 709 | for my $pid (grep defined, $self->ppid, $self->watchlist) { | ||
| 710 | my $data = $self->recall($pid); | ||
| 711 | push @{$result}, $data if defined $data; | ||
| 712 | } | ||
| 713 | |||
| 714 | return wantarray ? @{$result} : $result; | ||
| 715 | } | ||
| 716 | |||
| 717 | sub recv { | ||
| 718 | my ($self, $pid) = @_; | ||
| 719 | |||
| 720 | return undef if !$pid; | ||
| 721 | |||
| 722 | my $key = $self->recv_key($pid); | ||
| 723 | |||
| 724 | my $string = $self->read($key); | ||
| 725 | |||
| 726 | return undef if !defined $string; | ||
| 727 | |||
| 728 | my $data = $self->decode($string); | ||
| 729 | |||
| 730 | return $data; | ||
| 731 | } | ||
| 732 | |||
| 733 | sub recvall { | ||
| 734 | my ($self) = @_; | ||
| 735 | |||
| 736 | my $result = []; | ||
| 737 | |||
| 738 | for my $pid (grep defined, $self->ppid, $self->watchlist) { | ||
| 739 | my $data = $self->recv($pid); | ||
| 740 | push @{$result}, $data if defined $data; | ||
| 741 | } | ||
| 742 | |||
| 743 | return wantarray ? @{$result} : $result; | ||
| 744 | } | ||
| 745 | |||
| 746 | sub recv_key { | ||
| 747 | my ($self, $pid) = @_; | ||
| 748 | |||
| 749 | return CORE::join '.', $pid, $self->pid; | ||
| 750 | } | ||
| 751 | |||
| 752 | sub register { | ||
| 753 | my ($self) = @_; | ||
| 754 | |||
| 755 | require Venus::Path; | ||
| 756 | |||
| 757 | my $path = Venus::Path->new($PATH); | ||
| 758 | |||
| 759 | $path = $path->child($self->exchange) if $self->exchange; | ||
| 760 | |||
| 761 | my $key = $self->send_key($self->pid); | ||
| 762 | |||
| 763 | $path = $path->child($key); | ||
| 764 | |||
| 765 | $path->catch('mkdirs') if !$path->exists; | ||
| 766 | |||
| 767 | return $self; | ||
| 768 | } | ||
| 769 | |||
| 770 | sub registrants { | ||
| 771 | my ($self) = @_; | ||
| 772 | |||
| 773 | require Venus::Path; | ||
| 774 | |||
| 775 | my $path = Venus::Path->new($PATH); | ||
| 776 | |||
| 777 | $path = $path->child($self->exchange) if $self->exchange; | ||
| 778 | |||
| 779 | my $result = [ | ||
| 780 | map /(\d+)$/, grep /(\d+)\.\1$/, CORE::glob($path->child('*.*')->absolute) | ||
| 781 | ]; | ||
| 782 | |||
| 783 | return wantarray ? @{$result} : $result; | ||
| 784 | } | ||
| 785 | |||
| 786 | sub restart { | ||
| 787 | my ($self, $code) = @_; | ||
| 788 | |||
| 789 | my $result = []; | ||
| 790 | |||
| 791 | $self->status(sub { | ||
| 792 | push @{$result}, $code->(@_) if ($_[1] == -1) || ($_[1] == $_[0]) | ||
| 793 | }); | ||
| 794 | |||
| 795 | return wantarray ? @{$result} : $result; | ||
| 796 | } | ||
| 797 | |||
| 798 | sub send { | ||
| 799 | my ($self, $pid, $data) = @_; | ||
| 800 | |||
| 801 | return $self if !$pid; | ||
| 802 | |||
| 803 | my $string = $self->encode($data); | ||
| 804 | |||
| 805 | my $key = $self->send_key($pid); | ||
| 806 | |||
| 807 | $self->write($key, $string); | ||
| 808 | |||
| 809 | return $self; | ||
| 810 | } | ||
| 811 | |||
| 812 | sub sendall { | ||
| 813 | my ($self, $data) = @_; | ||
| 814 | |||
| 815 | for my $pid (grep defined, $self->ppid, $self->watchlist) { | ||
| 816 | $self->send($pid, $data); | ||
| 817 | } | ||
| 818 | |||
| 819 | return $self; | ||
| 820 | } | ||
| 821 | |||
| 822 | sub send_key { | ||
| 823 | my ($self, $pid) = @_; | ||
| 824 | |||
| 825 | return CORE::join '.', $self->pid, $pid; | ||
| 826 | } | ||
| 827 | |||
| 828 | sub serve { | ||
| 829 | my ($self, $count, $code) = @_; | ||
| 830 | |||
| 831 | do {$self->work($code) until $self->limit($count)} while _serve; | ||
| 832 | |||
| 833 | return $self; | ||
| 834 | } | ||
| 835 | |||
| 836 | sub setsid { | ||
| 837 | my ($self) = @_; | ||
| 838 | |||
| 839 | return _setsid != -1 | ||
| 840 | || $self->error({throw => 'error_on_setid', pid => $PID, error => $!}); | ||
| 841 | } | ||
| 842 | |||
| 843 | sub started { | ||
| 844 | my ($self) = @_; | ||
| 845 | |||
| 846 | my $result = []; | ||
| 847 | |||
| 848 | $self->status(sub { | ||
| 849 | push @{$result}, $_[0] if $_[1] > -1 && $_[1] != $_[0] | ||
| 850 | }); | ||
| 851 | |||
| 852 | return wantarray ? @{$result} : $result; | ||
| 853 | } | ||
| 854 | |||
| 855 | sub status { | ||
| 856 | my ($self, $code) = @_; | ||
| 857 | |||
| 858 | my $result = []; | ||
| 859 | my $watchlist = $self->watchlist; | ||
| 860 | |||
| 861 | for my $pid (@{$watchlist}) { | ||
| 862 | local $_ = $pid; | ||
| 863 | push @{$result}, $code->($pid, $self->check($pid)); | ||
| 864 | } | ||
| 865 | |||
| 866 | return wantarray ? @{$result} : $result; | ||
| 867 | } | ||
| 868 | |||
| 869 | sub stderr { | ||
| 870 | my ($self, $path) = @_; | ||
| 871 | |||
| 872 | state $STDERR; | ||
| 873 | |||
| 874 | if (!$STDERR) { | ||
| 875 | _open($STDERR, '>&', \*STDERR); | ||
| 876 | } | ||
| 877 | if (!$path) { | ||
| 878 | _open(\*STDERR, '>&', $STDERR); | ||
| 879 | } | ||
| 880 | else { | ||
| 881 | _open(\*STDERR, '>&', IO::File->new($path, 'w')) or $self->error({ | ||
| 882 | throw => 'error_on_stderr', | ||
| 883 | path => $path, | ||
| 884 | pid => $PID, | ||
| 885 | error => $! | ||
| 886 | }); | ||
| 887 | } | ||
| 888 | |||
| 889 | return $self; | ||
| 890 | } | ||
| 891 | |||
| 892 | sub stdin { | ||
| 893 | my ($self, $path) = @_; | ||
| 894 | |||
| 895 | state $STDIN; | ||
| 896 | |||
| 897 | if (!$STDIN) { | ||
| 898 | _open($STDIN, '<&', \*STDIN); | ||
| 899 | } | ||
| 900 | if (!$path) { | ||
| 901 | _open(\*STDIN, '<&', $STDIN); | ||
| 902 | } | ||
| 903 | else { | ||
| 904 | _open(\*STDIN, '<&', IO::File->new($path, 'r')) or $self->error({ | ||
| 905 | throw => 'error_on_stdin', | ||
| 906 | path => $path, | ||
| 907 | pid => $PID, | ||
| 908 | error => $! | ||
| 909 | }); | ||
| 910 | } | ||
| 911 | |||
| 912 | return $self; | ||
| 913 | } | ||
| 914 | |||
| 915 | sub stdout { | ||
| 916 | my ($self, $path) = @_; | ||
| 917 | |||
| 918 | state $STDOUT; | ||
| 919 | |||
| 920 | if (!$STDOUT) { | ||
| 921 | _open($STDOUT, '>&', \*STDOUT); | ||
| 922 | } | ||
| 923 | if (!$path) { | ||
| 924 | _open(\*STDOUT, '>&', $STDOUT); | ||
| 925 | } | ||
| 926 | else { | ||
| 927 | _open(\*STDOUT, '>&', IO::File->new($path, 'w')) or $self->error({ | ||
| 928 | throw => 'error_on_stdout', | ||
| 929 | path => $path, | ||
| 930 | pid => $PID, | ||
| 931 | error => $! | ||
| 932 | }); | ||
| 933 | } | ||
| 934 | |||
| 935 | return $self; | ||
| 936 | } | ||
| 937 | |||
| 938 | sub stopped { | ||
| 939 | my ($self) = @_; | ||
| 940 | |||
| 941 | my $result = []; | ||
| 942 | |||
| 943 | $self->status(sub { | ||
| 944 | push @{$result}, $_[0] if ($_[1] == -1) || ($_[1] == $_[0]) | ||
| 945 | }); | ||
| 946 | |||
| 947 | return wantarray ? @{$result} : $result; | ||
| 948 | } | ||
| 949 | |||
| 950 | sub sync { | ||
| 951 | my ($self, $count, $timeout) = @_; | ||
| 952 | |||
| 953 | if (!$count) { | ||
| 954 | $count = 1; | ||
| 955 | } | ||
| 956 | |||
| 957 | if (!$timeout) { | ||
| 958 | $timeout = 0; | ||
| 959 | } | ||
| 960 | |||
| 961 | my $time = _time(); | ||
| 962 | my $then = $time + $timeout; | ||
| 963 | my $msgs = 0; | ||
| 964 | |||
| 965 | while (time <= $then) { | ||
| 966 | last if ($msgs = (scalar grep $self->data($_), $self->pool->watchlist)) >= $count; | ||
| 967 | } | ||
| 968 | |||
| 969 | if ($msgs < $count) { | ||
| 970 | $self->error({ | ||
| 971 | throw => 'error_on_timeout_sync', | ||
| 972 | timeout => $timeout, | ||
| 973 | pool_size => $count | ||
| 974 | }); | ||
| 975 | } | ||
| 976 | |||
| 977 | return $self; | ||
| 978 | } | ||
| 979 | |||
| 980 | sub trap { | ||
| 981 | my ($self, $name, $expr) = @_; | ||
| 982 | |||
| 983 | $SIG{uc($name)} = !ref($expr) ? uc($expr) : sub { | ||
| 984 | local($!, $?); | ||
| 985 | return $self->$expr->(uc($name), @_) if ref $expr eq 'CODE'; | ||
| 986 | }; | ||
| 987 | |||
| 988 | return $self; | ||
| 989 | } | ||
| 990 | |||
| 991 | sub wait { | ||
| 992 | my ($self, $pid) = @_; | ||
| 993 | |||
| 994 | my $result = _waitpid($pid, 0); | ||
| 995 | |||
| 996 | return wantarray ? ($result, _exitcode) : $result; | ||
| 997 | } | ||
| 998 | |||
| 999 | sub waitall { | ||
| 1000 | my ($self, @pids) = @_; | ||
| 1001 | |||
| 1002 | my $result = [map [$self->wait($_)], @pids ? @pids : $self->watchlist]; | ||
| 1003 | |||
| 1004 | return wantarray ? @{$result} : $result; | ||
| 1005 | } | ||
| 1006 | |||
| 1007 | sub watch { | ||
| 1008 | my ($self, @args) = @_; | ||
| 1009 | |||
| 1010 | my $watchlist = $self->watchlist; | ||
| 1011 | |||
| 1012 | my %seen; @{$watchlist} = grep !$seen{$_}++, @{$watchlist}, @args; | ||
| 1013 | |||
| 1014 | return wantarray ? @{$watchlist} : $watchlist; | ||
| 1015 | } | ||
| 1016 | |||
| 1017 | sub watchlist { | ||
| 1018 | my ($self) = @_; | ||
| 1019 | |||
| 1020 | my $watchlist = $self->{watchlist} ||= []; | ||
| 1021 | |||
| 1022 | return wantarray ? @{$watchlist} : $watchlist; | ||
| 1023 | } | ||
| 1024 | |||
| 1025 | sub work { | ||
| 1026 | my ($self, $code, @args) = @_; | ||
| 1027 | |||
| 1028 | my @returned = $self->fork(sub{ | ||
| 1029 | my ($process) = @_; | ||
| 1030 | local $_ = $process; | ||
| 1031 | $process->$code(@args); | ||
| 1032 | $process->exit; | ||
| 1033 | }); | ||
| 1034 | |||
| 1035 | return $returned[-1]; | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | sub works { | ||
| 1039 | my ($self, $count, $code, @args) = @_; | ||
| 1040 | |||
| 1041 | my $result = []; | ||
| 1042 | |||
| 1043 | for (my $i = 1; $i <= ($count || 0); $i++) { | ||
| 1044 | push @{$result}, scalar($self->work($code, @args)); | ||
| 1045 | } | ||
| 1046 | |||
| 1047 | return wantarray ? @{$result} : $result; | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | sub write { | ||
| 1051 | my ($self, $key, $data) = @_; | ||
| 1052 | |||
| 1053 | require Venus::Path; | ||
| 1054 | |||
| 1055 | my $path = Venus::Path->new($PATH); | ||
| 1056 | |||
| 1057 | $path = $path->child($self->exchange) if $self->exchange; | ||
| 1058 | |||
| 1059 | $path = $path->child($key); | ||
| 1060 | |||
| 1061 | $path->catch('mkdirs') if !$path->exists; | ||
| 1062 | |||
| 1063 | state $atom = 0; | ||
| 1064 | state $time = time; | ||
| 1065 | |||
| 1066 | ($atom = ($time == time) ? $atom + 1 : 1); | ||
| 1067 | |||
| 1068 | $path = $path->child(CORE::join '.', time, $atom, 'data'); | ||
| 1069 | |||
| 1070 | $time = time; | ||
| 1071 | |||
| 1072 | $path->write($data); | ||
| 1073 | |||
| 1074 | return $self; | ||
| 1075 | } | ||
| 1076 | |||
| 1077 | sub unregister { | ||
| 1078 | my ($self) = @_; | ||
| 1079 | |||
| 1080 | require Venus::Path; | ||
| 1081 | |||
| 1082 | my $path = Venus::Path->new($PATH); | ||
| 1083 | |||
| 1084 | $path = $path->child($self->exchange) if $self->exchange; | ||
| 1085 | |||
| 1086 | my $key = $self->recv_key($self->pid); | ||
| 1087 | |||
| 1088 | $path = $path->child($key); | ||
| 1089 | |||
| 1090 | $path->rmdirs if $path->exists; | ||
| 1091 | |||
| 1092 | return $self; | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | sub untrap { | ||
| 1096 | my ($self, $name) = @_; | ||
| 1097 | |||
| 1098 | if ($name) { | ||
| 1099 | $SIG{uc($name)} = $$MAPSIG{uc($name)}; | ||
| 1100 | } | ||
| 1101 | else { | ||
| 1102 | %SIG = %$MAPSIG; | ||
| 1103 | } | ||
| 1104 | |||
| 1105 | return $self; | ||
| 1106 | } | ||
| 1107 | |||
| 1108 | sub unwatch { | ||
| 1109 | my ($self, @args) = @_; | ||
| 1110 | |||
| 1111 | my $watchlist = $self->watchlist; | ||
| 1112 | |||
| 1113 | my %seen = map +($_, 1), @args; | ||
| 1114 | |||
| 1115 | @{$watchlist} = grep !$seen{$_}++, @{$watchlist}, @args; | ||
| 1116 | |||
| 1117 | return wantarray ? @{$watchlist} : $watchlist; | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | # DESTROY | ||
| 1121 | |||
| 1122 | sub DESTROY { | ||
| 1123 | my ($self, @data) = @_; | ||
| 1124 | |||
| 1125 | $self->SUPER::DESTROY(@data); | ||
| 1126 | |||
| 1127 | if ($self->is_dyadic && !$self->others) { | ||
| 1128 | $self->unregister; | ||
| 1129 | require Venus::Path; Venus::Path->new($self->{directory})->rmdirs; | ||
| 1130 | } | ||
| 1131 | |||
| 1132 | return $self; | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | # ERRORS | ||
| 1136 | |||
| 1137 | sub error_on_chdir { | ||
| 1138 | my ($self, $data) = @_; | ||
| 1139 | |||
| 1140 | my $message = 'Can\'t chdir "{{path}}": {{error}}'; | ||
| 1141 | |||
| 1142 | my $stash = { | ||
| 1143 | error => $data->{error}, | ||
| 1144 | path => $data->{path}, | ||
| 1145 | pid => $data->{pid}, | ||
| 1146 | }; | ||
| 1147 | |||
| 1148 | my $result = { | ||
| 1149 | name => 'on.chdir', | ||
| 1150 | raise => true, | ||
| 1151 | stash => $stash, | ||
| 1152 | message => $message, | ||
| 1153 | }; | ||
| 1154 | |||
| 1155 | return $result; | ||
| 1156 | } | ||
| 1157 | |||
| 1158 | sub error_on_fork_process { | ||
| 1159 | my ($self, $data) = @_; | ||
| 1160 | |||
| 1161 | my $message = 'Can\'t fork process {{pid}}: {{error}}'; | ||
| 1162 | |||
| 1163 | my $stash = { | ||
| 1164 | error => $data->{error}, | ||
| 1165 | pid => $data->{pid}, | ||
| 1166 | }; | ||
| 1167 | |||
| 1168 | my $result = { | ||
| 1169 | name => 'on.fork.process', | ||
| 1170 | raise => true, | ||
| 1171 | stash => $stash, | ||
| 1172 | message => $message, | ||
| 1173 | }; | ||
| 1174 | |||
| 1175 | return $result; | ||
| 1176 | } | ||
| 1177 | |||
| 1178 | sub error_on_fork_support { | ||
| 1179 | my ($self, $data) = @_; | ||
| 1180 | |||
| 1181 | my $message = 'Can\'t fork process {{pid}}: Fork emulation not supported'; | ||
| 1182 | |||
| 1183 | my $stash = { | ||
| 1184 | pid => $data->{pid}, | ||
| 1185 | }; | ||
| 1186 | |||
| 1187 | my $result = { | ||
| 1188 | name => 'on.fork.support', | ||
| 1189 | raise => true, | ||
| 1190 | stash => $stash, | ||
| 1191 | message => $message, | ||
| 1192 | }; | ||
| 1193 | |||
| 1194 | return $result; | ||
| 1195 | } | ||
| 1196 | |||
| 1197 | sub error_on_ping { | ||
| 1198 | my ($self, $data) = @_; | ||
| 1199 | |||
| 1200 | my $message = 'Process {{pid}} not responding to ping'; | ||
| 1201 | |||
| 1202 | my $stash = { | ||
| 1203 | pid => $data->{pid}, | ||
| 1204 | }; | ||
| 1205 | |||
| 1206 | my $result = { | ||
| 1207 | name => 'on.ping', | ||
| 1208 | raise => true, | ||
| 1209 | stash => $stash, | ||
| 1210 | message => $message, | ||
| 1211 | }; | ||
| 1212 | |||
| 1213 | return $result; | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | sub error_on_setid { | ||
| 1217 | my ($self, $data) = @_; | ||
| 1218 | |||
| 1219 | my $message = 'Can\'t start a new session: {{error}}'; | ||
| 1220 | |||
| 1221 | my $stash = { | ||
| 1222 | error => $data->{error}, | ||
| 1223 | pid => $data->{pid}, | ||
| 1224 | }; | ||
| 1225 | |||
| 1226 | my $result = { | ||
| 1227 | name => 'on.setid', | ||
| 1228 | raise => true, | ||
| 1229 | stash => $stash, | ||
| 1230 | message => $message, | ||
| 1231 | }; | ||
| 1232 | |||
| 1233 | return $result; | ||
| 1234 | } | ||
| 1235 | |||
| 1236 | sub error_on_stderr { | ||
| 1237 | my ($self, $data) = @_; | ||
| 1238 | |||
| 1239 | my $message = 'Can\'t redirect STDERR to "{{path}}": {{error}}'; | ||
| 1240 | |||
| 1241 | my $stash = { | ||
| 1242 | error => $data->{error}, | ||
| 1243 | path => $data->{path}, | ||
| 1244 | pid => $data->{pid}, | ||
| 1245 | }; | ||
| 1246 | |||
| 1247 | my $result = { | ||
| 1248 | name => 'on.stderr', | ||
| 1249 | raise => true, | ||
| 1250 | stash => $stash, | ||
| 1251 | message => $message, | ||
| 1252 | }; | ||
| 1253 | |||
| 1254 | return $result; | ||
| 1255 | } | ||
| 1256 | |||
| 1257 | sub error_on_stdin { | ||
| 1258 | my ($self, $data) = @_; | ||
| 1259 | |||
| 1260 | my $message = 'Can\'t redirect STDIN to "{{path}}": {{error}}'; | ||
| 1261 | |||
| 1262 | my $stash = { | ||
| 1263 | error => $data->{error}, | ||
| 1264 | path => $data->{path}, | ||
| 1265 | pid => $data->{pid}, | ||
| 1266 | }; | ||
| 1267 | |||
| 1268 | my $result = { | ||
| 1269 | name => 'on.stdin', | ||
| 1270 | raise => true, | ||
| 1271 | stash => $stash, | ||
| 1272 | message => $message, | ||
| 1273 | }; | ||
| 1274 | |||
| 1275 | return $result; | ||
| 1276 | } | ||
| 1277 | |||
| 1278 | sub error_on_stdout { | ||
| 1279 | my ($self, $data) = @_; | ||
| 1280 | |||
| 1281 | my $message = 'Can\'t redirect STDOUT to "{{path}}": {{error}}'; | ||
| 1282 | |||
| 1283 | my $stash = { | ||
| 1284 | error => $data->{error}, | ||
| 1285 | path => $data->{path}, | ||
| 1286 | pid => $data->{pid}, | ||
| 1287 | }; | ||
| 1288 | |||
| 1289 | my $result = { | ||
| 1290 | name => 'on.stdout', | ||
| 1291 | raise => true, | ||
| 1292 | stash => $stash, | ||
| 1293 | message => $message, | ||
| 1294 | }; | ||
| 1295 | |||
| 1296 | return $result; | ||
| 1297 | } | ||
| 1298 | |||
| 1299 | sub error_on_timeout_poll { | ||
| 1300 | my ($self, $data) = @_; | ||
| 1301 | |||
| 1302 | my $message = CORE::join ' ', 'Timed out after {{timeout}} seconds', | ||
| 1303 | 'in process {{pid}} while polling {{name}}'; | ||
| 1304 | |||
| 1305 | my $stash = { | ||
| 1306 | code => $data->{code}, | ||
| 1307 | exchange => $self->exchange, | ||
| 1308 | name => (ref $data->{code} eq 'CODE' ? '__ANON__' : $data->{code}), | ||
| 1309 | pid => $self->pid, | ||
| 1310 | timeout => $data->{timeout}, | ||
| 1311 | }; | ||
| 1312 | |||
| 1313 | my $result = { | ||
| 1314 | name => 'on.timeout.poll', | ||
| 1315 | raise => true, | ||
| 1316 | stash => $stash, | ||
| 1317 | message => $message, | ||
| 1318 | }; | ||
| 1319 | |||
| 1320 | return $result; | ||
| 1321 | } | ||
| 1322 | |||
| 1323 | sub error_on_timeout_pool { | ||
| 1324 | my ($self, $data) = @_; | ||
| 1325 | |||
| 1326 | my $message = CORE::join ' ', 'Timed out after {{timeout}} seconds', | ||
| 1327 | 'in process {{pid}} while pooling'; | ||
| 1328 | |||
| 1329 | my $stash = { | ||
| 1330 | pool_size => $data->{pool_size}, | ||
| 1331 | exchange => $self->exchange, | ||
| 1332 | pid => $self->pid, | ||
| 1333 | timeout => $data->{timeout}, | ||
| 1334 | }; | ||
| 1335 | |||
| 1336 | my $result = { | ||
| 1337 | name => 'on.timeout.pool', | ||
| 1338 | raise => true, | ||
| 1339 | stash => $stash, | ||
| 1340 | message => $message, | ||
| 1341 | }; | ||
| 1342 | |||
| 1343 | return $result; | ||
| 1344 | } | ||
| 1345 | |||
| 1346 | sub error_on_timeout_sync { | ||
| 1347 | my ($self, $data) = @_; | ||
| 1348 | |||
| 1349 | my $message = CORE::join ' ', 'Timed out after {{timeout}} seconds', | ||
| 1350 | 'in process {{pid}} while syncing'; | ||
| 1351 | |||
| 1352 | my $stash = { | ||
| 1353 | pool_size => $data->{pool_size}, | ||
| 1354 | exchange => $self->exchange, | ||
| 1355 | pid => $self->pid, | ||
| 1356 | timeout => $data->{timeout}, | ||
| 1357 | }; | ||
| 1358 | |||
| 1359 | my $result = { | ||
| 1360 | name => 'on.timeout.sync', | ||
| 1361 | raise => true, | ||
| 1362 | stash => $stash, | ||
| 1363 | message => $message, | ||
| 1364 | }; | ||
| 1365 | |||
| 1366 | return $result; | ||
| 1367 | } | ||
| 1368 | |||
| 1369 | 1; | ||
| 1370 | |||
| 1371 | |||
| 1372 | |||
| 1373 | =head1 NAME | ||
| 1374 | |||
| 1375 | Venus::Process - Process Class | ||
| 1376 | |||
| 1377 | =cut | ||
| 1378 | |||
| 1379 | =head1 ABSTRACT | ||
| 1380 | |||
| 1381 | Process Class for Perl 5 | ||
| 1382 | |||
| 1383 | =cut | ||
| 1384 | |||
| 1385 | =head1 SYNOPSIS | ||
| 1386 | |||
| 1387 | package main; | ||
| 1388 | |||
| 1389 | use Venus::Process; | ||
| 1390 | |||
| 1391 | my $parent = Venus::Process->new; | ||
| 1392 | |||
| 1393 | my $process = $parent->fork; | ||
| 1394 | |||
| 1395 | if ($process) { | ||
| 1396 | # do something in child process ... | ||
| 1397 | $process->exit; | ||
| 1398 | } | ||
| 1399 | else { | ||
| 1400 | # do something in parent process ... | ||
| 1401 | $parent->wait(-1); | ||
| 1402 | } | ||
| 1403 | |||
| 1404 | # $parent->exit; | ||
| 1405 | |||
| 1406 | =cut | ||
| 1407 | |||
| 1408 | =head1 DESCRIPTION | ||
| 1409 | |||
| 1410 | This package provides methods for handling and forking processes. | ||
| 1411 | |||
| 1412 | =cut | ||
| 1413 | |||
| 1414 | =head1 ATTRIBUTES | ||
| 1415 | |||
| 1416 | This package has the following attributes: | ||
| 1417 | |||
| 1418 | =cut | ||
| 1419 | |||
| 1420 | =head2 alarm | ||
| 1421 | |||
| 1422 | alarm(number $seconds) (number) | ||
| 1423 | |||
| 1424 | The alarm attribute is used in calls to L<alarm> when the process is forked, | ||
| 1425 | installing an alarm in the forked process if set. | ||
| 1426 | |||
| 1427 | I<Since C<2.40>> | ||
| 1428 | |||
| 1429 | =over 4 | ||
| 1430 | |||
| 1431 | =item alarm example 1 | ||
| 1432 | |||
| 1433 | # given: synopsis | ||
| 1434 | |||
| 1435 | package main; | ||
| 1436 | |||
| 1437 | my $alarm = $parent->alarm; | ||
| 1438 | |||
| 1439 | # undef | ||
| 1440 | |||
| 1441 | =back | ||
| 1442 | |||
| 1443 | =over 4 | ||
| 1444 | |||
| 1445 | =item alarm example 2 | ||
| 1446 | |||
| 1447 | # given: synopsis | ||
| 1448 | |||
| 1449 | package main; | ||
| 1450 | |||
| 1451 | my $alarm = $parent->alarm(10); | ||
| 1452 | |||
| 1453 | # 10 | ||
| 1454 | |||
| 1455 | =back | ||
| 1456 | |||
| 1457 | =cut | ||
| 1458 | |||
| 1459 | =head1 INHERITS | ||
| 1460 | |||
| 1461 | This package inherits behaviors from: | ||
| 1462 | |||
| 1463 | L<Venus::Kind::Utility> | ||
| 1464 | |||
| 1465 | =cut | ||
| 1466 | |||
| 1467 | =head1 INTEGRATES | ||
| 1468 | |||
| 1469 | This package integrates behaviors from: | ||
| 1470 | |||
| 1471 | L<Venus::Role::Accessible> | ||
| 1472 | |||
| 1473 | L<Venus::Role::Buildable> | ||
| 1474 | |||
| 1475 | L<Venus::Role::Explainable> | ||
| 1476 | |||
| 1477 | L<Venus::Role::Valuable> | ||
| 1478 | |||
| 1479 | =cut | ||
| 1480 | |||
| 1481 | =head1 METHODS | ||
| 1482 | |||
| 1483 | This package provides the following methods: | ||
| 1484 | |||
| 1485 | =cut | ||
| 1486 | |||
| 1487 | =head2 async | ||
| 1488 | |||
| 1489 | async(coderef $code, any @args) (Venus::Process) | ||
| 1490 | |||
| 1491 | The async method creates a new L<Venus::Process> object and asynchronously runs | ||
| 1492 | the callback provided via the L</work> method. Both process objects are | ||
| 1493 | configured to be are dyadic, i.e. representing an exclusing bi-directoral | ||
| 1494 | relationship. Additionally, the callback return value will be automatically | ||
| 1495 | made available via the L</await> method unless it's undefined. This method | ||
| 1496 | returns the newly created L<"dyadic"|/is_dyadic> process object. | ||
| 1497 | |||
| 1498 | I<Since C<3.40>> | ||
| 1499 | |||
| 1500 | =over 4 | ||
| 1501 | |||
| 1502 | =item async example 1 | ||
| 1503 | |||
| 1504 | # given: synopsis; | ||
| 1505 | |||
| 1506 | my $async = $parent->async(sub{ | ||
| 1507 | my ($process) = @_; | ||
| 1508 | # in forked process ... | ||
| 1509 | $process->exit; | ||
| 1510 | }); | ||
| 1511 | |||
| 1512 | # bless({...}, 'Venus::Process') | ||
| 1513 | |||
| 1514 | =back | ||
| 1515 | |||
| 1516 | =cut | ||
| 1517 | |||
| 1518 | =head2 await | ||
| 1519 | |||
| 1520 | await(number $timeout) (arrayref) | ||
| 1521 | |||
| 1522 | The await method expects to operate on a L<"dyadic"|/is_dyadic> process object | ||
| 1523 | and blocks the execution of the current process until a value is received from | ||
| 1524 | its couterpart. If a timeout is provided, execution will be blocked until a | ||
| 1525 | value is received or the wait time expires. If a timeout of C<0> is provided, | ||
| 1526 | execution will not be blocked. If no timeout is provided at all, execution will | ||
| 1527 | block indefinitely. | ||
| 1528 | |||
| 1529 | I<Since C<3.40>> | ||
| 1530 | |||
| 1531 | =over 4 | ||
| 1532 | |||
| 1533 | =item await example 1 | ||
| 1534 | |||
| 1535 | # given: synopsis; | ||
| 1536 | |||
| 1537 | my $async = $parent->async(sub{ | ||
| 1538 | ($process) = @_; | ||
| 1539 | # in forked process ... | ||
| 1540 | return 'done'; | ||
| 1541 | }); | ||
| 1542 | |||
| 1543 | my $await = $async->await; | ||
| 1544 | |||
| 1545 | # ['done'] | ||
| 1546 | |||
| 1547 | =back | ||
| 1548 | |||
| 1549 | =over 4 | ||
| 1550 | |||
| 1551 | =item await example 2 | ||
| 1552 | |||
| 1553 | # given: synopsis; | ||
| 1554 | |||
| 1555 | my $async = $parent->async(sub{ | ||
| 1556 | ($process) = @_; | ||
| 1557 | # in forked process ... | ||
| 1558 | return {status => 'done'}; | ||
| 1559 | }); | ||
| 1560 | |||
| 1561 | my $await = $async->await; | ||
| 1562 | |||
| 1563 | # [{status => 'done'}] | ||
| 1564 | |||
| 1565 | =back | ||
| 1566 | |||
| 1567 | =over 4 | ||
| 1568 | |||
| 1569 | =item await example 3 | ||
| 1570 | |||
| 1571 | # given: synopsis; | ||
| 1572 | |||
| 1573 | my $async = $parent->async(sub{ | ||
| 1574 | ($process) = @_; | ||
| 1575 | # in forked process ... | ||
| 1576 | return 'done'; | ||
| 1577 | }); | ||
| 1578 | |||
| 1579 | my ($await) = $async->await; | ||
| 1580 | |||
| 1581 | # 'done' | ||
| 1582 | |||
| 1583 | =back | ||
| 1584 | |||
| 1585 | =over 4 | ||
| 1586 | |||
| 1587 | =item await example 4 | ||
| 1588 | |||
| 1589 | # given: synopsis; | ||
| 1590 | |||
| 1591 | my $async = $parent->async(sub{ | ||
| 1592 | ($process) = @_; | ||
| 1593 | # in forked process ... | ||
| 1594 | return {status => 'done'}; | ||
| 1595 | }); | ||
| 1596 | |||
| 1597 | my ($await) = $async->await; | ||
| 1598 | |||
| 1599 | # {status => 'done'} | ||
| 1600 | |||
| 1601 | =back | ||
| 1602 | |||
| 1603 | =over 4 | ||
| 1604 | |||
| 1605 | =item await example 5 | ||
| 1606 | |||
| 1607 | # given: synopsis; | ||
| 1608 | |||
| 1609 | my $async = $parent->async(sub{ | ||
| 1610 | ($process) = @_; | ||
| 1611 | # in forked process ... | ||
| 1612 | $process->sendall('send 1'); | ||
| 1613 | $process->sendall('send 2'); | ||
| 1614 | $process->sendall('send 3'); | ||
| 1615 | return; | ||
| 1616 | }); | ||
| 1617 | |||
| 1618 | my $await; | ||
| 1619 | |||
| 1620 | my $results = []; | ||
| 1621 | |||
| 1622 | push @$results, $async->await; | ||
| 1623 | |||
| 1624 | # 'send 1' | ||
| 1625 | |||
| 1626 | push @$results, $async->await; | ||
| 1627 | |||
| 1628 | # 'send 2' | ||
| 1629 | |||
| 1630 | push @$results, $async->await; | ||
| 1631 | |||
| 1632 | # 'send 3' | ||
| 1633 | |||
| 1634 | $results; | ||
| 1635 | |||
| 1636 | # ['send 1', 'send 2', 'send 3'] | ||
| 1637 | |||
| 1638 | =back | ||
| 1639 | |||
| 1640 | =cut | ||
| 1641 | |||
| 1642 | =head2 chdir | ||
| 1643 | |||
| 1644 | chdir(string $path) (Venus::Process) | ||
| 1645 | |||
| 1646 | The chdir method changes the working directory the current process is operating | ||
| 1647 | within. | ||
| 1648 | |||
| 1649 | I<Since C<0.06>> | ||
| 1650 | |||
| 1651 | =over 4 | ||
| 1652 | |||
| 1653 | =item chdir example 1 | ||
| 1654 | |||
| 1655 | # given: synopsis; | ||
| 1656 | |||
| 1657 | $parent = $parent->chdir; | ||
| 1658 | |||
| 1659 | # bless({...}, 'Venus::Process') | ||
| 1660 | |||
| 1661 | =back | ||
| 1662 | |||
| 1663 | =over 4 | ||
| 1664 | |||
| 1665 | =item chdir example 2 | ||
| 1666 | |||
| 1667 | # given: synopsis; | ||
| 1668 | |||
| 1669 | $parent = $parent->chdir('/tmp'); | ||
| 1670 | |||
| 1671 | # bless({...}, 'Venus::Process') | ||
| 1672 | |||
| 1673 | =back | ||
| 1674 | |||
| 1675 | =over 4 | ||
| 1676 | |||
| 1677 | =item chdir example 3 | ||
| 1678 | |||
| 1679 | # given: synopsis; | ||
| 1680 | |||
| 1681 | $parent = $parent->chdir('/xyz'); | ||
| 1682 | |||
| 1683 | # Exception! (isa Venus::Process::Error) (see error_on_chdir) | ||
| 1684 | |||
| 1685 | =back | ||
| 1686 | |||
| 1687 | =cut | ||
| 1688 | |||
| 1689 | =head2 check | ||
| 1690 | |||
| 1691 | check(number $pid) (number, number) | ||
| 1692 | |||
| 1693 | The check method does a non-blocking L<perlfunc/waitpid> operation and returns | ||
| 1694 | the wait status. In list context, returns the specified process' exit code (if | ||
| 1695 | terminated). | ||
| 1696 | |||
| 1697 | I<Since C<0.06>> | ||
| 1698 | |||
| 1699 | =over 4 | ||
| 1700 | |||
| 1701 | =item check example 1 | ||
| 1702 | |||
| 1703 | package main; | ||
| 1704 | |||
| 1705 | use Venus::Process; | ||
| 1706 | |||
| 1707 | my $parent = Venus::Process->new; | ||
| 1708 | |||
| 1709 | my ($process, $pid) = $parent->fork; | ||
| 1710 | |||
| 1711 | if ($process) { | ||
| 1712 | # in forked process ... | ||
| 1713 | $process->exit; | ||
| 1714 | } | ||
| 1715 | |||
| 1716 | my $check = $parent->check($pid); | ||
| 1717 | |||
| 1718 | # 0 | ||
| 1719 | |||
| 1720 | =back | ||
| 1721 | |||
| 1722 | =over 4 | ||
| 1723 | |||
| 1724 | =item check example 2 | ||
| 1725 | |||
| 1726 | package main; | ||
| 1727 | |||
| 1728 | use Venus::Process; | ||
| 1729 | |||
| 1730 | my $parent = Venus::Process->new; | ||
| 1731 | |||
| 1732 | my ($process, $pid) = $parent->fork; | ||
| 1733 | |||
| 1734 | if ($process) { | ||
| 1735 | # in forked process ... | ||
| 1736 | $process->exit; | ||
| 1737 | } | ||
| 1738 | |||
| 1739 | my ($check, $status) = $parent->check('00000'); | ||
| 1740 | |||
| 1741 | # (-1, -1) | ||
| 1742 | |||
| 1743 | =back | ||
| 1744 | |||
| 1745 | =over 4 | ||
| 1746 | |||
| 1747 | =item check example 3 | ||
| 1748 | |||
| 1749 | package main; | ||
| 1750 | |||
| 1751 | use Venus::Process; | ||
| 1752 | |||
| 1753 | my $parent = Venus::Process->new; | ||
| 1754 | |||
| 1755 | my ($process, $pid) = $parent->fork(sub{ $_->exit(1) }); | ||
| 1756 | |||
| 1757 | if ($process) { | ||
| 1758 | # in forked process ... | ||
| 1759 | $process->exit; | ||
| 1760 | } | ||
| 1761 | |||
| 1762 | my ($check, $status) = $parent->check($pid); | ||
| 1763 | |||
| 1764 | # ($pid, 1) | ||
| 1765 | |||
| 1766 | =back | ||
| 1767 | |||
| 1768 | =cut | ||
| 1769 | |||
| 1770 | =head2 count | ||
| 1771 | |||
| 1772 | count(string | coderef $code, any @args) (number) | ||
| 1773 | |||
| 1774 | The count method dispatches to the method specified (or the L</watchlist> if | ||
| 1775 | not specified) and returns a count of the items returned from the dispatched | ||
| 1776 | call. | ||
| 1777 | |||
| 1778 | I<Since C<2.40>> | ||
| 1779 | |||
| 1780 | =over 4 | ||
| 1781 | |||
| 1782 | =item count example 1 | ||
| 1783 | |||
| 1784 | package main; | ||
| 1785 | |||
| 1786 | use Venus::Process; | ||
| 1787 | |||
| 1788 | my $parent = Venus::Process->new; | ||
| 1789 | |||
| 1790 | my $count = $parent->count; | ||
| 1791 | |||
| 1792 | # 0 | ||
| 1793 | |||
| 1794 | =back | ||
| 1795 | |||
| 1796 | =over 4 | ||
| 1797 | |||
| 1798 | =item count example 2 | ||
| 1799 | |||
| 1800 | package main; | ||
| 1801 | |||
| 1802 | use Venus::Process; | ||
| 1803 | |||
| 1804 | my $parent = Venus::Process->new; | ||
| 1805 | |||
| 1806 | my ($pid) = $parent->watch(1001); | ||
| 1807 | |||
| 1808 | my $count = $parent->count; | ||
| 1809 | |||
| 1810 | # 1 | ||
| 1811 | |||
| 1812 | =back | ||
| 1813 | |||
| 1814 | =over 4 | ||
| 1815 | |||
| 1816 | =item count example 3 | ||
| 1817 | |||
| 1818 | package main; | ||
| 1819 | |||
| 1820 | use Venus::Process; | ||
| 1821 | |||
| 1822 | my $parent = Venus::Process->new; | ||
| 1823 | |||
| 1824 | my ($pid) = $parent->watch(1001); | ||
| 1825 | |||
| 1826 | my $count = $parent->count('watchlist'); | ||
| 1827 | |||
| 1828 | # 1 | ||
| 1829 | |||
| 1830 | =back | ||
| 1831 | |||
| 1832 | =cut | ||
| 1833 | |||
| 1834 | =head2 daemon | ||
| 1835 | |||
| 1836 | daemon() (Venus::Process) | ||
| 1837 | |||
| 1838 | The daemon method detaches the process from controlling terminal and runs it in | ||
| 1839 | the background as system daemon. This method internally calls L</disengage> and | ||
| 1840 | L</setsid> and attempts to change the working directory to the root directory. | ||
| 1841 | |||
| 1842 | I<Since C<0.06>> | ||
| 1843 | |||
| 1844 | =over 4 | ||
| 1845 | |||
| 1846 | =item daemon example 1 | ||
| 1847 | |||
| 1848 | # given: synopsis; | ||
| 1849 | |||
| 1850 | my $daemon = $parent->daemon; # exits parent immediately | ||
| 1851 | |||
| 1852 | # in forked process ... | ||
| 1853 | |||
| 1854 | # $daemon->exit; | ||
| 1855 | |||
| 1856 | =back | ||
| 1857 | |||
| 1858 | =cut | ||
| 1859 | |||
| 1860 | =head2 data | ||
| 1861 | |||
| 1862 | data(number @pids) (number) | ||
| 1863 | |||
| 1864 | The data method returns the number of messages sent to the current process, | ||
| 1865 | from the PID or PIDs provided (if any). If no PID list is provided, the count | ||
| 1866 | returned is based on the PIDs returned from L</watchlist>. | ||
| 1867 | |||
| 1868 | I<Since C<2.91>> | ||
| 1869 | |||
| 1870 | =over 4 | ||
| 1871 | |||
| 1872 | =item data example 1 | ||
| 1873 | |||
| 1874 | # given: synopsis | ||
| 1875 | |||
| 1876 | package main; | ||
| 1877 | |||
| 1878 | my $data = $parent->data; | ||
| 1879 | |||
| 1880 | # 0 | ||
| 1881 | |||
| 1882 | =back | ||
| 1883 | |||
| 1884 | =over 4 | ||
| 1885 | |||
| 1886 | =item data example 2 | ||
| 1887 | |||
| 1888 | # given: synopsis | ||
| 1889 | |||
| 1890 | package main; | ||
| 1891 | |||
| 1892 | # in process 1 | ||
| 1893 | |||
| 1894 | my $process_1 = Venus::Process->new(12345)->join('procs'); | ||
| 1895 | |||
| 1896 | # in process 2 | ||
| 1897 | |||
| 1898 | my $process_2 = Venus::Process->new(12346)->join('procs'); | ||
| 1899 | |||
| 1900 | # in process 3 | ||
| 1901 | |||
| 1902 | my $process_3 = Venus::Process->new(12347)->join('procs'); | ||
| 1903 | |||
| 1904 | # in process 1 | ||
| 1905 | |||
| 1906 | $process_1->pool(2)->sendall({ | ||
| 1907 | from => $process_1->pid, said => 'hello', | ||
| 1908 | }); | ||
| 1909 | |||
| 1910 | # in process 2 | ||
| 1911 | |||
| 1912 | $process_2->pool(2)->sendall({ | ||
| 1913 | from => $process_2->pid, said => 'hello', | ||
| 1914 | }); | ||
| 1915 | |||
| 1916 | # $process_2->data; | ||
| 1917 | |||
| 1918 | # 2 | ||
| 1919 | |||
| 1920 | # in process 3 | ||
| 1921 | |||
| 1922 | $process_3->pool(2)->sendall({ | ||
| 1923 | from => $process_3->pid, said => 'hello', | ||
| 1924 | }); | ||
| 1925 | |||
| 1926 | # $process_3->data; | ||
| 1927 | |||
| 1928 | # 2 | ||
| 1929 | |||
| 1930 | # in process 1 | ||
| 1931 | |||
| 1932 | my $data = $process_1->data; | ||
| 1933 | |||
| 1934 | # 2 | ||
| 1935 | |||
| 1936 | =back | ||
| 1937 | |||
| 1938 | =over 4 | ||
| 1939 | |||
| 1940 | =item data example 3 | ||
| 1941 | |||
| 1942 | # given: synopsis | ||
| 1943 | |||
| 1944 | package main; | ||
| 1945 | |||
| 1946 | # in process 1 | ||
| 1947 | |||
| 1948 | my $process_1 = Venus::Process->new(12345)->join('procs'); | ||
| 1949 | |||
| 1950 | # in process 2 | ||
| 1951 | |||
| 1952 | my $process_2 = Venus::Process->new(12346)->join('procs'); | ||
| 1953 | |||
| 1954 | # in process 3 | ||
| 1955 | |||
| 1956 | my $process_3 = Venus::Process->new(12347)->join('procs'); | ||
| 1957 | |||
| 1958 | # in process 1 | ||
| 1959 | |||
| 1960 | $process_1->pool(2)->sendall({ | ||
| 1961 | from => $process_1->pid, said => 'hello', | ||
| 1962 | }); | ||
| 1963 | |||
| 1964 | # in process 2 | ||
| 1965 | |||
| 1966 | $process_2->pool(2)->sendall({ | ||
| 1967 | from => $process_2->pid, said => 'hello', | ||
| 1968 | }); | ||
| 1969 | |||
| 1970 | # $process_2->data; | ||
| 1971 | |||
| 1972 | # 2 | ||
| 1973 | |||
| 1974 | # in process 3 | ||
| 1975 | |||
| 1976 | $process_3->pool(2)->sendall({ | ||
| 1977 | from => $process_3->pid, said => 'hello', | ||
| 1978 | }); | ||
| 1979 | |||
| 1980 | # $process_3->data; | ||
| 1981 | |||
| 1982 | # 2 | ||
| 1983 | |||
| 1984 | # in process 1 | ||
| 1985 | |||
| 1986 | $process_1->recvall; | ||
| 1987 | |||
| 1988 | my $data = $process_1->data; | ||
| 1989 | |||
| 1990 | # 0 | ||
| 1991 | |||
| 1992 | =back | ||
| 1993 | |||
| 1994 | =cut | ||
| 1995 | |||
| 1996 | =head2 decode | ||
| 1997 | |||
| 1998 | decode(string $data) (any) | ||
| 1999 | |||
| 2000 | The decode method accepts a string representation of a Perl value and returns | ||
| 2001 | the Perl value. | ||
| 2002 | |||
| 2003 | I<Since C<2.91>> | ||
| 2004 | |||
| 2005 | =over 4 | ||
| 2006 | |||
| 2007 | =item decode example 1 | ||
| 2008 | |||
| 2009 | # given: synopsis | ||
| 2010 | |||
| 2011 | package main; | ||
| 2012 | |||
| 2013 | my $decode = $parent->decode("{ok=>1}"); | ||
| 2014 | |||
| 2015 | # { ok => 1 } | ||
| 2016 | |||
| 2017 | =back | ||
| 2018 | |||
| 2019 | =cut | ||
| 2020 | |||
| 2021 | =head2 disengage | ||
| 2022 | |||
| 2023 | disengage() (Venus::Process) | ||
| 2024 | |||
| 2025 | The disengage method limits the interactivity of the process by changing the | ||
| 2026 | working directory to the root directory and redirecting its standard file | ||
| 2027 | descriptors from and to C</dev/null>, or the OS' equivalent. These state | ||
| 2028 | changes can be undone by calling the L</engage> method. | ||
| 2029 | |||
| 2030 | I<Since C<0.06>> | ||
| 2031 | |||
| 2032 | =over 4 | ||
| 2033 | |||
| 2034 | =item disengage example 1 | ||
| 2035 | |||
| 2036 | # given: synopsis; | ||
| 2037 | |||
| 2038 | $parent = $parent->disengage; | ||
| 2039 | |||
| 2040 | # bless({...}, 'Venus::Process') | ||
| 2041 | |||
| 2042 | =back | ||
| 2043 | |||
| 2044 | =cut | ||
| 2045 | |||
| 2046 | =head2 encode | ||
| 2047 | |||
| 2048 | encode(any $data) (string) | ||
| 2049 | |||
| 2050 | The encode method accepts a Perl value and returns a string representation of | ||
| 2051 | that Perl value. | ||
| 2052 | |||
| 2053 | I<Since C<2.91>> | ||
| 2054 | |||
| 2055 | =over 4 | ||
| 2056 | |||
| 2057 | =item encode example 1 | ||
| 2058 | |||
| 2059 | # given: synopsis | ||
| 2060 | |||
| 2061 | package main; | ||
| 2062 | |||
| 2063 | my $encode = $parent->encode({ok=>1}); | ||
| 2064 | |||
| 2065 | # "{ok=>1}" | ||
| 2066 | |||
| 2067 | =back | ||
| 2068 | |||
| 2069 | =cut | ||
| 2070 | |||
| 2071 | =head2 engage | ||
| 2072 | |||
| 2073 | engage() (Venus::Process) | ||
| 2074 | |||
| 2075 | The engage method ensures the interactivity of the process by changing the | ||
| 2076 | working directory to the directory used to launch the process, and by | ||
| 2077 | redirecting/returning its standard file descriptors from and to their defaults. | ||
| 2078 | This method effectively does the opposite of the L</disengage> method. | ||
| 2079 | |||
| 2080 | I<Since C<0.06>> | ||
| 2081 | |||
| 2082 | =over 4 | ||
| 2083 | |||
| 2084 | =item engage example 1 | ||
| 2085 | |||
| 2086 | # given: synopsis; | ||
| 2087 | |||
| 2088 | $parent = $parent->engage; | ||
| 2089 | |||
| 2090 | # bless({...}, 'Venus::Process') | ||
| 2091 | |||
| 2092 | =back | ||
| 2093 | |||
| 2094 | =cut | ||
| 2095 | |||
| 2096 | =head2 exchange | ||
| 2097 | |||
| 2098 | exchange(string $name) (any) | ||
| 2099 | |||
| 2100 | The exchange method gets and/or sets the name of the data exchange. The | ||
| 2101 | exchange is the ontext in which processes can register and cooperate. Process | ||
| 2102 | can cooperate in different exchanges (or contexts) and messages sent to a | ||
| 2103 | process in one context are not available to be retrieved will operating in | ||
| 2104 | another exchange (or context). | ||
| 2105 | |||
| 2106 | I<Since C<2.91>> | ||
| 2107 | |||
| 2108 | =over 4 | ||
| 2109 | |||
| 2110 | =item exchange example 1 | ||
| 2111 | |||
| 2112 | # given: synopsis | ||
| 2113 | |||
| 2114 | package main; | ||
| 2115 | |||
| 2116 | my $exchange = $parent->exchange; | ||
| 2117 | |||
| 2118 | # undef | ||
| 2119 | |||
| 2120 | =back | ||
| 2121 | |||
| 2122 | =over 4 | ||
| 2123 | |||
| 2124 | =item exchange example 2 | ||
| 2125 | |||
| 2126 | # given: synopsis | ||
| 2127 | |||
| 2128 | package main; | ||
| 2129 | |||
| 2130 | my $exchange = $parent->exchange('procs'); | ||
| 2131 | |||
| 2132 | # "procs" | ||
| 2133 | |||
| 2134 | =back | ||
| 2135 | |||
| 2136 | =over 4 | ||
| 2137 | |||
| 2138 | =item exchange example 3 | ||
| 2139 | |||
| 2140 | # given: synopsis | ||
| 2141 | |||
| 2142 | package main; | ||
| 2143 | |||
| 2144 | my $exchange = $parent->exchange('procs'); | ||
| 2145 | |||
| 2146 | # "procs" | ||
| 2147 | |||
| 2148 | $exchange = $parent->exchange; | ||
| 2149 | |||
| 2150 | # "procs" | ||
| 2151 | |||
| 2152 | =back | ||
| 2153 | |||
| 2154 | =cut | ||
| 2155 | |||
| 2156 | =head2 exit | ||
| 2157 | |||
| 2158 | exit(number $status) (number) | ||
| 2159 | |||
| 2160 | The exit method exits the program immediately. | ||
| 2161 | |||
| 2162 | I<Since C<0.06>> | ||
| 2163 | |||
| 2164 | =over 4 | ||
| 2165 | |||
| 2166 | =item exit example 1 | ||
| 2167 | |||
| 2168 | # given: synopsis; | ||
| 2169 | |||
| 2170 | my $exit = $parent->exit; | ||
| 2171 | |||
| 2172 | # 0 | ||
| 2173 | |||
| 2174 | =back | ||
| 2175 | |||
| 2176 | =over 4 | ||
| 2177 | |||
| 2178 | =item exit example 2 | ||
| 2179 | |||
| 2180 | # given: synopsis; | ||
| 2181 | |||
| 2182 | my $exit = $parent->exit(1); | ||
| 2183 | |||
| 2184 | # 1 | ||
| 2185 | |||
| 2186 | =back | ||
| 2187 | |||
| 2188 | =cut | ||
| 2189 | |||
| 2190 | =head2 followers | ||
| 2191 | |||
| 2192 | followers() (arrayref) | ||
| 2193 | |||
| 2194 | The followers method returns the list of PIDs registered under the current | ||
| 2195 | L</exchange> who are not the L</leader>. | ||
| 2196 | |||
| 2197 | I<Since C<2.91>> | ||
| 2198 | |||
| 2199 | =over 4 | ||
| 2200 | |||
| 2201 | =item followers example 1 | ||
| 2202 | |||
| 2203 | # given: synopsis | ||
| 2204 | |||
| 2205 | package main; | ||
| 2206 | |||
| 2207 | my $followers = $parent->followers; | ||
| 2208 | |||
| 2209 | # [] | ||
| 2210 | |||
| 2211 | =back | ||
| 2212 | |||
| 2213 | =over 4 | ||
| 2214 | |||
| 2215 | =item followers example 2 | ||
| 2216 | |||
| 2217 | # given: synopsis | ||
| 2218 | |||
| 2219 | package main; | ||
| 2220 | |||
| 2221 | # in process 1 | ||
| 2222 | |||
| 2223 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 2224 | |||
| 2225 | # in process 2 | ||
| 2226 | |||
| 2227 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 2228 | |||
| 2229 | # in process 3 | ||
| 2230 | |||
| 2231 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 2232 | |||
| 2233 | # in process 1 | ||
| 2234 | |||
| 2235 | my $followers = $process_1->followers; | ||
| 2236 | |||
| 2237 | # [12346, 12347] | ||
| 2238 | |||
| 2239 | =back | ||
| 2240 | |||
| 2241 | =cut | ||
| 2242 | |||
| 2243 | =head2 fork | ||
| 2244 | |||
| 2245 | fork(string | coderef $code, any @args) (Venus::Process, number) | ||
| 2246 | |||
| 2247 | The fork method calls the system L<perlfunc/fork> function and creates a new | ||
| 2248 | process running the same program at the same point (or call site). This method | ||
| 2249 | returns a new L<Venus::Process> object representing the child process (from | ||
| 2250 | within the execution of the child process (or fork)), and returns C<undef> to | ||
| 2251 | the parent (or originating) process. In list context, this method returns both | ||
| 2252 | the process and I<PID> (or process ID) of the child process. If a callback or | ||
| 2253 | argument is provided it will be executed in the child process. | ||
| 2254 | |||
| 2255 | I<Since C<0.06>> | ||
| 2256 | |||
| 2257 | =over 4 | ||
| 2258 | |||
| 2259 | =item fork example 1 | ||
| 2260 | |||
| 2261 | # given: synopsis; | ||
| 2262 | |||
| 2263 | $process = $parent->fork; | ||
| 2264 | |||
| 2265 | # if ($process) { | ||
| 2266 | # # in forked process ... | ||
| 2267 | # $process->exit; | ||
| 2268 | # } | ||
| 2269 | # else { | ||
| 2270 | # # in parent process ... | ||
| 2271 | # $parent->wait(-1); | ||
| 2272 | # } | ||
| 2273 | |||
| 2274 | # in child process | ||
| 2275 | |||
| 2276 | # bless({...}, 'Venus::Process') | ||
| 2277 | |||
| 2278 | =back | ||
| 2279 | |||
| 2280 | =over 4 | ||
| 2281 | |||
| 2282 | =item fork example 2 | ||
| 2283 | |||
| 2284 | # given: synopsis; | ||
| 2285 | |||
| 2286 | my $pid; | ||
| 2287 | |||
| 2288 | ($process, $pid) = $parent->fork; | ||
| 2289 | |||
| 2290 | # if ($process) { | ||
| 2291 | # # in forked process ... | ||
| 2292 | # $process->exit; | ||
| 2293 | # } | ||
| 2294 | # else { | ||
| 2295 | # # in parent process ... | ||
| 2296 | # $parent->wait($pid); | ||
| 2297 | # } | ||
| 2298 | |||
| 2299 | # in parent process | ||
| 2300 | |||
| 2301 | # (undef, $pid) | ||
| 2302 | |||
| 2303 | =back | ||
| 2304 | |||
| 2305 | =over 4 | ||
| 2306 | |||
| 2307 | =item fork example 3 | ||
| 2308 | |||
| 2309 | # given: synopsis; | ||
| 2310 | |||
| 2311 | my $pid; | ||
| 2312 | |||
| 2313 | ($process, $pid) = $parent->fork(sub{ | ||
| 2314 | $$_{started} = time; | ||
| 2315 | }); | ||
| 2316 | |||
| 2317 | # if ($process) { | ||
| 2318 | # # in forked process ... | ||
| 2319 | # $process->exit; | ||
| 2320 | # } | ||
| 2321 | # else { | ||
| 2322 | # # in parent process ... | ||
| 2323 | # $parent->wait($pid); | ||
| 2324 | # } | ||
| 2325 | |||
| 2326 | # in parent process | ||
| 2327 | |||
| 2328 | # (undef, $pid) | ||
| 2329 | |||
| 2330 | =back | ||
| 2331 | |||
| 2332 | =over 4 | ||
| 2333 | |||
| 2334 | =item fork example 4 | ||
| 2335 | |||
| 2336 | # given: synopsis; | ||
| 2337 | |||
| 2338 | $process = $parent->fork(sub{}); | ||
| 2339 | |||
| 2340 | # simulate fork failure | ||
| 2341 | |||
| 2342 | # no forking attempted if NOT supported | ||
| 2343 | |||
| 2344 | # Exception! (isa Venus::Process:Error) (see error_on_fork_support) | ||
| 2345 | |||
| 2346 | =back | ||
| 2347 | |||
| 2348 | =over 4 | ||
| 2349 | |||
| 2350 | =item fork example 5 | ||
| 2351 | |||
| 2352 | # given: synopsis | ||
| 2353 | |||
| 2354 | $process = $parent->do('alarm', 10)->fork; | ||
| 2355 | |||
| 2356 | # if ($process) { | ||
| 2357 | # # in forked process with alarm installed ... | ||
| 2358 | # $process->exit; | ||
| 2359 | # } | ||
| 2360 | # else { | ||
| 2361 | # # in parent process ... | ||
| 2362 | # $parent->wait(-1); | ||
| 2363 | # } | ||
| 2364 | |||
| 2365 | # in child process | ||
| 2366 | |||
| 2367 | # bless({...}, 'Venus::Process') | ||
| 2368 | |||
| 2369 | =back | ||
| 2370 | |||
| 2371 | =cut | ||
| 2372 | |||
| 2373 | =head2 forks | ||
| 2374 | |||
| 2375 | forks(string | coderef $code, any @args) (Venus::Process, within[arrayref, number]) | ||
| 2376 | |||
| 2377 | The forks method creates multiple forks by calling the L</fork> method C<n> | ||
| 2378 | times, based on the count specified. As with the L</fork> method, this method | ||
| 2379 | returns a new L<Venus::Process> object representing the child process (from | ||
| 2380 | within the execution of the child process (or fork)), and returns C<undef> to | ||
| 2381 | the parent (or originating) process. In list context, this method returns both | ||
| 2382 | the process and an arrayref of I<PID> values (or process IDs) for each of the | ||
| 2383 | child processes created. If a callback or argument is provided it will be | ||
| 2384 | executed in each child process. | ||
| 2385 | |||
| 2386 | I<Since C<0.06>> | ||
| 2387 | |||
| 2388 | =over 4 | ||
| 2389 | |||
| 2390 | =item forks example 1 | ||
| 2391 | |||
| 2392 | # given: synopsis; | ||
| 2393 | |||
| 2394 | $process = $parent->forks(5); | ||
| 2395 | |||
| 2396 | # if ($process) { | ||
| 2397 | # # do something in (each) forked process ... | ||
| 2398 | # $process->exit; | ||
| 2399 | # } | ||
| 2400 | # else { | ||
| 2401 | # # do something in parent process ... | ||
| 2402 | # $parent->wait(-1); | ||
| 2403 | # } | ||
| 2404 | |||
| 2405 | # bless({...}, 'Venus::Process') | ||
| 2406 | |||
| 2407 | =back | ||
| 2408 | |||
| 2409 | =over 4 | ||
| 2410 | |||
| 2411 | =item forks example 2 | ||
| 2412 | |||
| 2413 | # given: synopsis; | ||
| 2414 | |||
| 2415 | my $pids; | ||
| 2416 | |||
| 2417 | ($process, $pids) = $parent->forks(5); | ||
| 2418 | |||
| 2419 | # if ($process) { | ||
| 2420 | # # do something in (each) forked process ... | ||
| 2421 | # $process->exit; | ||
| 2422 | # } | ||
| 2423 | # else { | ||
| 2424 | # # do something in parent process ... | ||
| 2425 | # $parent->wait($_) for @$pids; | ||
| 2426 | # } | ||
| 2427 | |||
| 2428 | # in parent process | ||
| 2429 | |||
| 2430 | # (undef, $pids) | ||
| 2431 | |||
| 2432 | =back | ||
| 2433 | |||
| 2434 | =over 4 | ||
| 2435 | |||
| 2436 | =item forks example 3 | ||
| 2437 | |||
| 2438 | # given: synopsis; | ||
| 2439 | |||
| 2440 | my $pids; | ||
| 2441 | |||
| 2442 | ($process, $pids) = $parent->forks(5, sub{ | ||
| 2443 | my ($fork, $pid, $iteration) = @_; | ||
| 2444 | # $iteration is the fork iteration index | ||
| 2445 | $fork->exit; | ||
| 2446 | }); | ||
| 2447 | |||
| 2448 | # if ($process) { | ||
| 2449 | # # do something in (each) forked process ... | ||
| 2450 | # $process->exit; | ||
| 2451 | # } | ||
| 2452 | # else { | ||
| 2453 | # # do something in parent process ... | ||
| 2454 | # $parent->wait($_) for @$pids; | ||
| 2455 | # } | ||
| 2456 | |||
| 2457 | # in child process | ||
| 2458 | |||
| 2459 | # bless({...}, 'Venus::Process') | ||
| 2460 | |||
| 2461 | =back | ||
| 2462 | |||
| 2463 | =cut | ||
| 2464 | |||
| 2465 | =head2 future | ||
| 2466 | |||
| 2467 | future(coderef $code, any @args) (Venus::Future) | ||
| 2468 | |||
| 2469 | The future method creates a new object via L</async> which runs the callback | ||
| 2470 | asynchronously and returns a L<Venus::Future> object with a promise which | ||
| 2471 | eventually resolves to the value emitted or error raised. | ||
| 2472 | |||
| 2473 | I<Since C<3.55>> | ||
| 2474 | |||
| 2475 | =over 4 | ||
| 2476 | |||
| 2477 | =item future example 1 | ||
| 2478 | |||
| 2479 | # given: synopsis; | ||
| 2480 | |||
| 2481 | my $future = $parent->future(sub{ | ||
| 2482 | my ($process) = @_; | ||
| 2483 | # in forked process ... | ||
| 2484 | $process->exit; | ||
| 2485 | }); | ||
| 2486 | |||
| 2487 | # bless({...}, 'Venus::Future') | ||
| 2488 | |||
| 2489 | =back | ||
| 2490 | |||
| 2491 | =over 4 | ||
| 2492 | |||
| 2493 | =item future example 2 | ||
| 2494 | |||
| 2495 | # given: synopsis; | ||
| 2496 | |||
| 2497 | my $future = $parent->future(sub{ | ||
| 2498 | ($process) = @_; | ||
| 2499 | # in forked process ... | ||
| 2500 | return 'done'; | ||
| 2501 | }); | ||
| 2502 | |||
| 2503 | # $future->fulfill; | ||
| 2504 | |||
| 2505 | # true | ||
| 2506 | |||
| 2507 | # $future->value; | ||
| 2508 | |||
| 2509 | # 'done' | ||
| 2510 | |||
| 2511 | =back | ||
| 2512 | |||
| 2513 | =over 4 | ||
| 2514 | |||
| 2515 | =item future example 3 | ||
| 2516 | |||
| 2517 | # given: synopsis; | ||
| 2518 | |||
| 2519 | my $future = $parent->future(sub{ | ||
| 2520 | ($process) = @_; | ||
| 2521 | # in forked process ... | ||
| 2522 | return {status => 'done'}; | ||
| 2523 | }); | ||
| 2524 | |||
| 2525 | # $future->fulfill; | ||
| 2526 | |||
| 2527 | # true | ||
| 2528 | |||
| 2529 | # $future->value | ||
| 2530 | |||
| 2531 | # {status => 'done'} | ||
| 2532 | |||
| 2533 | =back | ||
| 2534 | |||
| 2535 | =over 4 | ||
| 2536 | |||
| 2537 | =item future example 4 | ||
| 2538 | |||
| 2539 | # given: synopsis; | ||
| 2540 | |||
| 2541 | my $future = $parent->future(sub{ | ||
| 2542 | ($process) = @_; | ||
| 2543 | # in forked process ... | ||
| 2544 | return ['done']; | ||
| 2545 | }); | ||
| 2546 | |||
| 2547 | # $future->fulfill; | ||
| 2548 | |||
| 2549 | # true | ||
| 2550 | |||
| 2551 | # my ($await) = $future->value; | ||
| 2552 | |||
| 2553 | # ['done'] | ||
| 2554 | |||
| 2555 | =back | ||
| 2556 | |||
| 2557 | =over 4 | ||
| 2558 | |||
| 2559 | =item future example 5 | ||
| 2560 | |||
| 2561 | # given: synopsis; | ||
| 2562 | |||
| 2563 | my $future = $parent->future(sub{ | ||
| 2564 | ($process) = @_; | ||
| 2565 | # in forked process ... | ||
| 2566 | $process->sendall(['send 1', 'send 2', 'send 3']); | ||
| 2567 | $process->sendall(['send 4']); | ||
| 2568 | $process->sendall(['send 5']); | ||
| 2569 | return; | ||
| 2570 | }); | ||
| 2571 | |||
| 2572 | # $future->fulfill; | ||
| 2573 | |||
| 2574 | # true | ||
| 2575 | |||
| 2576 | # my ($await) = $future->value; | ||
| 2577 | |||
| 2578 | # ['send 1', 'send 2', 'send 3'] | ||
| 2579 | |||
| 2580 | =back | ||
| 2581 | |||
| 2582 | =cut | ||
| 2583 | |||
| 2584 | =head2 is_dyadic | ||
| 2585 | |||
| 2586 | is_dyadic() (boolean) | ||
| 2587 | |||
| 2588 | The is_dyadic method returns true is the process is configured to exclusively | ||
| 2589 | communicate with one other process, otherwise returns false. | ||
| 2590 | |||
| 2591 | I<Since C<3.40>> | ||
| 2592 | |||
| 2593 | =over 4 | ||
| 2594 | |||
| 2595 | =item is_dyadic example 1 | ||
| 2596 | |||
| 2597 | package main; | ||
| 2598 | |||
| 2599 | use Venus::Process; | ||
| 2600 | |||
| 2601 | my $process = Venus::Process->new; | ||
| 2602 | |||
| 2603 | my $is_dyadic = $process->is_dyadic; | ||
| 2604 | |||
| 2605 | # false | ||
| 2606 | |||
| 2607 | =back | ||
| 2608 | |||
| 2609 | =over 4 | ||
| 2610 | |||
| 2611 | =item is_dyadic example 2 | ||
| 2612 | |||
| 2613 | package main; | ||
| 2614 | |||
| 2615 | use Venus::Process; | ||
| 2616 | |||
| 2617 | my $process = Venus::Process->new->async(sub{ | ||
| 2618 | return 'done'; | ||
| 2619 | }); | ||
| 2620 | |||
| 2621 | my $is_dyadic = $process->is_dyadic; | ||
| 2622 | |||
| 2623 | # true | ||
| 2624 | |||
| 2625 | =back | ||
| 2626 | |||
| 2627 | =cut | ||
| 2628 | |||
| 2629 | =head2 is_follower | ||
| 2630 | |||
| 2631 | is_follower() (boolean) | ||
| 2632 | |||
| 2633 | The is_follower method returns true if the process is not the L</leader>, otherwise | ||
| 2634 | returns false. | ||
| 2635 | |||
| 2636 | I<Since C<2.91>> | ||
| 2637 | |||
| 2638 | =over 4 | ||
| 2639 | |||
| 2640 | =item is_follower example 1 | ||
| 2641 | |||
| 2642 | package main; | ||
| 2643 | |||
| 2644 | use Venus::Process; | ||
| 2645 | |||
| 2646 | my $process = Venus::Process->new; | ||
| 2647 | |||
| 2648 | my $is_follower = $process->is_follower; | ||
| 2649 | |||
| 2650 | # false | ||
| 2651 | |||
| 2652 | =back | ||
| 2653 | |||
| 2654 | =over 4 | ||
| 2655 | |||
| 2656 | =item is_follower example 2 | ||
| 2657 | |||
| 2658 | package main; | ||
| 2659 | |||
| 2660 | use Venus::Process; | ||
| 2661 | |||
| 2662 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 2663 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 2664 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 2665 | |||
| 2666 | my $is_follower = $process_1->is_follower; | ||
| 2667 | |||
| 2668 | # false | ||
| 2669 | |||
| 2670 | # my $is_follower = $process_2->is_follower; | ||
| 2671 | |||
| 2672 | # true | ||
| 2673 | |||
| 2674 | # my $is_follower = $process_3->is_follower; | ||
| 2675 | |||
| 2676 | # true | ||
| 2677 | |||
| 2678 | =back | ||
| 2679 | |||
| 2680 | =over 4 | ||
| 2681 | |||
| 2682 | =item is_follower example 3 | ||
| 2683 | |||
| 2684 | package main; | ||
| 2685 | |||
| 2686 | use Venus::Process; | ||
| 2687 | |||
| 2688 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 2689 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 2690 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 2691 | |||
| 2692 | # my $is_follower = $process_1->is_follower; | ||
| 2693 | |||
| 2694 | # false | ||
| 2695 | |||
| 2696 | my $is_follower = $process_2->is_follower; | ||
| 2697 | |||
| 2698 | # true | ||
| 2699 | |||
| 2700 | # my $is_follower = $process_3->is_follower; | ||
| 2701 | |||
| 2702 | # true | ||
| 2703 | |||
| 2704 | =back | ||
| 2705 | |||
| 2706 | =cut | ||
| 2707 | |||
| 2708 | =head2 is_leader | ||
| 2709 | |||
| 2710 | is_leader() (boolean) | ||
| 2711 | |||
| 2712 | The is_leader method returns true if the process is the L</leader>, otherwise | ||
| 2713 | returns false. | ||
| 2714 | |||
| 2715 | I<Since C<2.91>> | ||
| 2716 | |||
| 2717 | =over 4 | ||
| 2718 | |||
| 2719 | =item is_leader example 1 | ||
| 2720 | |||
| 2721 | package main; | ||
| 2722 | |||
| 2723 | use Venus::Process; | ||
| 2724 | |||
| 2725 | my $process = Venus::Process->new; | ||
| 2726 | |||
| 2727 | my $is_leader = $process->is_leader; | ||
| 2728 | |||
| 2729 | # true | ||
| 2730 | |||
| 2731 | =back | ||
| 2732 | |||
| 2733 | =over 4 | ||
| 2734 | |||
| 2735 | =item is_leader example 2 | ||
| 2736 | |||
| 2737 | package main; | ||
| 2738 | |||
| 2739 | use Venus::Process; | ||
| 2740 | |||
| 2741 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 2742 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 2743 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 2744 | |||
| 2745 | my $is_leader = $process_1->is_leader; | ||
| 2746 | |||
| 2747 | # true | ||
| 2748 | |||
| 2749 | # my $is_leader = $process_2->is_leader; | ||
| 2750 | |||
| 2751 | # false | ||
| 2752 | |||
| 2753 | # my $is_leader = $process_3->is_leader; | ||
| 2754 | |||
| 2755 | # false | ||
| 2756 | |||
| 2757 | =back | ||
| 2758 | |||
| 2759 | =over 4 | ||
| 2760 | |||
| 2761 | =item is_leader example 3 | ||
| 2762 | |||
| 2763 | package main; | ||
| 2764 | |||
| 2765 | use Venus::Process; | ||
| 2766 | |||
| 2767 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 2768 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 2769 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 2770 | |||
| 2771 | # my $is_leader = $process_1->is_leader; | ||
| 2772 | |||
| 2773 | # true | ||
| 2774 | |||
| 2775 | my $is_leader = $process_2->is_leader; | ||
| 2776 | |||
| 2777 | # false | ||
| 2778 | |||
| 2779 | # my $is_leader = $process_3->is_leader; | ||
| 2780 | |||
| 2781 | # false | ||
| 2782 | |||
| 2783 | =back | ||
| 2784 | |||
| 2785 | =cut | ||
| 2786 | |||
| 2787 | =head2 is_registered | ||
| 2788 | |||
| 2789 | is_registered() (boolean) | ||
| 2790 | |||
| 2791 | The is_registered method returns true if the process has registered using the | ||
| 2792 | L</register> method, otherwise returns false. | ||
| 2793 | |||
| 2794 | I<Since C<2.91>> | ||
| 2795 | |||
| 2796 | =over 4 | ||
| 2797 | |||
| 2798 | =item is_registered example 1 | ||
| 2799 | |||
| 2800 | package main; | ||
| 2801 | |||
| 2802 | use Venus::Process; | ||
| 2803 | |||
| 2804 | my $process = Venus::Process->new; | ||
| 2805 | |||
| 2806 | my $is_registered = $process->is_registered; | ||
| 2807 | |||
| 2808 | # false | ||
| 2809 | |||
| 2810 | =back | ||
| 2811 | |||
| 2812 | =over 4 | ||
| 2813 | |||
| 2814 | =item is_registered example 2 | ||
| 2815 | |||
| 2816 | package main; | ||
| 2817 | |||
| 2818 | use Venus::Process; | ||
| 2819 | |||
| 2820 | my $process = Venus::Process->new(12345)->register; | ||
| 2821 | |||
| 2822 | my $is_registered = $process->is_registered; | ||
| 2823 | |||
| 2824 | # true | ||
| 2825 | |||
| 2826 | =back | ||
| 2827 | |||
| 2828 | =cut | ||
| 2829 | |||
| 2830 | =head2 is_unregistered | ||
| 2831 | |||
| 2832 | is_unregistered() (boolean) | ||
| 2833 | |||
| 2834 | The is_unregistered method returns true if the process has unregistered using | ||
| 2835 | the L</unregister> method, or had never registered at all, otherwise returns | ||
| 2836 | false. | ||
| 2837 | |||
| 2838 | I<Since C<2.91>> | ||
| 2839 | |||
| 2840 | =over 4 | ||
| 2841 | |||
| 2842 | =item is_unregistered example 1 | ||
| 2843 | |||
| 2844 | package main; | ||
| 2845 | |||
| 2846 | use Venus::Process; | ||
| 2847 | |||
| 2848 | my $process = Venus::Process->new; | ||
| 2849 | |||
| 2850 | my $is_unregistered = $process->is_unregistered; | ||
| 2851 | |||
| 2852 | # true | ||
| 2853 | |||
| 2854 | =back | ||
| 2855 | |||
| 2856 | =over 4 | ||
| 2857 | |||
| 2858 | =item is_unregistered example 2 | ||
| 2859 | |||
| 2860 | package main; | ||
| 2861 | |||
| 2862 | use Venus::Process; | ||
| 2863 | |||
| 2864 | my $process = Venus::Process->new(12345); | ||
| 2865 | |||
| 2866 | my $is_unregistered = $process->is_unregistered; | ||
| 2867 | |||
| 2868 | # true | ||
| 2869 | |||
| 2870 | =back | ||
| 2871 | |||
| 2872 | =over 4 | ||
| 2873 | |||
| 2874 | =item is_unregistered example 3 | ||
| 2875 | |||
| 2876 | package main; | ||
| 2877 | |||
| 2878 | use Venus::Process; | ||
| 2879 | |||
| 2880 | my $process = Venus::Process->new(12345)->register; | ||
| 2881 | |||
| 2882 | my $is_unregistered = $process->is_unregistered; | ||
| 2883 | |||
| 2884 | # false | ||
| 2885 | |||
| 2886 | =back | ||
| 2887 | |||
| 2888 | =cut | ||
| 2889 | |||
| 2890 | =head2 join | ||
| 2891 | |||
| 2892 | join(string $name) (Venus::Process) | ||
| 2893 | |||
| 2894 | The join method sets the L</exchange>, registers the process with the exchange | ||
| 2895 | using L</register>, and clears the L</watchlist>, then returns the invocant. | ||
| 2896 | |||
| 2897 | I<Since C<2.91>> | ||
| 2898 | |||
| 2899 | =over 4 | ||
| 2900 | |||
| 2901 | =item join example 1 | ||
| 2902 | |||
| 2903 | package main; | ||
| 2904 | |||
| 2905 | use Venus::Process; | ||
| 2906 | |||
| 2907 | my $process = Venus::Process->new; | ||
| 2908 | |||
| 2909 | $process = $process->join; | ||
| 2910 | |||
| 2911 | # bless({...}, 'Venus::Process') | ||
| 2912 | |||
| 2913 | =back | ||
| 2914 | |||
| 2915 | =over 4 | ||
| 2916 | |||
| 2917 | =item join example 2 | ||
| 2918 | |||
| 2919 | package main; | ||
| 2920 | |||
| 2921 | use Venus::Process; | ||
| 2922 | |||
| 2923 | my $process = Venus::Process->new; | ||
| 2924 | |||
| 2925 | $process = $process->join('procs'); | ||
| 2926 | |||
| 2927 | # bless({...}, 'Venus::Process') | ||
| 2928 | |||
| 2929 | =back | ||
| 2930 | |||
| 2931 | =cut | ||
| 2932 | |||
| 2933 | =head2 kill | ||
| 2934 | |||
| 2935 | kill(string $signal, number @pids) (number) | ||
| 2936 | |||
| 2937 | The kill method calls the system L<perlfunc/kill> function which sends a signal | ||
| 2938 | to a list of processes and returns truthy or falsy. B<Note:> A truthy result | ||
| 2939 | doesn't necessarily mean all processes were successfully signalled. | ||
| 2940 | |||
| 2941 | I<Since C<0.06>> | ||
| 2942 | |||
| 2943 | =over 4 | ||
| 2944 | |||
| 2945 | =item kill example 1 | ||
| 2946 | |||
| 2947 | # given: synopsis; | ||
| 2948 | |||
| 2949 | if ($process = $parent->fork) { | ||
| 2950 | # in forked process ... | ||
| 2951 | $process->exit; | ||
| 2952 | } | ||
| 2953 | |||
| 2954 | my $kill = $parent->kill('term', int $process); | ||
| 2955 | |||
| 2956 | # 1 | ||
| 2957 | |||
| 2958 | =back | ||
| 2959 | |||
| 2960 | =cut | ||
| 2961 | |||
| 2962 | =head2 killall | ||
| 2963 | |||
| 2964 | killall(string $name, number @pids) (arrayref) | ||
| 2965 | |||
| 2966 | The killall method accepts a list of PIDs (or uses the L</watchlist> if not | ||
| 2967 | provided) and returns the result of calling the L</kill> method for each PID. | ||
| 2968 | Returns a list in list context. | ||
| 2969 | |||
| 2970 | I<Since C<2.40>> | ||
| 2971 | |||
| 2972 | =over 4 | ||
| 2973 | |||
| 2974 | =item killall example 1 | ||
| 2975 | |||
| 2976 | # given: synopsis | ||
| 2977 | |||
| 2978 | package main; | ||
| 2979 | |||
| 2980 | if ($process = $parent->fork) { | ||
| 2981 | # in forked process ... | ||
| 2982 | $process->exit; | ||
| 2983 | } | ||
| 2984 | |||
| 2985 | my $killall = $parent->killall('term'); | ||
| 2986 | |||
| 2987 | # [1] | ||
| 2988 | |||
| 2989 | =back | ||
| 2990 | |||
| 2991 | =over 4 | ||
| 2992 | |||
| 2993 | =item killall example 2 | ||
| 2994 | |||
| 2995 | # given: synopsis | ||
| 2996 | |||
| 2997 | package main; | ||
| 2998 | |||
| 2999 | if ($process = $parent->fork) { | ||
| 3000 | # in forked process ... | ||
| 3001 | $process->exit; | ||
| 3002 | } | ||
| 3003 | |||
| 3004 | my $killall = $parent->killall('term', 1001..1004); | ||
| 3005 | |||
| 3006 | # [1, 1, 1, 1] | ||
| 3007 | |||
| 3008 | =back | ||
| 3009 | |||
| 3010 | =cut | ||
| 3011 | |||
| 3012 | =head2 leader | ||
| 3013 | |||
| 3014 | leader() (number) | ||
| 3015 | |||
| 3016 | The leader method uses a simple leader election algorithm to determine the | ||
| 3017 | process leader and returns the PID for that process. The leader is always the | ||
| 3018 | lowest value active PID (i.e. that responds to L</ping>). | ||
| 3019 | |||
| 3020 | I<Since C<2.91>> | ||
| 3021 | |||
| 3022 | =over 4 | ||
| 3023 | |||
| 3024 | =item leader example 1 | ||
| 3025 | |||
| 3026 | # given: synopsis | ||
| 3027 | |||
| 3028 | package main; | ||
| 3029 | |||
| 3030 | my $leader = $parent->leader; | ||
| 3031 | |||
| 3032 | # 12345 | ||
| 3033 | |||
| 3034 | =back | ||
| 3035 | |||
| 3036 | =over 4 | ||
| 3037 | |||
| 3038 | =item leader example 2 | ||
| 3039 | |||
| 3040 | # given: synopsis | ||
| 3041 | |||
| 3042 | package main; | ||
| 3043 | |||
| 3044 | # in process 1 | ||
| 3045 | |||
| 3046 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3047 | |||
| 3048 | # in process 2 | ||
| 3049 | |||
| 3050 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3051 | |||
| 3052 | # in process 3 | ||
| 3053 | |||
| 3054 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 3055 | |||
| 3056 | # in process 1 | ||
| 3057 | |||
| 3058 | my $leader = $process_3->leader; | ||
| 3059 | |||
| 3060 | # 12345 | ||
| 3061 | |||
| 3062 | =back | ||
| 3063 | |||
| 3064 | =over 4 | ||
| 3065 | |||
| 3066 | =item leader example 3 | ||
| 3067 | |||
| 3068 | # given: synopsis | ||
| 3069 | |||
| 3070 | package main; | ||
| 3071 | |||
| 3072 | my $leader = $parent->register->leader; | ||
| 3073 | |||
| 3074 | # 12345 | ||
| 3075 | |||
| 3076 | =back | ||
| 3077 | |||
| 3078 | =cut | ||
| 3079 | |||
| 3080 | =head2 leave | ||
| 3081 | |||
| 3082 | leave(string $name) (Venus::Process) | ||
| 3083 | |||
| 3084 | The leave method sets the L</exchange> to undefined, unregisters the process | ||
| 3085 | using L</unregister>, and clears the L</watchlist>, then returns the invocant. | ||
| 3086 | |||
| 3087 | I<Since C<2.91>> | ||
| 3088 | |||
| 3089 | =over 4 | ||
| 3090 | |||
| 3091 | =item leave example 1 | ||
| 3092 | |||
| 3093 | package main; | ||
| 3094 | |||
| 3095 | use Venus::Process; | ||
| 3096 | |||
| 3097 | my $process = Venus::Process->new; | ||
| 3098 | |||
| 3099 | $process = $process->leave; | ||
| 3100 | |||
| 3101 | # bless({...}, 'Venus::Process') | ||
| 3102 | |||
| 3103 | =back | ||
| 3104 | |||
| 3105 | =over 4 | ||
| 3106 | |||
| 3107 | =item leave example 2 | ||
| 3108 | |||
| 3109 | package main; | ||
| 3110 | |||
| 3111 | use Venus::Process; | ||
| 3112 | |||
| 3113 | my $process = Venus::Process->new; | ||
| 3114 | |||
| 3115 | $process = $process->leave('procs'); | ||
| 3116 | |||
| 3117 | # bless({...}, 'Venus::Process') | ||
| 3118 | |||
| 3119 | =back | ||
| 3120 | |||
| 3121 | =cut | ||
| 3122 | |||
| 3123 | =head2 limit | ||
| 3124 | |||
| 3125 | limit(number $count) (boolean) | ||
| 3126 | |||
| 3127 | The limit method blocks the execution of the current process until the number | ||
| 3128 | of processes in the L</watchlist> falls bellow the count specified. The method | ||
| 3129 | returns true once execution continues if execution was blocked, and false if | ||
| 3130 | the limit has yet to be reached. | ||
| 3131 | |||
| 3132 | I<Since C<3.40>> | ||
| 3133 | |||
| 3134 | =over 4 | ||
| 3135 | |||
| 3136 | =item limit example 1 | ||
| 3137 | |||
| 3138 | package main; | ||
| 3139 | |||
| 3140 | use Venus::Process; | ||
| 3141 | |||
| 3142 | my $parent = Venus::Process->new; | ||
| 3143 | |||
| 3144 | $parent->work(sub { | ||
| 3145 | my ($process) = @_; | ||
| 3146 | # in forked process ... | ||
| 3147 | $process->exit; | ||
| 3148 | }); | ||
| 3149 | |||
| 3150 | my $limit = $parent->limit(2); | ||
| 3151 | |||
| 3152 | # false | ||
| 3153 | |||
| 3154 | =back | ||
| 3155 | |||
| 3156 | =over 4 | ||
| 3157 | |||
| 3158 | =item limit example 2 | ||
| 3159 | |||
| 3160 | package main; | ||
| 3161 | |||
| 3162 | use Venus::Process; | ||
| 3163 | |||
| 3164 | my $parent = Venus::Process->new; | ||
| 3165 | |||
| 3166 | $parent->works(2, sub { | ||
| 3167 | my ($process) = @_; | ||
| 3168 | # in forked process ... | ||
| 3169 | $process->exit; | ||
| 3170 | }); | ||
| 3171 | |||
| 3172 | my $limit = $parent->limit(2); | ||
| 3173 | |||
| 3174 | # true | ||
| 3175 | |||
| 3176 | =back | ||
| 3177 | |||
| 3178 | =cut | ||
| 3179 | |||
| 3180 | =head2 others | ||
| 3181 | |||
| 3182 | others() (arrayref) | ||
| 3183 | |||
| 3184 | The others method returns all L</registrants> other than the current process, | ||
| 3185 | i.e. all other registered process PIDs whether active or inactive. | ||
| 3186 | |||
| 3187 | I<Since C<2.91>> | ||
| 3188 | |||
| 3189 | =over 4 | ||
| 3190 | |||
| 3191 | =item others example 1 | ||
| 3192 | |||
| 3193 | # given: synopsis | ||
| 3194 | |||
| 3195 | package main; | ||
| 3196 | |||
| 3197 | my $others = $parent->others; | ||
| 3198 | |||
| 3199 | # [] | ||
| 3200 | |||
| 3201 | =back | ||
| 3202 | |||
| 3203 | =over 4 | ||
| 3204 | |||
| 3205 | =item others example 2 | ||
| 3206 | |||
| 3207 | # given: synopsis | ||
| 3208 | |||
| 3209 | package main; | ||
| 3210 | |||
| 3211 | # in process 1 | ||
| 3212 | |||
| 3213 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3214 | |||
| 3215 | # in process 2 | ||
| 3216 | |||
| 3217 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3218 | |||
| 3219 | # in process 3 | ||
| 3220 | |||
| 3221 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 3222 | |||
| 3223 | # in process 1 | ||
| 3224 | |||
| 3225 | my $others = $process_1->others; | ||
| 3226 | |||
| 3227 | # [12346, 12347] | ||
| 3228 | |||
| 3229 | =back | ||
| 3230 | |||
| 3231 | =cut | ||
| 3232 | |||
| 3233 | =head2 others_active | ||
| 3234 | |||
| 3235 | others_active() (arrayref) | ||
| 3236 | |||
| 3237 | The others_active method returns all L</registrants> other than the current | ||
| 3238 | process which are active, i.e. all other registered process PIDs that responds | ||
| 3239 | to L</ping>. | ||
| 3240 | |||
| 3241 | I<Since C<2.91>> | ||
| 3242 | |||
| 3243 | =over 4 | ||
| 3244 | |||
| 3245 | =item others_active example 1 | ||
| 3246 | |||
| 3247 | # given: synopsis | ||
| 3248 | |||
| 3249 | package main; | ||
| 3250 | |||
| 3251 | # in process 1 | ||
| 3252 | |||
| 3253 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3254 | |||
| 3255 | # in process 2 | ||
| 3256 | |||
| 3257 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3258 | |||
| 3259 | # in process 3 | ||
| 3260 | |||
| 3261 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 3262 | |||
| 3263 | # in process 1 | ||
| 3264 | |||
| 3265 | my $others_active = $process_1->others_active; | ||
| 3266 | |||
| 3267 | # [12346, 12347] | ||
| 3268 | |||
| 3269 | =back | ||
| 3270 | |||
| 3271 | =cut | ||
| 3272 | |||
| 3273 | =head2 others_inactive | ||
| 3274 | |||
| 3275 | others_inactive() (arrayref) | ||
| 3276 | |||
| 3277 | The others_inactive method returns all L</registrants> other than the current | ||
| 3278 | process which are inactive, i.e. all other registered process PIDs that do not | ||
| 3279 | respond to L</ping>. | ||
| 3280 | |||
| 3281 | I<Since C<2.91>> | ||
| 3282 | |||
| 3283 | =over 4 | ||
| 3284 | |||
| 3285 | =item others_inactive example 1 | ||
| 3286 | |||
| 3287 | # given: synopsis | ||
| 3288 | |||
| 3289 | package main; | ||
| 3290 | |||
| 3291 | # in process 1 | ||
| 3292 | |||
| 3293 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3294 | |||
| 3295 | # in process 2 | ||
| 3296 | |||
| 3297 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3298 | |||
| 3299 | # in process 3 | ||
| 3300 | |||
| 3301 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 3302 | |||
| 3303 | # in process 1 (assuming all processes exited) | ||
| 3304 | |||
| 3305 | my $others_inactive = $process_1->others_inactive; | ||
| 3306 | |||
| 3307 | # [12346, 12347] | ||
| 3308 | |||
| 3309 | =back | ||
| 3310 | |||
| 3311 | =cut | ||
| 3312 | |||
| 3313 | =head2 pid | ||
| 3314 | |||
| 3315 | pid() (number) | ||
| 3316 | |||
| 3317 | The pid method returns the PID of the current process. | ||
| 3318 | |||
| 3319 | I<Since C<2.40>> | ||
| 3320 | |||
| 3321 | =over 4 | ||
| 3322 | |||
| 3323 | =item pid example 1 | ||
| 3324 | |||
| 3325 | package main; | ||
| 3326 | |||
| 3327 | use Venus::Process; | ||
| 3328 | |||
| 3329 | my $parent = Venus::Process->new; | ||
| 3330 | |||
| 3331 | my $pid = $parent->pid; | ||
| 3332 | |||
| 3333 | # 00000 | ||
| 3334 | |||
| 3335 | =back | ||
| 3336 | |||
| 3337 | =cut | ||
| 3338 | |||
| 3339 | =head2 pids | ||
| 3340 | |||
| 3341 | pids() (arrayref) | ||
| 3342 | |||
| 3343 | The pids method returns the PID of the current process, and the PIDs of any | ||
| 3344 | child processes. | ||
| 3345 | |||
| 3346 | I<Since C<2.40>> | ||
| 3347 | |||
| 3348 | =over 4 | ||
| 3349 | |||
| 3350 | =item pids example 1 | ||
| 3351 | |||
| 3352 | package main; | ||
| 3353 | |||
| 3354 | use Venus::Process; | ||
| 3355 | |||
| 3356 | my $parent = Venus::Process->new; | ||
| 3357 | |||
| 3358 | my $pids = $parent->pids; | ||
| 3359 | |||
| 3360 | # [00000] | ||
| 3361 | |||
| 3362 | =back | ||
| 3363 | |||
| 3364 | =over 4 | ||
| 3365 | |||
| 3366 | =item pids example 2 | ||
| 3367 | |||
| 3368 | package main; | ||
| 3369 | |||
| 3370 | use Venus::Process; | ||
| 3371 | |||
| 3372 | my $parent = Venus::Process->new; | ||
| 3373 | |||
| 3374 | $parent->watch(1001..1004); | ||
| 3375 | |||
| 3376 | my $pids = $parent->pids; | ||
| 3377 | |||
| 3378 | # [00000, 1001..1004] | ||
| 3379 | |||
| 3380 | =back | ||
| 3381 | |||
| 3382 | =cut | ||
| 3383 | |||
| 3384 | =head2 ping | ||
| 3385 | |||
| 3386 | ping(number @pids) (number) | ||
| 3387 | |||
| 3388 | The ping method returns truthy if the process of the PID provided is active. If | ||
| 3389 | multiple PIDs are provided, this method will return the count of active PIDs. | ||
| 3390 | |||
| 3391 | I<Since C<2.01>> | ||
| 3392 | |||
| 3393 | =over 4 | ||
| 3394 | |||
| 3395 | =item ping example 1 | ||
| 3396 | |||
| 3397 | # given: synopsis; | ||
| 3398 | |||
| 3399 | if ($process = $parent->fork) { | ||
| 3400 | # in forked process ... | ||
| 3401 | $process->exit; | ||
| 3402 | } | ||
| 3403 | |||
| 3404 | my $ping = $parent->ping(int $process); | ||
| 3405 | |||
| 3406 | # 1 | ||
| 3407 | |||
| 3408 | =back | ||
| 3409 | |||
| 3410 | =cut | ||
| 3411 | |||
| 3412 | =head2 poll | ||
| 3413 | |||
| 3414 | poll(number $timeout, string | coderef $code, any @args) (arrayref) | ||
| 3415 | |||
| 3416 | The poll method continuously calls the named method or coderef and returns the | ||
| 3417 | result that's not undefined, or throws an exception on timeout. If no method | ||
| 3418 | name is provided this method will default to calling L</recvall>. | ||
| 3419 | |||
| 3420 | I<Since C<2.91>> | ||
| 3421 | |||
| 3422 | =over 4 | ||
| 3423 | |||
| 3424 | =item poll example 1 | ||
| 3425 | |||
| 3426 | # given: synopsis | ||
| 3427 | |||
| 3428 | package main; | ||
| 3429 | |||
| 3430 | my $poll = $parent->poll(0, 'ping', $parent->pid); | ||
| 3431 | |||
| 3432 | # [1] | ||
| 3433 | |||
| 3434 | =back | ||
| 3435 | |||
| 3436 | =over 4 | ||
| 3437 | |||
| 3438 | =item poll example 2 | ||
| 3439 | |||
| 3440 | # given: synopsis | ||
| 3441 | |||
| 3442 | package main; | ||
| 3443 | |||
| 3444 | my $poll = $parent->poll(5, 'ping', $parent->pid); | ||
| 3445 | |||
| 3446 | # [1] | ||
| 3447 | |||
| 3448 | =back | ||
| 3449 | |||
| 3450 | =over 4 | ||
| 3451 | |||
| 3452 | =item poll example 3 | ||
| 3453 | |||
| 3454 | # given: synopsis | ||
| 3455 | |||
| 3456 | package main; | ||
| 3457 | |||
| 3458 | my $poll = $parent->poll(0, 'recv', $parent->pid); | ||
| 3459 | |||
| 3460 | # Exception! (isa Venus::Process::Error) (see error_on_timeout_poll) | ||
| 3461 | |||
| 3462 | =back | ||
| 3463 | |||
| 3464 | =over 4 | ||
| 3465 | |||
| 3466 | =item poll example 4 | ||
| 3467 | |||
| 3468 | # given: synopsis | ||
| 3469 | |||
| 3470 | package main; | ||
| 3471 | |||
| 3472 | my $poll = $parent->poll(5, sub { | ||
| 3473 | int(rand(2)) ? "" : () | ||
| 3474 | }); | ||
| 3475 | |||
| 3476 | # [""] | ||
| 3477 | |||
| 3478 | =back | ||
| 3479 | |||
| 3480 | =cut | ||
| 3481 | |||
| 3482 | =head2 pool | ||
| 3483 | |||
| 3484 | pool(number $count, number $timeout) (Venus::Process) | ||
| 3485 | |||
| 3486 | The pool method blocks the execution of the current process until the number of | ||
| 3487 | L</other> processes are registered and pingable. This method returns the | ||
| 3488 | invocant when successful, or throws an exception if the operation timed out. | ||
| 3489 | |||
| 3490 | I<Since C<2.91>> | ||
| 3491 | |||
| 3492 | =over 4 | ||
| 3493 | |||
| 3494 | =item pool example 1 | ||
| 3495 | |||
| 3496 | # given: synopsis | ||
| 3497 | |||
| 3498 | package main; | ||
| 3499 | |||
| 3500 | # in process 1 | ||
| 3501 | |||
| 3502 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3503 | |||
| 3504 | # in process 2 | ||
| 3505 | |||
| 3506 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3507 | |||
| 3508 | # in process 1 | ||
| 3509 | |||
| 3510 | $process_1 = $process_1->pool; | ||
| 3511 | |||
| 3512 | # bless({...}, 'Venus::Process') | ||
| 3513 | |||
| 3514 | =back | ||
| 3515 | |||
| 3516 | =over 4 | ||
| 3517 | |||
| 3518 | =item pool example 2 | ||
| 3519 | |||
| 3520 | # given: synopsis | ||
| 3521 | |||
| 3522 | package main; | ||
| 3523 | |||
| 3524 | # in process 1 | ||
| 3525 | |||
| 3526 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3527 | |||
| 3528 | # in process 2 | ||
| 3529 | |||
| 3530 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3531 | |||
| 3532 | # in process 3 | ||
| 3533 | |||
| 3534 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 3535 | |||
| 3536 | # in process 1 | ||
| 3537 | |||
| 3538 | $process_1 = $process_1->pool(2); | ||
| 3539 | |||
| 3540 | # bless({...}, 'Venus::Process') | ||
| 3541 | |||
| 3542 | =back | ||
| 3543 | |||
| 3544 | =over 4 | ||
| 3545 | |||
| 3546 | =item pool example 3 | ||
| 3547 | |||
| 3548 | # given: synopsis | ||
| 3549 | |||
| 3550 | package main; | ||
| 3551 | |||
| 3552 | # in process 1 | ||
| 3553 | |||
| 3554 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3555 | |||
| 3556 | # in process 2 | ||
| 3557 | |||
| 3558 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3559 | |||
| 3560 | # in process 3 | ||
| 3561 | |||
| 3562 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 3563 | |||
| 3564 | # in process 1 | ||
| 3565 | |||
| 3566 | $process_1 = $process_1->pool(3, 0); | ||
| 3567 | |||
| 3568 | # Exception! (isa Venus::Process::Error) (see error_on_timeout_pool) | ||
| 3569 | |||
| 3570 | =back | ||
| 3571 | |||
| 3572 | =cut | ||
| 3573 | |||
| 3574 | =head2 ppid | ||
| 3575 | |||
| 3576 | ppid() (number) | ||
| 3577 | |||
| 3578 | The ppid method returns the PID of the parent process (i.e. the process which | ||
| 3579 | forked the current process, if any). | ||
| 3580 | |||
| 3581 | I<Since C<2.91>> | ||
| 3582 | |||
| 3583 | =over 4 | ||
| 3584 | |||
| 3585 | =item ppid example 1 | ||
| 3586 | |||
| 3587 | # given: synopsis; | ||
| 3588 | |||
| 3589 | my $ppid = $parent->ppid; | ||
| 3590 | |||
| 3591 | # undef | ||
| 3592 | |||
| 3593 | =back | ||
| 3594 | |||
| 3595 | =over 4 | ||
| 3596 | |||
| 3597 | =item ppid example 2 | ||
| 3598 | |||
| 3599 | # given: synopsis; | ||
| 3600 | |||
| 3601 | $process = $parent->fork; | ||
| 3602 | |||
| 3603 | # in child process | ||
| 3604 | |||
| 3605 | my $ppid = $process->ppid; | ||
| 3606 | |||
| 3607 | # 00000 | ||
| 3608 | |||
| 3609 | =back | ||
| 3610 | |||
| 3611 | =cut | ||
| 3612 | |||
| 3613 | =head2 prune | ||
| 3614 | |||
| 3615 | prune() (Venus::Process) | ||
| 3616 | |||
| 3617 | The prune method removes all stopped processes and returns the invocant. | ||
| 3618 | |||
| 3619 | I<Since C<2.40>> | ||
| 3620 | |||
| 3621 | =over 4 | ||
| 3622 | |||
| 3623 | =item prune example 1 | ||
| 3624 | |||
| 3625 | package main; | ||
| 3626 | |||
| 3627 | use Venus::Process; | ||
| 3628 | |||
| 3629 | my $parent = Venus::Process->new; | ||
| 3630 | |||
| 3631 | $parent->watch(1001); | ||
| 3632 | |||
| 3633 | $parent = $parent->prune; | ||
| 3634 | |||
| 3635 | # bless({...}, 'Venus::Process') | ||
| 3636 | |||
| 3637 | =back | ||
| 3638 | |||
| 3639 | =over 4 | ||
| 3640 | |||
| 3641 | =item prune example 2 | ||
| 3642 | |||
| 3643 | package main; | ||
| 3644 | |||
| 3645 | use Venus::Process; | ||
| 3646 | |||
| 3647 | my $parent = Venus::Process->new; | ||
| 3648 | |||
| 3649 | my $process = $parent->fork; | ||
| 3650 | |||
| 3651 | if ($process) { | ||
| 3652 | # in forked process ... | ||
| 3653 | $process->exit; | ||
| 3654 | } | ||
| 3655 | |||
| 3656 | $parent = $parent->prune; | ||
| 3657 | |||
| 3658 | # bless({...}, 'Venus::Process') | ||
| 3659 | |||
| 3660 | =back | ||
| 3661 | |||
| 3662 | =over 4 | ||
| 3663 | |||
| 3664 | =item prune example 3 | ||
| 3665 | |||
| 3666 | package main; | ||
| 3667 | |||
| 3668 | use Venus::Process; | ||
| 3669 | |||
| 3670 | my $parent = Venus::Process->new; | ||
| 3671 | |||
| 3672 | $parent->work(sub { | ||
| 3673 | my ($process) = @_; | ||
| 3674 | # in forked process ... | ||
| 3675 | $process->exit; | ||
| 3676 | }); | ||
| 3677 | |||
| 3678 | $parent = $parent->prune; | ||
| 3679 | |||
| 3680 | # bless({...}, 'Venus::Process') | ||
| 3681 | |||
| 3682 | =back | ||
| 3683 | |||
| 3684 | =cut | ||
| 3685 | |||
| 3686 | =head2 recall | ||
| 3687 | |||
| 3688 | recall(number $pid) (any) | ||
| 3689 | |||
| 3690 | The recall method returns the earliest message, sent by the current process to | ||
| 3691 | the process specified by the PID provided, which is no longer active (i.e. | ||
| 3692 | responding to L</ping>). | ||
| 3693 | |||
| 3694 | I<Since C<2.91>> | ||
| 3695 | |||
| 3696 | =over 4 | ||
| 3697 | |||
| 3698 | =item recall example 1 | ||
| 3699 | |||
| 3700 | # given: synopsis | ||
| 3701 | |||
| 3702 | package main; | ||
| 3703 | |||
| 3704 | # in process 1 | ||
| 3705 | |||
| 3706 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3707 | |||
| 3708 | # in process 2 | ||
| 3709 | |||
| 3710 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3711 | |||
| 3712 | # in process 1 | ||
| 3713 | |||
| 3714 | $process_1->send($process_2->pid, {from => $process_1->pid}); | ||
| 3715 | |||
| 3716 | # in process 1 (process 2) | ||
| 3717 | |||
| 3718 | my $recall = $process_1->recall($process_2->pid); | ||
| 3719 | |||
| 3720 | # {from => 12345} | ||
| 3721 | |||
| 3722 | =back | ||
| 3723 | |||
| 3724 | =cut | ||
| 3725 | |||
| 3726 | =head2 recallall | ||
| 3727 | |||
| 3728 | recallall() (arrayref) | ||
| 3729 | |||
| 3730 | The recallall method performs a L</recall> on the parent process (if any) via | ||
| 3731 | L</ppid> and any process listed in the L</watchlist>, and returns the results. | ||
| 3732 | |||
| 3733 | I<Since C<2.91>> | ||
| 3734 | |||
| 3735 | =over 4 | ||
| 3736 | |||
| 3737 | =item recallall example 1 | ||
| 3738 | |||
| 3739 | # given: synopsis | ||
| 3740 | |||
| 3741 | package main; | ||
| 3742 | |||
| 3743 | # in process 1 | ||
| 3744 | |||
| 3745 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3746 | |||
| 3747 | # in process 2 | ||
| 3748 | |||
| 3749 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3750 | |||
| 3751 | # in process 3 | ||
| 3752 | |||
| 3753 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 3754 | |||
| 3755 | # in process 1 | ||
| 3756 | |||
| 3757 | $process_1->send($process_2->pid, {from => $process_1->pid}); | ||
| 3758 | |||
| 3759 | $process_1->send($process_3->pid, {from => $process_1->pid}); | ||
| 3760 | |||
| 3761 | $process_1->watch($process_2->pid, $process_3->pid); | ||
| 3762 | |||
| 3763 | # in process 1 (process 2 and 3 died) | ||
| 3764 | |||
| 3765 | my $recallall = $process_1->recallall; | ||
| 3766 | |||
| 3767 | # [{from => 12345}, {from => 12345}] | ||
| 3768 | |||
| 3769 | =back | ||
| 3770 | |||
| 3771 | =cut | ||
| 3772 | |||
| 3773 | =head2 recv | ||
| 3774 | |||
| 3775 | recv(number $pid) (any) | ||
| 3776 | |||
| 3777 | The recv method returns the earliest message found from the process specified | ||
| 3778 | by the PID provided. | ||
| 3779 | |||
| 3780 | I<Since C<2.91>> | ||
| 3781 | |||
| 3782 | =over 4 | ||
| 3783 | |||
| 3784 | =item recv example 1 | ||
| 3785 | |||
| 3786 | # given: synopsis | ||
| 3787 | |||
| 3788 | package main; | ||
| 3789 | |||
| 3790 | my $recv = $parent->recv; | ||
| 3791 | |||
| 3792 | # undef | ||
| 3793 | |||
| 3794 | =back | ||
| 3795 | |||
| 3796 | =over 4 | ||
| 3797 | |||
| 3798 | =item recv example 2 | ||
| 3799 | |||
| 3800 | # given: synopsis | ||
| 3801 | |||
| 3802 | package main; | ||
| 3803 | |||
| 3804 | # in process 1 | ||
| 3805 | |||
| 3806 | my $process_1 = Venus::Process->new(12345); | ||
| 3807 | |||
| 3808 | # in process 2 | ||
| 3809 | |||
| 3810 | my $process_2 = Venus::Process->new(12346); | ||
| 3811 | |||
| 3812 | # in process 1 | ||
| 3813 | |||
| 3814 | my $recv = $process_1->recv($process_2->pid); | ||
| 3815 | |||
| 3816 | # undef | ||
| 3817 | |||
| 3818 | # in process 2 | ||
| 3819 | |||
| 3820 | $process_2->send($process_1->pid, {from => $process_2->pid, said => 'hello'}); | ||
| 3821 | |||
| 3822 | # in process 1 | ||
| 3823 | |||
| 3824 | $recv = $process_1->recv($process_2->pid); | ||
| 3825 | |||
| 3826 | # {from => 12346, said => 'hello'} | ||
| 3827 | |||
| 3828 | =back | ||
| 3829 | |||
| 3830 | =cut | ||
| 3831 | |||
| 3832 | =head2 recvall | ||
| 3833 | |||
| 3834 | recvall() (arrayref) | ||
| 3835 | |||
| 3836 | The recvall method performs a L</recv> on the parent process (if any) via | ||
| 3837 | L</ppid> and any process listed in the L</watchlist>, and returns the results. | ||
| 3838 | |||
| 3839 | I<Since C<2.91>> | ||
| 3840 | |||
| 3841 | =over 4 | ||
| 3842 | |||
| 3843 | =item recvall example 1 | ||
| 3844 | |||
| 3845 | # given: synopsis | ||
| 3846 | |||
| 3847 | package main; | ||
| 3848 | |||
| 3849 | # in process 1 | ||
| 3850 | |||
| 3851 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3852 | |||
| 3853 | # in process 2 | ||
| 3854 | |||
| 3855 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3856 | |||
| 3857 | $process_2->send($process_1->pid, {from => $process_2->pid}); | ||
| 3858 | |||
| 3859 | # in process 3 | ||
| 3860 | |||
| 3861 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 3862 | |||
| 3863 | $process_3->send($process_1->pid, {from => $process_3->pid}); | ||
| 3864 | |||
| 3865 | # in process 1 | ||
| 3866 | |||
| 3867 | my $recvall = $process_1->pool(2)->recvall; | ||
| 3868 | |||
| 3869 | # [{from => 12346}, {from => 12347}] | ||
| 3870 | |||
| 3871 | =back | ||
| 3872 | |||
| 3873 | =over 4 | ||
| 3874 | |||
| 3875 | =item recvall example 2 | ||
| 3876 | |||
| 3877 | # given: synopsis | ||
| 3878 | |||
| 3879 | package main; | ||
| 3880 | |||
| 3881 | # in process 1 | ||
| 3882 | |||
| 3883 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3884 | |||
| 3885 | # in process 2 | ||
| 3886 | |||
| 3887 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3888 | |||
| 3889 | $process_2->send($process_1->pid, {from => $process_2->pid}); | ||
| 3890 | |||
| 3891 | # in process 3 | ||
| 3892 | |||
| 3893 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 3894 | |||
| 3895 | $process_3->send($process_1->pid, {from => $process_3->pid}); | ||
| 3896 | |||
| 3897 | # in process 1 | ||
| 3898 | |||
| 3899 | my $recvall = $process_1->pool(2)->recvall; | ||
| 3900 | |||
| 3901 | # [{from => 12346}, {from => 12347}] | ||
| 3902 | |||
| 3903 | # in process 2 | ||
| 3904 | |||
| 3905 | $process_2->send($process_1->pid, {from => $process_2->pid}); | ||
| 3906 | |||
| 3907 | # in process 1 | ||
| 3908 | |||
| 3909 | $recvall = $process_1->recvall; | ||
| 3910 | |||
| 3911 | # [{from => 12346}] | ||
| 3912 | |||
| 3913 | =back | ||
| 3914 | |||
| 3915 | =cut | ||
| 3916 | |||
| 3917 | =head2 register | ||
| 3918 | |||
| 3919 | register() (Venus::Process) | ||
| 3920 | |||
| 3921 | The register method declares that the process is willing to cooperate with | ||
| 3922 | others (e.g. L</send> nad L</recv> messages), in a way that's discoverable by | ||
| 3923 | other processes, and returns the invocant. | ||
| 3924 | |||
| 3925 | I<Since C<2.91>> | ||
| 3926 | |||
| 3927 | =over 4 | ||
| 3928 | |||
| 3929 | =item register example 1 | ||
| 3930 | |||
| 3931 | # given: synopsis | ||
| 3932 | |||
| 3933 | package main; | ||
| 3934 | |||
| 3935 | my $register = $parent->register; | ||
| 3936 | |||
| 3937 | # bless({...}, 'Venus::Process') | ||
| 3938 | |||
| 3939 | =back | ||
| 3940 | |||
| 3941 | =cut | ||
| 3942 | |||
| 3943 | =head2 registrants | ||
| 3944 | |||
| 3945 | registrants() (arrayref) | ||
| 3946 | |||
| 3947 | The registrants method returns the PIDs for all the processes that registered | ||
| 3948 | using the L</register> method whether they're currently active or not. | ||
| 3949 | |||
| 3950 | I<Since C<2.91>> | ||
| 3951 | |||
| 3952 | =over 4 | ||
| 3953 | |||
| 3954 | =item registrants example 1 | ||
| 3955 | |||
| 3956 | # given: synopsis | ||
| 3957 | |||
| 3958 | package main; | ||
| 3959 | |||
| 3960 | # in process 1 | ||
| 3961 | |||
| 3962 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 3963 | |||
| 3964 | # in process 2 | ||
| 3965 | |||
| 3966 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 3967 | |||
| 3968 | # in process 3 | ||
| 3969 | |||
| 3970 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 3971 | |||
| 3972 | # in process 1 | ||
| 3973 | |||
| 3974 | my $registrants = $process_1->registrants; | ||
| 3975 | |||
| 3976 | # [12345, 12346, 12347] | ||
| 3977 | |||
| 3978 | =back | ||
| 3979 | |||
| 3980 | =cut | ||
| 3981 | |||
| 3982 | =head2 restart | ||
| 3983 | |||
| 3984 | restart(coderef $callback) (arrayref) | ||
| 3985 | |||
| 3986 | The restart method executes the callback provided for each PID returned by the | ||
| 3987 | L</stopped> method, passing the pid and the results of L</check> to the | ||
| 3988 | callback as arguments, and returns the result of each call as an arrayref. In | ||
| 3989 | list context, this method returns a list. | ||
| 3990 | |||
| 3991 | I<Since C<2.40>> | ||
| 3992 | |||
| 3993 | =over 4 | ||
| 3994 | |||
| 3995 | =item restart example 1 | ||
| 3996 | |||
| 3997 | # given: synopsis | ||
| 3998 | |||
| 3999 | package main; | ||
| 4000 | |||
| 4001 | $parent->watch(1001); | ||
| 4002 | |||
| 4003 | my $restart = $parent->restart(sub { | ||
| 4004 | my ($pid, $check, $exit) = @_; | ||
| 4005 | |||
| 4006 | # redeploy stopped process | ||
| 4007 | |||
| 4008 | return [$pid, $check, $exit]; | ||
| 4009 | }); | ||
| 4010 | |||
| 4011 | # [[1001, 1001, 255]] | ||
| 4012 | |||
| 4013 | =back | ||
| 4014 | |||
| 4015 | =cut | ||
| 4016 | |||
| 4017 | =head2 send | ||
| 4018 | |||
| 4019 | send(number $pid, any $data) (Venus::Process) | ||
| 4020 | |||
| 4021 | The send method makes the data provided available to the process specified by | ||
| 4022 | the PID provided. | ||
| 4023 | |||
| 4024 | I<Since C<2.91>> | ||
| 4025 | |||
| 4026 | =over 4 | ||
| 4027 | |||
| 4028 | =item send example 1 | ||
| 4029 | |||
| 4030 | # given: synopsis | ||
| 4031 | |||
| 4032 | package main; | ||
| 4033 | |||
| 4034 | my $send = $parent->send; | ||
| 4035 | |||
| 4036 | # bless({...}, 'Venus::Process') | ||
| 4037 | |||
| 4038 | =back | ||
| 4039 | |||
| 4040 | =over 4 | ||
| 4041 | |||
| 4042 | =item send example 2 | ||
| 4043 | |||
| 4044 | # given: synopsis | ||
| 4045 | |||
| 4046 | package main; | ||
| 4047 | |||
| 4048 | # in process 1 | ||
| 4049 | |||
| 4050 | my $process_1 = Venus::Process->new(12345); | ||
| 4051 | |||
| 4052 | # in process 2 | ||
| 4053 | |||
| 4054 | my $process_2 = Venus::Process->new(12346); | ||
| 4055 | |||
| 4056 | # in process 1 | ||
| 4057 | |||
| 4058 | $process_1 = $process_1->send($process_2->pid, { | ||
| 4059 | from => $process_1->pid, said => 'hello', | ||
| 4060 | }); | ||
| 4061 | |||
| 4062 | # bless({...}, 'Venus::Process') | ||
| 4063 | |||
| 4064 | # in process 2 | ||
| 4065 | |||
| 4066 | # $process_2->recv($process_1->pid); | ||
| 4067 | |||
| 4068 | # {from => 12345, said => 'hello'} | ||
| 4069 | |||
| 4070 | =back | ||
| 4071 | |||
| 4072 | =cut | ||
| 4073 | |||
| 4074 | =head2 sendall | ||
| 4075 | |||
| 4076 | sendall(any $data) (Venus::Process) | ||
| 4077 | |||
| 4078 | The sendall method performs a L</send> on the parent process (if any) via | ||
| 4079 | L</ppid> and any process listed in the L</watchlist>, and returns the invocant. | ||
| 4080 | |||
| 4081 | I<Since C<2.91>> | ||
| 4082 | |||
| 4083 | =over 4 | ||
| 4084 | |||
| 4085 | =item sendall example 1 | ||
| 4086 | |||
| 4087 | # given: synopsis | ||
| 4088 | |||
| 4089 | package main; | ||
| 4090 | |||
| 4091 | # in process 1 | ||
| 4092 | |||
| 4093 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 4094 | |||
| 4095 | # in process 2 | ||
| 4096 | |||
| 4097 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 4098 | |||
| 4099 | # in process 3 | ||
| 4100 | |||
| 4101 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 4102 | |||
| 4103 | # in process 1 | ||
| 4104 | |||
| 4105 | $process_1 = $process_1->pool(2)->sendall({ | ||
| 4106 | from => $process_1->pid, said => 'hello', | ||
| 4107 | }); | ||
| 4108 | |||
| 4109 | # bless({...}, 'Venus::Process') | ||
| 4110 | |||
| 4111 | =back | ||
| 4112 | |||
| 4113 | =cut | ||
| 4114 | |||
| 4115 | =head2 serve | ||
| 4116 | |||
| 4117 | serve(number $count, coderef $callback) (Venus::Process) | ||
| 4118 | |||
| 4119 | The serve method executes the callback using L</work> until L</limit> blocks | ||
| 4120 | the execution of the current process, indefinitely. It has the effect of | ||
| 4121 | serving the callback and maintaining the desired number of forks until killed | ||
| 4122 | or gracefully shutdown. | ||
| 4123 | |||
| 4124 | I<Since C<3.40>> | ||
| 4125 | |||
| 4126 | =over 4 | ||
| 4127 | |||
| 4128 | =item serve example 1 | ||
| 4129 | |||
| 4130 | package main; | ||
| 4131 | |||
| 4132 | use Venus::Process; | ||
| 4133 | |||
| 4134 | my $parent = Venus::Process->new; | ||
| 4135 | |||
| 4136 | $parent->serve(2, sub { | ||
| 4137 | my ($process) = @_; | ||
| 4138 | # in forked process ... | ||
| 4139 | $process->exit; | ||
| 4140 | }); | ||
| 4141 | |||
| 4142 | # ... | ||
| 4143 | |||
| 4144 | # bless({...}, "Venus::Process") | ||
| 4145 | |||
| 4146 | =back | ||
| 4147 | |||
| 4148 | =over 4 | ||
| 4149 | |||
| 4150 | =item serve example 2 | ||
| 4151 | |||
| 4152 | package main; | ||
| 4153 | |||
| 4154 | use Venus::Process; | ||
| 4155 | |||
| 4156 | my $parent = Venus::Process->new; | ||
| 4157 | |||
| 4158 | $parent->serve(10, sub { | ||
| 4159 | my ($process) = @_; | ||
| 4160 | # in forked process ... | ||
| 4161 | $process->exit; | ||
| 4162 | }); | ||
| 4163 | |||
| 4164 | # ... | ||
| 4165 | |||
| 4166 | # bless({...}, "Venus::Process") | ||
| 4167 | |||
| 4168 | =back | ||
| 4169 | |||
| 4170 | =cut | ||
| 4171 | |||
| 4172 | =head2 setsid | ||
| 4173 | |||
| 4174 | setsid() (number) | ||
| 4175 | |||
| 4176 | The setsid method calls the L<POSIX/setsid> function and sets the process group | ||
| 4177 | identifier of the current process. | ||
| 4178 | |||
| 4179 | I<Since C<0.06>> | ||
| 4180 | |||
| 4181 | =over 4 | ||
| 4182 | |||
| 4183 | =item setsid example 1 | ||
| 4184 | |||
| 4185 | # given: synopsis; | ||
| 4186 | |||
| 4187 | my $setsid = $parent->setsid; | ||
| 4188 | |||
| 4189 | # 1 | ||
| 4190 | |||
| 4191 | =back | ||
| 4192 | |||
| 4193 | =over 4 | ||
| 4194 | |||
| 4195 | =item setsid example 2 | ||
| 4196 | |||
| 4197 | # given: synopsis; | ||
| 4198 | |||
| 4199 | my $setsid = $parent->setsid; | ||
| 4200 | |||
| 4201 | # Exception! (isa Venus::Process::Error) (see error_on_setid) | ||
| 4202 | |||
| 4203 | =back | ||
| 4204 | |||
| 4205 | =cut | ||
| 4206 | |||
| 4207 | =head2 started | ||
| 4208 | |||
| 4209 | started() (arrayref) | ||
| 4210 | |||
| 4211 | The started method returns a list of PIDs whose processes have been started and | ||
| 4212 | which have not terminated. Returns a list in list context. | ||
| 4213 | |||
| 4214 | I<Since C<2.40>> | ||
| 4215 | |||
| 4216 | =over 4 | ||
| 4217 | |||
| 4218 | =item started example 1 | ||
| 4219 | |||
| 4220 | # given: synopsis | ||
| 4221 | |||
| 4222 | package main; | ||
| 4223 | |||
| 4224 | my $started = $parent->started; | ||
| 4225 | |||
| 4226 | # child not terminated | ||
| 4227 | |||
| 4228 | # [...] | ||
| 4229 | |||
| 4230 | =back | ||
| 4231 | |||
| 4232 | =over 4 | ||
| 4233 | |||
| 4234 | =item started example 2 | ||
| 4235 | |||
| 4236 | # given: synopsis | ||
| 4237 | |||
| 4238 | package main; | ||
| 4239 | |||
| 4240 | my $started = $parent->started; | ||
| 4241 | |||
| 4242 | # child terminated | ||
| 4243 | |||
| 4244 | # [] | ||
| 4245 | |||
| 4246 | =back | ||
| 4247 | |||
| 4248 | =cut | ||
| 4249 | |||
| 4250 | =head2 status | ||
| 4251 | |||
| 4252 | status(coderef $callback) (arrayref) | ||
| 4253 | |||
| 4254 | The status method executes the callback provided for each PID in the | ||
| 4255 | L</watchlist>, passing the pid and the results of L</check> to the callback as | ||
| 4256 | arguments, and returns the result of each call as an arrayref. In list context, | ||
| 4257 | this method returns a list. | ||
| 4258 | |||
| 4259 | I<Since C<2.40>> | ||
| 4260 | |||
| 4261 | =over 4 | ||
| 4262 | |||
| 4263 | =item status example 1 | ||
| 4264 | |||
| 4265 | package main; | ||
| 4266 | |||
| 4267 | use Venus::Process; | ||
| 4268 | |||
| 4269 | my $parent = Venus::Process->new; | ||
| 4270 | |||
| 4271 | $parent->watch(12346); | ||
| 4272 | |||
| 4273 | my $status = $parent->status(sub { | ||
| 4274 | my ($pid, $check, $exit) = @_; | ||
| 4275 | |||
| 4276 | # assuming PID 12346 is still running (not terminated) | ||
| 4277 | return [$pid, $check, $exit]; | ||
| 4278 | }); | ||
| 4279 | |||
| 4280 | # [[12346, 0, -1]] | ||
| 4281 | |||
| 4282 | =back | ||
| 4283 | |||
| 4284 | =over 4 | ||
| 4285 | |||
| 4286 | =item status example 2 | ||
| 4287 | |||
| 4288 | package main; | ||
| 4289 | |||
| 4290 | use Venus::Process; | ||
| 4291 | |||
| 4292 | my $parent = Venus::Process->new; | ||
| 4293 | |||
| 4294 | $parent->watch(12346); | ||
| 4295 | |||
| 4296 | my $status = $parent->status(sub { | ||
| 4297 | my ($pid, $check, $exit) = @_; | ||
| 4298 | |||
| 4299 | # assuming process 12346 terminated with exit code 255 | ||
| 4300 | return [$pid, $check, $exit]; | ||
| 4301 | }); | ||
| 4302 | |||
| 4303 | # [[12346, 12346, 255]] | ||
| 4304 | |||
| 4305 | =back | ||
| 4306 | |||
| 4307 | =over 4 | ||
| 4308 | |||
| 4309 | =item status example 3 | ||
| 4310 | |||
| 4311 | package main; | ||
| 4312 | |||
| 4313 | use Venus::Process; | ||
| 4314 | |||
| 4315 | my $parent = Venus::Process->new; | ||
| 4316 | |||
| 4317 | $parent->watch(12346); | ||
| 4318 | |||
| 4319 | my @status = $parent->status(sub { | ||
| 4320 | my ($pid, $check, $exit) = @_; | ||
| 4321 | |||
| 4322 | # assuming process 12346 terminated with exit code 255 | ||
| 4323 | return [$pid, $check, $exit]; | ||
| 4324 | }); | ||
| 4325 | |||
| 4326 | # ([12346, 12346, 255]) | ||
| 4327 | |||
| 4328 | =back | ||
| 4329 | |||
| 4330 | =cut | ||
| 4331 | |||
| 4332 | =head2 stderr | ||
| 4333 | |||
| 4334 | stderr(string $path) (Venus::Process) | ||
| 4335 | |||
| 4336 | The stderr method redirects C<STDERR> to the path provided, typically | ||
| 4337 | C</dev/null> or some equivalent. If called with no arguments C<STDERR> will be | ||
| 4338 | restored to its default. | ||
| 4339 | |||
| 4340 | I<Since C<0.06>> | ||
| 4341 | |||
| 4342 | =over 4 | ||
| 4343 | |||
| 4344 | =item stderr example 1 | ||
| 4345 | |||
| 4346 | # given: synopsis; | ||
| 4347 | |||
| 4348 | $parent = $parent->stderr; | ||
| 4349 | |||
| 4350 | # bless({...}, 'Venus::Process') | ||
| 4351 | |||
| 4352 | =back | ||
| 4353 | |||
| 4354 | =over 4 | ||
| 4355 | |||
| 4356 | =item stderr example 2 | ||
| 4357 | |||
| 4358 | # given: synopsis; | ||
| 4359 | |||
| 4360 | $parent = $parent->stderr('/nowhere'); | ||
| 4361 | |||
| 4362 | # Exception! (isa Venus::Process:Error) (see error_on_stderr) | ||
| 4363 | |||
| 4364 | =back | ||
| 4365 | |||
| 4366 | =cut | ||
| 4367 | |||
| 4368 | =head2 stdin | ||
| 4369 | |||
| 4370 | stdin(string $path) (Venus::Process) | ||
| 4371 | |||
| 4372 | The stdin method redirects C<STDIN> to the path provided, typically | ||
| 4373 | C</dev/null> or some equivalent. If called with no arguments C<STDIN> will be | ||
| 4374 | restored to its default. | ||
| 4375 | |||
| 4376 | I<Since C<0.06>> | ||
| 4377 | |||
| 4378 | =over 4 | ||
| 4379 | |||
| 4380 | =item stdin example 1 | ||
| 4381 | |||
| 4382 | # given: synopsis; | ||
| 4383 | |||
| 4384 | $parent = $parent->stdin; | ||
| 4385 | |||
| 4386 | # bless({...}, 'Venus::Process') | ||
| 4387 | |||
| 4388 | =back | ||
| 4389 | |||
| 4390 | =over 4 | ||
| 4391 | |||
| 4392 | =item stdin example 2 | ||
| 4393 | |||
| 4394 | # given: synopsis; | ||
| 4395 | |||
| 4396 | $parent = $parent->stdin('/nowhere'); | ||
| 4397 | |||
| 4398 | # Exception! (isa Venus::Process::Error) (see error_on_stdin) | ||
| 4399 | |||
| 4400 | =back | ||
| 4401 | |||
| 4402 | =cut | ||
| 4403 | |||
| 4404 | =head2 stdout | ||
| 4405 | |||
| 4406 | stdout(string $path) (Venus::Process) | ||
| 4407 | |||
| 4408 | The stdout method redirects C<STDOUT> to the path provided, typically | ||
| 4409 | C</dev/null> or some equivalent. If called with no arguments C<STDOUT> will be | ||
| 4410 | restored to its default. | ||
| 4411 | |||
| 4412 | I<Since C<0.06>> | ||
| 4413 | |||
| 4414 | =over 4 | ||
| 4415 | |||
| 4416 | =item stdout example 1 | ||
| 4417 | |||
| 4418 | # given: synopsis; | ||
| 4419 | |||
| 4420 | $parent = $parent->stdout; | ||
| 4421 | |||
| 4422 | # bless({...}, 'Venus::Process') | ||
| 4423 | |||
| 4424 | =back | ||
| 4425 | |||
| 4426 | =over 4 | ||
| 4427 | |||
| 4428 | =item stdout example 2 | ||
| 4429 | |||
| 4430 | # given: synopsis; | ||
| 4431 | |||
| 4432 | $parent = $parent->stdout('/nowhere'); | ||
| 4433 | |||
| 4434 | # Exception! Venus::Process::Error (error_on_stdout) | ||
| 4435 | |||
| 4436 | =back | ||
| 4437 | |||
| 4438 | =cut | ||
| 4439 | |||
| 4440 | =head2 stopped | ||
| 4441 | |||
| 4442 | stopped() (arrayref) | ||
| 4443 | |||
| 4444 | The stopped method returns a list of PIDs whose processes have terminated. | ||
| 4445 | Returns a list in list context. | ||
| 4446 | |||
| 4447 | I<Since C<2.40>> | ||
| 4448 | |||
| 4449 | =over 4 | ||
| 4450 | |||
| 4451 | =item stopped example 1 | ||
| 4452 | |||
| 4453 | # given: synopsis | ||
| 4454 | |||
| 4455 | package main; | ||
| 4456 | |||
| 4457 | my $stopped = $parent->stopped; | ||
| 4458 | |||
| 4459 | # child terminated | ||
| 4460 | |||
| 4461 | # [...] | ||
| 4462 | |||
| 4463 | =back | ||
| 4464 | |||
| 4465 | =over 4 | ||
| 4466 | |||
| 4467 | =item stopped example 2 | ||
| 4468 | |||
| 4469 | # given: synopsis | ||
| 4470 | |||
| 4471 | package main; | ||
| 4472 | |||
| 4473 | my $stopped = $parent->stopped; | ||
| 4474 | |||
| 4475 | # child not terminated | ||
| 4476 | |||
| 4477 | # [] | ||
| 4478 | |||
| 4479 | =back | ||
| 4480 | |||
| 4481 | =cut | ||
| 4482 | |||
| 4483 | =head2 sync | ||
| 4484 | |||
| 4485 | sync(number $count, number $timeout) (Venus::Process) | ||
| 4486 | |||
| 4487 | The sync method blocks the execution of the current process until the number of | ||
| 4488 | L</other> processes are registered, pingable, and have each sent at-least one | ||
| 4489 | message to the current process. This method returns the invocant when | ||
| 4490 | successful, or throws an exception if the operation timed out. | ||
| 4491 | |||
| 4492 | I<Since C<2.91>> | ||
| 4493 | |||
| 4494 | =over 4 | ||
| 4495 | |||
| 4496 | =item sync example 1 | ||
| 4497 | |||
| 4498 | # given: synopsis | ||
| 4499 | |||
| 4500 | package main; | ||
| 4501 | |||
| 4502 | # in process 1 | ||
| 4503 | |||
| 4504 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 4505 | |||
| 4506 | # in process 2 | ||
| 4507 | |||
| 4508 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 4509 | |||
| 4510 | $process_2->send($process_1->pid, {from => $process_2->pid, said => "hello"}); | ||
| 4511 | |||
| 4512 | # in process 1 | ||
| 4513 | |||
| 4514 | $process_1 = $process_1->sync; | ||
| 4515 | |||
| 4516 | # bless({...}, 'Venus::Process') | ||
| 4517 | |||
| 4518 | =back | ||
| 4519 | |||
| 4520 | =over 4 | ||
| 4521 | |||
| 4522 | =item sync example 2 | ||
| 4523 | |||
| 4524 | # given: synopsis | ||
| 4525 | |||
| 4526 | package main; | ||
| 4527 | |||
| 4528 | # in process 1 | ||
| 4529 | |||
| 4530 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 4531 | |||
| 4532 | # in process 2 | ||
| 4533 | |||
| 4534 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 4535 | |||
| 4536 | $process_2->send($process_1->pid, {from => $process_2->pid, said => "hello"}); | ||
| 4537 | |||
| 4538 | # in process 3 | ||
| 4539 | |||
| 4540 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 4541 | |||
| 4542 | $process_3->send($process_1->pid, {from => $process_3->pid, said => "hello"}); | ||
| 4543 | |||
| 4544 | # in process 1 | ||
| 4545 | |||
| 4546 | $process_1 = $process_1->sync(2); | ||
| 4547 | |||
| 4548 | # bless({...}, 'Venus::Process') | ||
| 4549 | |||
| 4550 | =back | ||
| 4551 | |||
| 4552 | =over 4 | ||
| 4553 | |||
| 4554 | =item sync example 3 | ||
| 4555 | |||
| 4556 | # given: synopsis | ||
| 4557 | |||
| 4558 | package main; | ||
| 4559 | |||
| 4560 | # in process 1 | ||
| 4561 | |||
| 4562 | my $process_1 = Venus::Process->new(12345)->register; | ||
| 4563 | |||
| 4564 | # in process 2 | ||
| 4565 | |||
| 4566 | my $process_2 = Venus::Process->new(12346)->register; | ||
| 4567 | |||
| 4568 | $process_2->send($process_1->pid, {from => $process_2->pid, said => "hello"}); | ||
| 4569 | |||
| 4570 | # in process 3 | ||
| 4571 | |||
| 4572 | my $process_3 = Venus::Process->new(12347)->register; | ||
| 4573 | |||
| 4574 | $process_3->send($process_1->pid, {from => $process_3->pid, said => "hello"}); | ||
| 4575 | |||
| 4576 | # in process 1 | ||
| 4577 | |||
| 4578 | $process_1 = $process_1->sync(3, 0); | ||
| 4579 | |||
| 4580 | # Exception! (isa Venus::Process::Error) (see error_on_timeout_sync) | ||
| 4581 | |||
| 4582 | =back | ||
| 4583 | |||
| 4584 | =cut | ||
| 4585 | |||
| 4586 | =head2 trap | ||
| 4587 | |||
| 4588 | trap(string $name, string | coderef $expr) (Venus::Process) | ||
| 4589 | |||
| 4590 | The trap method registers a process signal trap (or callback) which will be | ||
| 4591 | invoked whenever the current process receives that matching signal. The signal | ||
| 4592 | traps are globally installed and will overwrite any preexisting behavior. | ||
| 4593 | Signal traps are inherited by child processes (or forks) but can be overwritten | ||
| 4594 | using this method, or reverted to the default behavior by using the L</untrap> | ||
| 4595 | method. | ||
| 4596 | |||
| 4597 | I<Since C<0.06>> | ||
| 4598 | |||
| 4599 | =over 4 | ||
| 4600 | |||
| 4601 | =item trap example 1 | ||
| 4602 | |||
| 4603 | # given: synopsis; | ||
| 4604 | |||
| 4605 | $parent = $parent->trap(term => sub{ | ||
| 4606 | die 'Something failed!'; | ||
| 4607 | }); | ||
| 4608 | |||
| 4609 | # bless({...}, 'Venus::Process') | ||
| 4610 | |||
| 4611 | =back | ||
| 4612 | |||
| 4613 | =cut | ||
| 4614 | |||
| 4615 | =head2 unregister | ||
| 4616 | |||
| 4617 | unregister() (Venus::Process) | ||
| 4618 | |||
| 4619 | The unregister method declares that the process is no longer willing to | ||
| 4620 | cooperate with others (e.g. L</send> nad L</recv> messages), and will no longer | ||
| 4621 | be discoverable by other processes, and returns the invocant. | ||
| 4622 | |||
| 4623 | I<Since C<2.91>> | ||
| 4624 | |||
| 4625 | =over 4 | ||
| 4626 | |||
| 4627 | =item unregister example 1 | ||
| 4628 | |||
| 4629 | # given: synopsis | ||
| 4630 | |||
| 4631 | package main; | ||
| 4632 | |||
| 4633 | my $unregister = $parent->unregister; | ||
| 4634 | |||
| 4635 | # bless({...}, 'Venus::Process') | ||
| 4636 | |||
| 4637 | =back | ||
| 4638 | |||
| 4639 | =cut | ||
| 4640 | |||
| 4641 | =head2 untrap | ||
| 4642 | |||
| 4643 | untrap(string $name) (Venus::Process) | ||
| 4644 | |||
| 4645 | The untrap method restores the process signal trap specified to its default | ||
| 4646 | behavior. If called with no arguments, it restores all signal traps overwriting | ||
| 4647 | any user-defined signal traps in the current process. | ||
| 4648 | |||
| 4649 | I<Since C<0.06>> | ||
| 4650 | |||
| 4651 | =over 4 | ||
| 4652 | |||
| 4653 | =item untrap example 1 | ||
| 4654 | |||
| 4655 | # given: synopsis; | ||
| 4656 | |||
| 4657 | $parent->trap(chld => 'ignore')->trap(term => sub{ | ||
| 4658 | die 'Something failed!'; | ||
| 4659 | }); | ||
| 4660 | |||
| 4661 | $parent = $parent->untrap('term'); | ||
| 4662 | |||
| 4663 | # bless({...}, 'Venus::Process') | ||
| 4664 | |||
| 4665 | =back | ||
| 4666 | |||
| 4667 | =over 4 | ||
| 4668 | |||
| 4669 | =item untrap example 2 | ||
| 4670 | |||
| 4671 | # given: synopsis; | ||
| 4672 | |||
| 4673 | $parent->trap(chld => 'ignore')->trap(term => sub{ | ||
| 4674 | die 'Something failed!'; | ||
| 4675 | }); | ||
| 4676 | |||
| 4677 | $parent = $parent->untrap; | ||
| 4678 | |||
| 4679 | # bless({...}, 'Venus::Process') | ||
| 4680 | |||
| 4681 | =back | ||
| 4682 | |||
| 4683 | =cut | ||
| 4684 | |||
| 4685 | =head2 unwatch | ||
| 4686 | |||
| 4687 | unwatch(number @pids) (arrayref) | ||
| 4688 | |||
| 4689 | The unwatch method removes the PIDs provided from the watchlist and returns the | ||
| 4690 | list of PIDs remaining to be watched. In list context returns a list. | ||
| 4691 | |||
| 4692 | I<Since C<2.40>> | ||
| 4693 | |||
| 4694 | =over 4 | ||
| 4695 | |||
| 4696 | =item unwatch example 1 | ||
| 4697 | |||
| 4698 | package main; | ||
| 4699 | |||
| 4700 | use Venus::Process; | ||
| 4701 | |||
| 4702 | my $parent = Venus::Process->new; | ||
| 4703 | |||
| 4704 | my $unwatch = $parent->unwatch; | ||
| 4705 | |||
| 4706 | # [] | ||
| 4707 | |||
| 4708 | =back | ||
| 4709 | |||
| 4710 | =over 4 | ||
| 4711 | |||
| 4712 | =item unwatch example 2 | ||
| 4713 | |||
| 4714 | package main; | ||
| 4715 | |||
| 4716 | use Venus::Process; | ||
| 4717 | |||
| 4718 | my $parent = Venus::Process->new; | ||
| 4719 | |||
| 4720 | $parent->watch(1001..1004); | ||
| 4721 | |||
| 4722 | my $unwatch = $parent->unwatch(1001); | ||
| 4723 | |||
| 4724 | # [1002..1004] | ||
| 4725 | |||
| 4726 | =back | ||
| 4727 | |||
| 4728 | =over 4 | ||
| 4729 | |||
| 4730 | =item unwatch example 3 | ||
| 4731 | |||
| 4732 | package main; | ||
| 4733 | |||
| 4734 | use Venus::Process; | ||
| 4735 | |||
| 4736 | my $parent = Venus::Process->new; | ||
| 4737 | |||
| 4738 | $parent->watch(1001..1004); | ||
| 4739 | |||
| 4740 | my $unwatch = $parent->unwatch(1002, 1004); | ||
| 4741 | |||
| 4742 | # [1001, 1003] | ||
| 4743 | |||
| 4744 | =back | ||
| 4745 | |||
| 4746 | =cut | ||
| 4747 | |||
| 4748 | =head2 wait | ||
| 4749 | |||
| 4750 | wait(number $pid) (number, number) | ||
| 4751 | |||
| 4752 | The wait method does a blocking L<perlfunc/waitpid> operation and returns the | ||
| 4753 | wait status. In list context, returns the specified process' exit code (if | ||
| 4754 | terminated). | ||
| 4755 | |||
| 4756 | I<Since C<0.06>> | ||
| 4757 | |||
| 4758 | =over 4 | ||
| 4759 | |||
| 4760 | =item wait example 1 | ||
| 4761 | |||
| 4762 | package main; | ||
| 4763 | |||
| 4764 | use Venus::Process; | ||
| 4765 | |||
| 4766 | my $parent = Venus::Process->new; | ||
| 4767 | |||
| 4768 | my ($process, $pid) = $parent->fork; | ||
| 4769 | |||
| 4770 | if ($process) { | ||
| 4771 | # in forked process ... | ||
| 4772 | $process->exit; | ||
| 4773 | } | ||
| 4774 | |||
| 4775 | my $wait = $parent->wait($pid); | ||
| 4776 | |||
| 4777 | # 0 | ||
| 4778 | |||
| 4779 | =back | ||
| 4780 | |||
| 4781 | =over 4 | ||
| 4782 | |||
| 4783 | =item wait example 2 | ||
| 4784 | |||
| 4785 | package main; | ||
| 4786 | |||
| 4787 | use Venus::Process; | ||
| 4788 | |||
| 4789 | my $parent = Venus::Process->new; | ||
| 4790 | |||
| 4791 | my ($process, $pid) = $parent->fork; | ||
| 4792 | |||
| 4793 | if ($process) { | ||
| 4794 | # in forked process ... | ||
| 4795 | $process->exit; | ||
| 4796 | } | ||
| 4797 | |||
| 4798 | my ($wait, $status) = $parent->wait('00000'); | ||
| 4799 | |||
| 4800 | # (-1, -1) | ||
| 4801 | |||
| 4802 | =back | ||
| 4803 | |||
| 4804 | =over 4 | ||
| 4805 | |||
| 4806 | =item wait example 3 | ||
| 4807 | |||
| 4808 | package main; | ||
| 4809 | |||
| 4810 | use Venus::Process; | ||
| 4811 | |||
| 4812 | my $parent = Venus::Process->new; | ||
| 4813 | |||
| 4814 | my ($process, $pid) = $parent->fork(sub{ $_->exit(1) }); | ||
| 4815 | |||
| 4816 | if ($process) { | ||
| 4817 | # in forked process ... | ||
| 4818 | $process->exit; | ||
| 4819 | } | ||
| 4820 | |||
| 4821 | my ($wait, $status) = $parent->wait($pid); | ||
| 4822 | |||
| 4823 | # ($pid, 1) | ||
| 4824 | |||
| 4825 | =back | ||
| 4826 | |||
| 4827 | =cut | ||
| 4828 | |||
| 4829 | =head2 waitall | ||
| 4830 | |||
| 4831 | waitall(number @pids) (arrayref) | ||
| 4832 | |||
| 4833 | The waitall method does a blocking L</wait> call for all processes based on the | ||
| 4834 | PIDs provided (or the PIDs returned by L</watchlist> if not provided) and | ||
| 4835 | returns an arrayref of results from calling L</wait> on each PID. Returns a | ||
| 4836 | list in list context. | ||
| 4837 | |||
| 4838 | I<Since C<2.40>> | ||
| 4839 | |||
| 4840 | =over 4 | ||
| 4841 | |||
| 4842 | =item waitall example 1 | ||
| 4843 | |||
| 4844 | package main; | ||
| 4845 | |||
| 4846 | use Venus::Process; | ||
| 4847 | |||
| 4848 | my $parent = Venus::Process->new; | ||
| 4849 | |||
| 4850 | my $waitall = $parent->waitall; | ||
| 4851 | |||
| 4852 | # [] | ||
| 4853 | |||
| 4854 | =back | ||
| 4855 | |||
| 4856 | =over 4 | ||
| 4857 | |||
| 4858 | =item waitall example 2 | ||
| 4859 | |||
| 4860 | package main; | ||
| 4861 | |||
| 4862 | use Venus::Process; | ||
| 4863 | |||
| 4864 | my $parent = Venus::Process->new; | ||
| 4865 | |||
| 4866 | my $waitall = $parent->waitall(1001); | ||
| 4867 | |||
| 4868 | # [[1001, 0]] | ||
| 4869 | |||
| 4870 | =back | ||
| 4871 | |||
| 4872 | =over 4 | ||
| 4873 | |||
| 4874 | =item waitall example 3 | ||
| 4875 | |||
| 4876 | package main; | ||
| 4877 | |||
| 4878 | use Venus::Process; | ||
| 4879 | |||
| 4880 | my $parent = Venus::Process->new; | ||
| 4881 | |||
| 4882 | my ($process, $pid) = $parent->fork; | ||
| 4883 | |||
| 4884 | if ($process) { | ||
| 4885 | # in forked process ... | ||
| 4886 | $process->exit; | ||
| 4887 | } | ||
| 4888 | |||
| 4889 | my $waitall = $parent->waitall; | ||
| 4890 | |||
| 4891 | # [[$pid, 0]] | ||
| 4892 | |||
| 4893 | =back | ||
| 4894 | |||
| 4895 | =cut | ||
| 4896 | |||
| 4897 | =head2 watch | ||
| 4898 | |||
| 4899 | watch(number @pids) (arrayref) | ||
| 4900 | |||
| 4901 | The watch method records PIDs to be watched, e.g. using the L</status> method | ||
| 4902 | and returns all PIDs being watched. Returns a list in list context. | ||
| 4903 | |||
| 4904 | I<Since C<2.40>> | ||
| 4905 | |||
| 4906 | =over 4 | ||
| 4907 | |||
| 4908 | =item watch example 1 | ||
| 4909 | |||
| 4910 | package main; | ||
| 4911 | |||
| 4912 | use Venus::Process; | ||
| 4913 | |||
| 4914 | my $parent = Venus::Process->new; | ||
| 4915 | |||
| 4916 | my $watch = $parent->watch; | ||
| 4917 | |||
| 4918 | # [] | ||
| 4919 | |||
| 4920 | =back | ||
| 4921 | |||
| 4922 | =over 4 | ||
| 4923 | |||
| 4924 | =item watch example 2 | ||
| 4925 | |||
| 4926 | package main; | ||
| 4927 | |||
| 4928 | use Venus::Process; | ||
| 4929 | |||
| 4930 | my $parent = Venus::Process->new; | ||
| 4931 | |||
| 4932 | my $watch = $parent->watch(1001..1004); | ||
| 4933 | |||
| 4934 | # [1001..1004] | ||
| 4935 | |||
| 4936 | =back | ||
| 4937 | |||
| 4938 | =over 4 | ||
| 4939 | |||
| 4940 | =item watch example 3 | ||
| 4941 | |||
| 4942 | package main; | ||
| 4943 | |||
| 4944 | use Venus::Process; | ||
| 4945 | |||
| 4946 | my $parent = Venus::Process->new; | ||
| 4947 | |||
| 4948 | my $watch = $parent->watch(1001..1004, 1001..1004); | ||
| 4949 | |||
| 4950 | # [1001..1004] | ||
| 4951 | |||
| 4952 | =back | ||
| 4953 | |||
| 4954 | =over 4 | ||
| 4955 | |||
| 4956 | =item watch example 4 | ||
| 4957 | |||
| 4958 | package main; | ||
| 4959 | |||
| 4960 | use Venus::Process; | ||
| 4961 | |||
| 4962 | my $parent = Venus::Process->new; | ||
| 4963 | |||
| 4964 | $parent->watch(1001..1004); | ||
| 4965 | |||
| 4966 | my $watch = $parent->watch; | ||
| 4967 | |||
| 4968 | # [1001..1004] | ||
| 4969 | |||
| 4970 | =back | ||
| 4971 | |||
| 4972 | =over 4 | ||
| 4973 | |||
| 4974 | =item watch example 5 | ||
| 4975 | |||
| 4976 | package main; | ||
| 4977 | |||
| 4978 | use Venus::Process; | ||
| 4979 | |||
| 4980 | my $parent = Venus::Process->new; | ||
| 4981 | |||
| 4982 | my @watch = $parent->watch(1001..1004); | ||
| 4983 | |||
| 4984 | # (1001..1004) | ||
| 4985 | |||
| 4986 | =back | ||
| 4987 | |||
| 4988 | =cut | ||
| 4989 | |||
| 4990 | =head2 watchlist | ||
| 4991 | |||
| 4992 | watchlist() (arrayref) | ||
| 4993 | |||
| 4994 | The watchlist method returns the recorded PIDs. Returns a list in list context. | ||
| 4995 | |||
| 4996 | I<Since C<2.40>> | ||
| 4997 | |||
| 4998 | =over 4 | ||
| 4999 | |||
| 5000 | =item watchlist example 1 | ||
| 5001 | |||
| 5002 | package main; | ||
| 5003 | |||
| 5004 | use Venus::Process; | ||
| 5005 | |||
| 5006 | my $parent = Venus::Process->new; | ||
| 5007 | |||
| 5008 | my $watchlist = $parent->watchlist; | ||
| 5009 | |||
| 5010 | # [] | ||
| 5011 | |||
| 5012 | =back | ||
| 5013 | |||
| 5014 | =over 4 | ||
| 5015 | |||
| 5016 | =item watchlist example 2 | ||
| 5017 | |||
| 5018 | package main; | ||
| 5019 | |||
| 5020 | use Venus::Process; | ||
| 5021 | |||
| 5022 | my $parent = Venus::Process->new; | ||
| 5023 | |||
| 5024 | $parent->watch(1001..1004); | ||
| 5025 | |||
| 5026 | my $watchlist = $parent->watchlist; | ||
| 5027 | |||
| 5028 | # [1001..1004] | ||
| 5029 | |||
| 5030 | =back | ||
| 5031 | |||
| 5032 | =cut | ||
| 5033 | |||
| 5034 | =head2 work | ||
| 5035 | |||
| 5036 | work(string | coderef $code, any @args) (number) | ||
| 5037 | |||
| 5038 | The work method forks the current process, runs the callback provided in the | ||
| 5039 | child process, and immediately exits after. This method returns the I<PID> of | ||
| 5040 | the child process. It is recommended to install an L<perlfunc/alarm> in the | ||
| 5041 | child process (i.e. callback) to avoid creating zombie processes in situations | ||
| 5042 | where the parent process might exit before the child process is done working. | ||
| 5043 | |||
| 5044 | I<Since C<0.06>> | ||
| 5045 | |||
| 5046 | =over 4 | ||
| 5047 | |||
| 5048 | =item work example 1 | ||
| 5049 | |||
| 5050 | # given: synopsis; | ||
| 5051 | |||
| 5052 | my $pid = $parent->work(sub{ | ||
| 5053 | my ($process) = @_; | ||
| 5054 | # in forked process ... | ||
| 5055 | $process->exit; | ||
| 5056 | }); | ||
| 5057 | |||
| 5058 | # $pid | ||
| 5059 | |||
| 5060 | =back | ||
| 5061 | |||
| 5062 | =cut | ||
| 5063 | |||
| 5064 | =head2 works | ||
| 5065 | |||
| 5066 | works(number $count, coderef $callback, any @args) (arrayref) | ||
| 5067 | |||
| 5068 | The works method creates multiple forks by calling the L</work> method C<n> | ||
| 5069 | times, based on the count specified. The works method runs the callback | ||
| 5070 | provided in the child process, and immediately exits after with an exit code of | ||
| 5071 | C<0> by default. This method returns the I<PIDs> of the child processes. It is | ||
| 5072 | recommended to install an L<perlfunc/alarm> in the child process (i.e. | ||
| 5073 | callback) to avoid creating zombie processes in situations where the parent | ||
| 5074 | process might exit before the child process is done working. | ||
| 5075 | |||
| 5076 | I<Since C<2.40>> | ||
| 5077 | |||
| 5078 | =over 4 | ||
| 5079 | |||
| 5080 | =item works example 1 | ||
| 5081 | |||
| 5082 | # given: synopsis; | ||
| 5083 | |||
| 5084 | my $pids = $parent->works(5, sub{ | ||
| 5085 | my ($process) = @_; | ||
| 5086 | # in forked process ... | ||
| 5087 | $process->exit; | ||
| 5088 | }); | ||
| 5089 | |||
| 5090 | # $pids | ||
| 5091 | |||
| 5092 | =back | ||
| 5093 | |||
| 5094 | =cut | ||
| 5095 | |||
| 5096 | =head1 ERRORS | ||
| 5097 | |||
| 5098 | This package may raise the following errors: | ||
| 5099 | |||
| 5100 | =cut | ||
| 5101 | |||
| 5102 | =over 4 | ||
| 5103 | |||
| 5104 | =item error: C<error_on_chdir> | ||
| 5105 | |||
| 5106 | This package may raise an error_on_chdir exception. | ||
| 5107 | |||
| 5108 | B<example 1> | ||
| 5109 | |||
| 5110 | # given: synopsis; | ||
| 5111 | |||
| 5112 | my $input = { | ||
| 5113 | throw => 'error_on_chdir', | ||
| 5114 | error => $!, | ||
| 5115 | path => '/nowhere', | ||
| 5116 | pid => 123, | ||
| 5117 | }; | ||
| 5118 | |||
| 5119 | my $error = $parent->catch('error', $input); | ||
| 5120 | |||
| 5121 | # my $name = $error->name; | ||
| 5122 | |||
| 5123 | # "on_chdir" | ||
| 5124 | |||
| 5125 | # my $message = $error->render; | ||
| 5126 | |||
| 5127 | # "Can't chdir \"$path\": $!" | ||
| 5128 | |||
| 5129 | # my $path = $error->stash('path'); | ||
| 5130 | |||
| 5131 | # "/nowhere" | ||
| 5132 | |||
| 5133 | # my $pid = $error->stash('pid'); | ||
| 5134 | |||
| 5135 | # 123 | ||
| 5136 | |||
| 5137 | =back | ||
| 5138 | |||
| 5139 | =over 4 | ||
| 5140 | |||
| 5141 | =item error: C<error_on_fork_process> | ||
| 5142 | |||
| 5143 | This package may raise an error_on_fork_process exception. | ||
| 5144 | |||
| 5145 | B<example 1> | ||
| 5146 | |||
| 5147 | # given: synopsis; | ||
| 5148 | |||
| 5149 | my $input = { | ||
| 5150 | throw => 'error_on_fork_process', | ||
| 5151 | error => $!, | ||
| 5152 | pid => 123, | ||
| 5153 | }; | ||
| 5154 | |||
| 5155 | my $error = $parent->catch('error', $input); | ||
| 5156 | |||
| 5157 | # my $name = $error->name; | ||
| 5158 | |||
| 5159 | # "on_fork_process" | ||
| 5160 | |||
| 5161 | # my $message = $error->render; | ||
| 5162 | |||
| 5163 | # "Can't fork process $pid: $!" | ||
| 5164 | |||
| 5165 | # my $pid = $error->stash('pid'); | ||
| 5166 | |||
| 5167 | # "123" | ||
| 5168 | |||
| 5169 | =back | ||
| 5170 | |||
| 5171 | =over 4 | ||
| 5172 | |||
| 5173 | =item error: C<error_on_fork_support> | ||
| 5174 | |||
| 5175 | This package may raise an error_on_fork_support exception. | ||
| 5176 | |||
| 5177 | B<example 1> | ||
| 5178 | |||
| 5179 | # given: synopsis; | ||
| 5180 | |||
| 5181 | my $input = { | ||
| 5182 | throw => 'error_on_fork_support', | ||
| 5183 | pid => 123, | ||
| 5184 | }; | ||
| 5185 | |||
| 5186 | my $error = $parent->catch('error', $input); | ||
| 5187 | |||
| 5188 | # my $name = $error->name; | ||
| 5189 | |||
| 5190 | # "on_fork_support" | ||
| 5191 | |||
| 5192 | # my $message = $error->render; | ||
| 5193 | |||
| 5194 | # "Can't fork process $pid: Fork emulation not supported" | ||
| 5195 | |||
| 5196 | # my $pid = $error->stash('pid'); | ||
| 5197 | |||
| 5198 | # 123 | ||
| 5199 | |||
| 5200 | =back | ||
| 5201 | |||
| 5202 | =over 4 | ||
| 5203 | |||
| 5204 | =item error: C<error_on_ping> | ||
| 5205 | |||
| 5206 | This package may raise an error_on_ping exception. | ||
| 5207 | |||
| 5208 | B<example 1> | ||
| 5209 | |||
| 5210 | # given: synopsis; | ||
| 5211 | |||
| 5212 | my $input = { | ||
| 5213 | throw => 'error_on_ping', | ||
| 5214 | pid => 123, | ||
| 5215 | }; | ||
| 5216 | |||
| 5217 | my $error = $parent->catch('error', $input); | ||
| 5218 | |||
| 5219 | # my $name = $error->name; | ||
| 5220 | |||
| 5221 | # "on_ping" | ||
| 5222 | |||
| 5223 | # my $message = $error->render; | ||
| 5224 | |||
| 5225 | # "Process 123 not responding to ping" | ||
| 5226 | |||
| 5227 | # my $pid = $error->stash('pid'); | ||
| 5228 | |||
| 5229 | # "123" | ||
| 5230 | |||
| 5231 | =back | ||
| 5232 | |||
| 5233 | =over 4 | ||
| 5234 | |||
| 5235 | =item error: C<error_on_setid> | ||
| 5236 | |||
| 5237 | This package may raise an error_on_setid exception. | ||
| 5238 | |||
| 5239 | B<example 1> | ||
| 5240 | |||
| 5241 | # given: synopsis; | ||
| 5242 | |||
| 5243 | my $input = { | ||
| 5244 | throw => 'error_on_setid', | ||
| 5245 | error => $!, | ||
| 5246 | pid => 123, | ||
| 5247 | }; | ||
| 5248 | |||
| 5249 | my $error = $parent->catch('error', $input); | ||
| 5250 | |||
| 5251 | # my $name = $error->name; | ||
| 5252 | |||
| 5253 | # "on_setid" | ||
| 5254 | |||
| 5255 | # my $message = $error->render; | ||
| 5256 | |||
| 5257 | # "Can't start a new session: $!" | ||
| 5258 | |||
| 5259 | # my $pid = $error->stash('pid'); | ||
| 5260 | |||
| 5261 | # 123 | ||
| 5262 | |||
| 5263 | =back | ||
| 5264 | |||
| 5265 | =over 4 | ||
| 5266 | |||
| 5267 | =item error: C<error_on_stderr> | ||
| 5268 | |||
| 5269 | This package may raise an error_on_stderr exception. | ||
| 5270 | |||
| 5271 | B<example 1> | ||
| 5272 | |||
| 5273 | # given: synopsis; | ||
| 5274 | |||
| 5275 | my $input = { | ||
| 5276 | throw => 'error_on_stderr', | ||
| 5277 | error => $!, | ||
| 5278 | path => "/nowhere", | ||
| 5279 | pid => 123, | ||
| 5280 | }; | ||
| 5281 | |||
| 5282 | my $error = $parent->catch('error', $input); | ||
| 5283 | |||
| 5284 | # my $name = $error->name; | ||
| 5285 | |||
| 5286 | # "on_stderr" | ||
| 5287 | |||
| 5288 | # my $message = $error->render; | ||
| 5289 | |||
| 5290 | # "Can't redirect STDERR to \"/nowhere\": $!" | ||
| 5291 | |||
| 5292 | # my $path = $error->stash('path'); | ||
| 5293 | |||
| 5294 | # "/nowhere" | ||
| 5295 | |||
| 5296 | # my $pid = $error->stash('pid'); | ||
| 5297 | |||
| 5298 | # 123 | ||
| 5299 | |||
| 5300 | =back | ||
| 5301 | |||
| 5302 | =over 4 | ||
| 5303 | |||
| 5304 | =item error: C<error_on_stdin> | ||
| 5305 | |||
| 5306 | This package may raise an error_on_stdin exception. | ||
| 5307 | |||
| 5308 | B<example 1> | ||
| 5309 | |||
| 5310 | # given: synopsis; | ||
| 5311 | |||
| 5312 | my $input = { | ||
| 5313 | throw => 'error_on_stdin', | ||
| 5314 | error => $!, | ||
| 5315 | path => "/nowhere", | ||
| 5316 | pid => 123, | ||
| 5317 | }; | ||
| 5318 | |||
| 5319 | my $error = $parent->catch('error', $input); | ||
| 5320 | |||
| 5321 | # my $name = $error->name; | ||
| 5322 | |||
| 5323 | # "on_stdin" | ||
| 5324 | |||
| 5325 | # my $message = $error->render; | ||
| 5326 | |||
| 5327 | # "Can't redirect STDIN to \"$path\": $!" | ||
| 5328 | |||
| 5329 | # my $path = $error->stash('path'); | ||
| 5330 | |||
| 5331 | # "/nowhere" | ||
| 5332 | |||
| 5333 | # my $pid = $error->stash('pid'); | ||
| 5334 | |||
| 5335 | # 123 | ||
| 5336 | |||
| 5337 | =back | ||
| 5338 | |||
| 5339 | =over 4 | ||
| 5340 | |||
| 5341 | =item error: C<error_on_stdout> | ||
| 5342 | |||
| 5343 | This package may raise an error_on_stdout exception. | ||
| 5344 | |||
| 5345 | B<example 1> | ||
| 5346 | |||
| 5347 | # given: synopsis; | ||
| 5348 | |||
| 5349 | my $input = { | ||
| 5350 | throw => 'error_on_stdout', | ||
| 5351 | error => $!, | ||
| 5352 | path => "/nowhere", | ||
| 5353 | pid => 123, | ||
| 5354 | }; | ||
| 5355 | |||
| 5356 | my $error = $parent->catch('error', $input); | ||
| 5357 | |||
| 5358 | # my $name = $error->name; | ||
| 5359 | |||
| 5360 | # "on_stdout" | ||
| 5361 | |||
| 5362 | # my $message = $error->render; | ||
| 5363 | |||
| 5364 | # "Can't redirect STDOUT to \"$path\": $!" | ||
| 5365 | |||
| 5366 | # my $path = $error->stash('path'); | ||
| 5367 | |||
| 5368 | # "/nowhere" | ||
| 5369 | |||
| 5370 | # my $pid = $error->stash('pid'); | ||
| 5371 | |||
| 5372 | # 123 | ||
| 5373 | |||
| 5374 | =back | ||
| 5375 | |||
| 5376 | =over 4 | ||
| 5377 | |||
| 5378 | =item error: C<error_on_timeout_poll> | ||
| 5379 | |||
| 5380 | This package may raise an error_on_timeout_poll exception. | ||
| 5381 | |||
| 5382 | B<example 1> | ||
| 5383 | |||
| 5384 | # given: synopsis; | ||
| 5385 | |||
| 5386 | my $input = { | ||
| 5387 | throw => 'error_on_timeout_poll', | ||
| 5388 | code => sub{}, | ||
| 5389 | timeout => 0, | ||
| 5390 | }; | ||
| 5391 | |||
| 5392 | my $error = $parent->catch('error', $input); | ||
| 5393 | |||
| 5394 | # my $name = $error->name; | ||
| 5395 | |||
| 5396 | # "on_timeout_poll" | ||
| 5397 | |||
| 5398 | # my $message = $error->render; | ||
| 5399 | |||
| 5400 | # "Timed out after 0 seconds in process 12345 while polling __ANON__" | ||
| 5401 | |||
| 5402 | # my $code = $error->stash('code'); | ||
| 5403 | |||
| 5404 | # sub{} | ||
| 5405 | |||
| 5406 | # my $exchange = $error->stash('exchange'); | ||
| 5407 | |||
| 5408 | # undef | ||
| 5409 | |||
| 5410 | # my $pid = $error->stash('pid'); | ||
| 5411 | |||
| 5412 | # 12345 | ||
| 5413 | |||
| 5414 | # my $timeout = $error->stash('timeout'); | ||
| 5415 | |||
| 5416 | # 0 | ||
| 5417 | |||
| 5418 | =back | ||
| 5419 | |||
| 5420 | =over 4 | ||
| 5421 | |||
| 5422 | =item error: C<error_on_timeout_pool> | ||
| 5423 | |||
| 5424 | This package may raise an error_on_timeout_pool exception. | ||
| 5425 | |||
| 5426 | B<example 1> | ||
| 5427 | |||
| 5428 | # given: synopsis; | ||
| 5429 | |||
| 5430 | my $input = { | ||
| 5431 | throw => 'error_on_timeout_pool', | ||
| 5432 | pool_size => 2, | ||
| 5433 | timeout => 0, | ||
| 5434 | }; | ||
| 5435 | |||
| 5436 | my $error = $parent->catch('error', $input); | ||
| 5437 | |||
| 5438 | # my $name = $error->name; | ||
| 5439 | |||
| 5440 | # "on_timeout_pool" | ||
| 5441 | |||
| 5442 | # my $message = $error->render; | ||
| 5443 | |||
| 5444 | # "Timed out after 0 seconds in process 12345 while pooling" | ||
| 5445 | |||
| 5446 | # my $exchange = $error->stash('exchange'); | ||
| 5447 | |||
| 5448 | # undef | ||
| 5449 | |||
| 5450 | # my $pid = $error->stash('pid'); | ||
| 5451 | |||
| 5452 | # 12345 | ||
| 5453 | |||
| 5454 | # my $pool_size = $error->stash('pool_size'); | ||
| 5455 | |||
| 5456 | # 2 | ||
| 5457 | |||
| 5458 | # my $timeout = $error->stash('timeout'); | ||
| 5459 | |||
| 5460 | # 0 | ||
| 5461 | |||
| 5462 | =back | ||
| 5463 | |||
| 5464 | =over 4 | ||
| 5465 | |||
| 5466 | =item error: C<error_on_timeout_sync> | ||
| 5467 | |||
| 5468 | This package may raise an error_on_timeout_sync exception. | ||
| 5469 | |||
| 5470 | B<example 1> | ||
| 5471 | |||
| 5472 | # given: synopsis; | ||
| 5473 | |||
| 5474 | my $input = { | ||
| 5475 | throw => 'error_on_timeout_sync', | ||
| 5476 | pool_size => 2, | ||
| 5477 | timeout => 0, | ||
| 5478 | }; | ||
| 5479 | |||
| 5480 | my $error = $parent->catch('error', $input); | ||
| 5481 | |||
| 5482 | # my $name = $error->name; | ||
| 5483 | |||
| 5484 | # "on_timeout_sync" | ||
| 5485 | |||
| 5486 | # my $message = $error->render; | ||
| 5487 | |||
| 5488 | # "Timed out after 0 seconds in process 12345 while syncing" | ||
| 5489 | |||
| 5490 | # my $exchange = $error->stash('exchange'); | ||
| 5491 | |||
| 5492 | # undef | ||
| 5493 | |||
| 5494 | # my $pid = $error->stash('pid'); | ||
| 5495 | |||
| 5496 | # 12345 | ||
| 5497 | |||
| 5498 | # my $pool_size = $error->stash('pool_size'); | ||
| 5499 | |||
| 5500 | # 2 | ||
| 5501 | |||
| 5502 | # my $timeout = $error->stash('timeout'); | ||
| 5503 | |||
| 5504 | # 0 | ||
| 5505 | |||
| 5506 | =back | ||
| 5507 | |||
| 5508 | =head1 OPERATORS | ||
| 5509 | |||
| 5510 | This package overloads the following operators: | ||
| 5511 | |||
| 5512 | =cut | ||
| 5513 | |||
| 5514 | =over 4 | ||
| 5515 | |||
| 5516 | =item operation: C<("")> | ||
| 5517 | |||
| 5518 | This package overloads the C<""> operator. | ||
| 5519 | |||
| 5520 | B<example 1> | ||
| 5521 | |||
| 5522 | # given: synopsis; | ||
| 5523 | |||
| 5524 | my $result = "$parent"; | ||
| 5525 | |||
| 5526 | # $pid | ||
| 5527 | |||
| 5528 | =back | ||
| 5529 | |||
| 5530 | =over 4 | ||
| 5531 | |||
| 5532 | =item operation: C<(~~)> | ||
| 5533 | |||
| 5534 | This package overloads the C<~~> operator. | ||
| 5535 | |||
| 5536 | B<example 1> | ||
| 5537 | |||
| 5538 | # given: synopsis; | ||
| 5539 | |||
| 5540 | my $result = $parent ~~ /^\d+$/; | ||
| 5541 | |||
| 5542 | # 1 | ||
| 5543 | |||
| 5544 | =back | ||
| 5545 | |||
| 5546 | =head1 AUTHORS | ||
| 5547 | |||
| 5548 | Awncorp, C<awncorp@cpan.org> | ||
| 5549 | |||
| 5550 | =cut | ||
| 5551 | |||
| 5552 | =head1 LICENSE | ||
| 5553 | |||
| 5554 | Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>. | ||
| 5555 | |||
| 5556 | This program is free software, you can redistribute it and/or modify it under | ||
| 5557 | the terms of the Apache license version 2.0. | ||
| 5558 | |||
| 5559 | =cut |