From 8305f9bf0b8d916428eff067a6fde7dca89f9681 Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Sat, 24 Jan 2015 18:59:08 +0000 Subject: [PATCH] [#171] Log files now rotate at 10MB up to seven times --- Netdisco/Changes | 6 ++++ Netdisco/bin/netdisco-daemon | 67 ++++++++++++++++++++++++++++++------ Netdisco/bin/netdisco-web | 49 +++++++++++++++++++++++--- 3 files changed, 108 insertions(+), 14 deletions(-) diff --git a/Netdisco/Changes b/Netdisco/Changes index 4a2fccf3..eb9227e6 100644 --- a/Netdisco/Changes +++ b/Netdisco/Changes @@ -1,3 +1,9 @@ +2.030001 - 2015-01-08 + + [NEW FEATURES] + + * [#171] Log files now rotate at 10MB up to seven times + 2.030000 - 2015-01-08 [NEW FEATURES] diff --git a/Netdisco/bin/netdisco-daemon b/Netdisco/bin/netdisco-daemon index 6e953c78..70a746d9 100755 --- a/Netdisco/bin/netdisco-daemon +++ b/Netdisco/bin/netdisco-daemon @@ -41,6 +41,7 @@ BEGIN { use Daemon::Control; use Filesys::Notify::Simple; +use File::Copy; use App::Netdisco::Environment; my $config = ($ENV{PLACK_ENV} || $ENV{DANCER_ENVIRONMENT}) .'.yml'; @@ -50,6 +51,7 @@ 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; @@ -59,25 +61,26 @@ Daemon::Control->new({ program => \&restarter, program_args => [@args], pid_file => file($home, 'netdisco-daemon.pid'), - stderr_file => file($log_dir, 'netdisco-daemon.log'), - stdout_file => file($log_dir, 'netdisco-daemon.log'), + 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'; - my $child = fork_and_start($daemon, @program_args); + $child = fork_and_start($daemon, @program_args); exit(1) unless $child; - my $watcher = Filesys::Notify::Simple->new([$ENV{DANCER_ENVDIR}]); + 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 { signal_child('TERM', $child); exit(0); }; + local $SIG{TERM} = sub { $child = signal_child('TERM', $child); exit(0); }; while (1) { my @restart; @@ -85,17 +88,31 @@ sub restarter { # this is blocking $watcher->wait(sub { my @events = @_; - @events = grep {file($_->{path})->basename eq $config} @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; - warn "-- $_->{path} updated.\n" for @restart; - signal_child('TERM', $child); - $child = fork_and_start($daemon, @program_args); - exit(1) unless $child; + 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; + } } } @@ -121,6 +138,36 @@ sub signal_child { 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 probability. if the file's larger, then we rename and kill + if ((-s $log_file) > (11 * 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 diff --git a/Netdisco/bin/netdisco-web b/Netdisco/bin/netdisco-web index 6d0b0b2e..dc93030b 100755 --- a/Netdisco/bin/netdisco-web +++ b/Netdisco/bin/netdisco-web @@ -42,6 +42,7 @@ BEGIN { use Daemon::Control; use Filesys::Notify::Simple; use IO::File; +use File::Copy; use App::Netdisco::Environment; my $config = ($ENV{PLACK_ENV} || $ENV{DANCER_ENVIRONMENT}) .'.yml'; @@ -92,7 +93,7 @@ sub restarter { my $child = fork_and_start($daemon, @program_args); exit(1) unless $child; - my $watcher = Filesys::Notify::Simple->new([$ENV{DANCER_ENVDIR}]); + my $watcher = Filesys::Notify::Simple->new([$ENV{DANCER_ENVDIR}, $log_dir]); warn "config watcher: watching $ENV{DANCER_ENVDIR} for updates.\n"; # TODO: starman also supports TTIN,TTOU,INT,QUIT @@ -105,15 +106,27 @@ sub restarter { # this is blocking $watcher->wait(sub { my @events = @_; - @events = grep {file($_->{path})->basename eq $config} @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; - warn "-- $_->{path} updated.\n" for @restart; - signal_child('HUP', $child); + foreach my $f (@restart) { + if ($f->{path} eq $log_file) { + ++$rotate; + } + else { + warn "-- $f->{path} updated.\n"; + ++$hupit; + } + } + + rotate_logs($child) if $rotate; + signal_child('HUP', $child) if $hupit; } } @@ -139,6 +152,34 @@ sub signal_child { 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 probability. if the file's larger, then we rename and kill + if ((-s $log_file) > (15 * 1024768)) { + rename $log_file, $log_file .'.1'; + signal_child('HUP', $child); + } + else { + copy $log_file, $log_file .'.1'; + truncate $log_file, 0; + } +} + =head1 NAME netdisco-web - Web Application Server for Netdisco