Skip to content

Instantly share code, notes, and snippets.

@brianmed
Created April 1, 2016 01:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianmed/0e2cb254cf6f35b1f6587b1fb320eb73 to your computer and use it in GitHub Desktop.
Save brianmed/0e2cb254cf6f35b1f6587b1fb320eb73 to your computer and use it in GitHub Desktop.
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