rename *-daemon apps to be *-backend
Squashed commit of the following: commit39b438aa4bAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat May 6 16:40:11 2017 +0100 add release notes commitca4ea90d35Author: Oliver Gorwits <oliver@cpan.org> Date: Sat May 6 16:32:06 2017 +0100 update distmeta commit4e35b904b0Author: Oliver Gorwits <oliver@cpan.org> Date: Sat May 6 16:30:22 2017 +0100 rename files from Daemon to Backend commit86a605ba68Author: Oliver Gorwits <oliver@cpan.org> Date: Sat May 6 16:26:43 2017 +0100 rename daemon to backend in code commitffe8fc180fAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat May 6 16:15:57 2017 +0100 add daemon files which exec to backend equivalents commit53e041594eAuthor: Oliver Gorwits <oliver@cpan.org> Date: Sat May 6 15:32:49 2017 +0100 rename netdisco-daemon to netdisco-backend
This commit is contained in:
@@ -3,184 +3,17 @@
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
our $home;
|
||||
use File::Spec;
|
||||
use Cwd 'realpath';
|
||||
|
||||
BEGIN {
|
||||
use FindBin;
|
||||
FindBin::again();
|
||||
use FindBin;
|
||||
FindBin::again();
|
||||
|
||||
my $me = File::Spec->catfile($FindBin::RealBin, $FindBin::RealScript);
|
||||
my $uid = (stat($me))[4] || 0;
|
||||
my $backend =
|
||||
File::Spec->catfile(
|
||||
(File::Spec->splitpath(
|
||||
realpath(File::Spec->catfile( $FindBin::RealBin, $FindBin::RealScript ))
|
||||
))[1],
|
||||
'netdisco-backend');
|
||||
|
||||
$home = ($ENV{NETDISCO_HOME} || (getpwuid($uid))[7] || $ENV{HOME});
|
||||
|
||||
# try to find a localenv if one isn't already in place.
|
||||
if (!exists $ENV{PERL_LOCAL_LIB_ROOT}) {
|
||||
use File::Spec;
|
||||
my $localenv = File::Spec->catfile($FindBin::Bin, 'localenv');
|
||||
exec($localenv, $0, @ARGV) if -f $localenv;
|
||||
$localenv = File::Spec->catfile($home, 'perl5', 'bin', 'localenv');
|
||||
exec($localenv, $0, @ARGV) if -f $localenv;
|
||||
|
||||
die "Sorry, can't find libs required for App::Netdisco.\n"
|
||||
if !exists $ENV{PERLBREW_PERL};
|
||||
}
|
||||
}
|
||||
|
||||
BEGIN {
|
||||
use Path::Class;
|
||||
|
||||
# stuff useful locations into @INC and $PATH
|
||||
unshift @INC,
|
||||
dir($FindBin::RealBin)->parent->subdir('lib')->stringify,
|
||||
dir($FindBin::RealBin, 'lib')->stringify;
|
||||
|
||||
use Config;
|
||||
$ENV{PATH} = $FindBin::RealBin . $Config{path_sep} . $ENV{PATH};
|
||||
}
|
||||
|
||||
use Daemon::Control;
|
||||
use Filesys::Notify::Simple;
|
||||
use File::Copy;
|
||||
|
||||
use App::Netdisco::Environment;
|
||||
my $config = ($ENV{PLACK_ENV} || $ENV{DANCER_ENVIRONMENT}) .'.yml';
|
||||
|
||||
my $netdisco = file($FindBin::RealBin, 'netdisco-daemon-fg');
|
||||
my @args = (scalar @ARGV > 1 ? @ARGV[1 .. $#ARGV] : ());
|
||||
|
||||
my $log_dir = dir($home, 'logs');
|
||||
mkdir $log_dir if ! -d $log_dir;
|
||||
my $log_file = file($log_dir, 'netdisco-daemon.log');
|
||||
|
||||
my $uid = (stat($netdisco->stringify))[4] || 0;
|
||||
my $gid = (stat($netdisco->stringify))[5] || 0;
|
||||
|
||||
Daemon::Control->new({
|
||||
name => 'Netdisco Daemon',
|
||||
program => \&restarter,
|
||||
program_args => [@args],
|
||||
pid_file => file($home, 'netdisco-daemon.pid'),
|
||||
stderr_file => $log_file,
|
||||
stdout_file => $log_file,
|
||||
redirect_before_fork => 0,
|
||||
uid => $uid, gid => $gid,
|
||||
})->run;
|
||||
|
||||
# the guts of this are borrowed from Plack::Loader::Restarter - many thanks!!
|
||||
my $child = 0;
|
||||
|
||||
sub restarter {
|
||||
my ($daemon, @program_args) = @_;
|
||||
$0 = 'netdisco-daemon';
|
||||
|
||||
$child = fork_and_start($daemon, @program_args);
|
||||
exit(1) unless $child;
|
||||
|
||||
my $watcher = Filesys::Notify::Simple->new([$ENV{DANCER_ENVDIR}, $log_dir]);
|
||||
warn "config watcher: watching $ENV{DANCER_ENVDIR} for updates.\n";
|
||||
|
||||
local $SIG{TERM} = sub { $child = signal_child('TERM', $child); exit(0); };
|
||||
|
||||
while (1) {
|
||||
my @restart;
|
||||
|
||||
# this is blocking
|
||||
$watcher->wait(sub {
|
||||
my @events = @_;
|
||||
@events = grep {$_->{path} eq $log_file or
|
||||
file($_->{path})->basename eq $config} @events;
|
||||
return unless @events;
|
||||
@restart = @events;
|
||||
});
|
||||
|
||||
my ($hupit, $rotate) = (0, 0);
|
||||
next unless @restart;
|
||||
|
||||
foreach my $f (@restart) {
|
||||
if ($f->{path} eq $log_file) {
|
||||
++$rotate;
|
||||
}
|
||||
else {
|
||||
warn "-- $f->{path} updated.\n";
|
||||
++$hupit;
|
||||
}
|
||||
}
|
||||
|
||||
rotate_logs($child, $daemon, @program_args) if $rotate;
|
||||
if ($hupit) {
|
||||
signal_child('TERM', $child);
|
||||
$child = fork_and_start($daemon, @program_args);
|
||||
exit(1) unless $child;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
sub fork_and_start {
|
||||
my ($daemon, @daemon_args) = @_;
|
||||
my $pid = fork;
|
||||
die "Can't fork: $!" unless defined $pid;
|
||||
|
||||
if ($pid == 0) { # child
|
||||
$daemon->redirect_filehandles;
|
||||
exec( $netdisco->stringify, @daemon_args );
|
||||
}
|
||||
else {
|
||||
return $pid;
|
||||
}
|
||||
}
|
||||
|
||||
sub signal_child {
|
||||
my ($signal, $pid) = @_;
|
||||
return unless $signal and $pid;
|
||||
warn "config watcher: sending $signal to the server (pid:$pid)...\n";
|
||||
kill $signal => $pid;
|
||||
waitpid($pid, 0);
|
||||
}
|
||||
|
||||
sub rotate_logs {
|
||||
my $child = shift;
|
||||
|
||||
return unless (-f $log_file) and
|
||||
((-s $log_file) > (10 * 1024768));
|
||||
|
||||
my @files = glob file($log_dir, '*');
|
||||
foreach my $f (reverse sort @files) {
|
||||
next unless $f =~ m/$log_file\.(\d)$/;
|
||||
my $pos = $1;
|
||||
unlink $f if $pos == 7;
|
||||
my $next = $pos + 1;
|
||||
(my $newf = $f) =~ s/\.$pos$/.$next/;
|
||||
rename $f, $newf;
|
||||
}
|
||||
|
||||
# if the log file's about 10M then the race condition in copy/truncate
|
||||
# has a low risk of data loss. if the file's larger, then we rename and
|
||||
# kill.
|
||||
if ((-s $log_file) > (12 * 1024768)) {
|
||||
rename $log_file, $log_file .'.1';
|
||||
signal_child('TERM', $child);
|
||||
$child = fork_and_start(@_);
|
||||
exit(1) unless $child;
|
||||
}
|
||||
else {
|
||||
copy $log_file, $log_file .'.1';
|
||||
truncate $log_file, 0;
|
||||
}
|
||||
}
|
||||
|
||||
=head1 NAME
|
||||
|
||||
netdisco-daemon - Job Control Daemon for Netdisco
|
||||
|
||||
=head1 SEE ALSO
|
||||
|
||||
=over 4
|
||||
|
||||
=item *
|
||||
|
||||
L<App::Netdisco>
|
||||
|
||||
=back
|
||||
|
||||
=cut
|
||||
exec {$backend} 'netdisco-backend', @ARGV;
|
||||
|
||||
Reference in New Issue
Block a user