Created
April 1, 2016 01:54
-
-
Save brianmed/0e2cb254cf6f35b1f6587b1fb320eb73 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Mojo::Base 'Mojolicious'; | |
use Mojo::Server; | |
use File::Basename qw(basename); | |
use Scalar::Util qw(openhandle); | |
use IO::Poll qw(POLLERR POLLHUP POLLPRI POLLNVAL); | |
use POSIX qw(setsid); | |
our $_parent; | |
our $_child; | |
my $app = __PACKAGE__->new(moniker => lc(basename($0, (".pl")))); | |
$app->log->path("log/development.log"); | |
$app->log->level("debug"); | |
# File handle for child communication | |
pipe($_parent, $_child) or die "Can't create pipe: $!"; | |
$_parent->autoflush(1); | |
$_child->autoflush(1); | |
die "Can't fork: $!" unless defined(my $pid = fork); | |
if ($pid) { | |
close($_parent); | |
while (1) { | |
$app->log->debug("Parent: $$: 0.25"); | |
select(undef, undef, undef, 0.25); | |
} | |
exit; | |
} | |
POSIX::setsid or die "Can't start a new session: $!"; | |
close($_child); | |
my $poll = IO::Poll->new; | |
$poll->mask($_parent => POLLPRI|POLLNVAL|POLLERR|POLLHUP); | |
while (1) { | |
$poll->poll(0.25); | |
my @stuff = $poll->handles(POLLNVAL|POLLHUP|POLLERR); | |
$app->log->debug("Child: $$: 0.25: " . scalar(@stuff)); | |
$app->log->debug("Child: $$: " . (openhandle($_parent) // "-1")); | |
foreach my $handle ($poll->handles) { | |
foreach my $events ($poll->events($handle)) { | |
$app->log->debug("Child: $$: events: $events"); | |
if ($events & (POLLHUP|POLLERR) ) { | |
$app->log->debug("Child: exit"); | |
exit(0); | |
} | |
} | |
} | |
} | |
exit(0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment