/usr/share/perl5/Plack/Loader/Restarter.pm is in libplack-perl 1.0047-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 | package Plack::Loader::Restarter;
use strict;
use warnings;
use parent qw(Plack::Loader);
use Plack::Util;
use Try::Tiny;
sub new {
my($class, $runner) = @_;
bless { watch => [] }, $class;
}
sub preload_app {
my($self, $builder) = @_;
$self->{builder} = $builder;
}
sub watch {
my($self, @dir) = @_;
push @{$self->{watch}}, @dir;
}
sub _fork_and_start {
my($self, $server) = @_;
delete $self->{pid}; # re-init in case it's a restart
my $pid = fork;
die "Can't fork: $!" unless defined $pid;
if ($pid == 0) { # child
return $server->run($self->{builder}->());
} else {
$self->{pid} = $pid;
}
}
sub _kill_child {
my $self = shift;
my $pid = $self->{pid} or return;
warn "Killing the existing server (pid:$pid)\n";
kill 'TERM' => $pid;
waitpid($pid, 0);
}
sub valid_file {
my($self, $file) = @_;
# vim temporary file is 4913 to 5036
# http://www.mail-archive.com/vim_dev@googlegroups.com/msg07518.html
if ( $file->{path} =~ m{(\d+)$} && $1 >= 4913 && $1 <= 5036) {
return 0;
}
$file->{path} !~ m!\.(?:git|svn)[/\\]|\.(?:bak|swp|swpx|swx)$|~$|_flymake\.p[lm]$|\.#!;
}
sub run {
my($self, $server) = @_;
$self->_fork_and_start($server);
return unless $self->{pid};
require Filesys::Notify::Simple;
my $watcher = Filesys::Notify::Simple->new($self->{watch});
warn "Watching @{$self->{watch}} for file updates.\n";
local $SIG{TERM} = sub { $self->_kill_child; exit(0); };
while (1) {
my @restart;
# this is blocking
$watcher->wait(sub {
my @events = @_;
@events = grep $self->valid_file($_), @events;
return unless @events;
@restart = @events;
});
next unless @restart;
for my $ev (@restart) {
warn "-- $ev->{path} updated.\n";
}
$self->_kill_child;
warn "Successfully killed! Restarting the new server process.\n";
$self->_fork_and_start($server);
return unless $self->{pid};
}
}
1;
__END__
=head1 NAME
Plack::Loader::Restarter - Restarting loader
=head1 SYNOPSIS
plackup -r -R paths
=head1 DESCRIPTION
Plack::Loader::Restarter is a loader backend that implements C<-r> and
C<-R> option for the L<plackup> script. It forks the server as a child
process and the parent watches the directories for file updates, and
whenever it receives the notification, kills the child server and
restart.
=head1 SEE ALSO
L<Plack::Runner>, L<Catalyst::Restarter>
=cut
|