From dd09c46cfd3bc445a879257bb530117b904f09fa Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 15 Oct 2012 17:23:19 -0400 Subject: [PATCH] Perl SIGHUP handling and config reloading Sending the HUP signal to a Perl Listener process now results in the following: * Reload the opensrf_core config * re-init the logger * kill idle child processes * child processes that are active when the signal is received are tracked and killed once they become idle. * New children are spawned per the min child settings The primary use case for these changes is temporarily changing the log level (or log file) for a given service for debug purposes. It may also be used, for example, to gracefully recover excess RAM consumed by a child process. Not all values in opensrf_core.xml are affected by a SIGHUP. Since the goal is a gracful reload, no attempt is made by the listener process to re-connect to jabber or re-register with the opensrf router(s). Child processes will by necessity connect to Jabber during startup, though, so any changes to the jabber configuration will affect child processes. In general, it's best to affect jabber configuration changes with a true service restart. opensrf.xml (opensrf.settings config) is not reloaded, so min/max child settings will not be affected. Signed-off-by: Bill Erickson Signed-off-by: Galen Charlton --- src/perl/lib/OpenSRF/Server.pm | 53 +++++++++++++++++++++++++++- src/perl/lib/OpenSRF/Utils/Logger.pm | 3 +- 2 files changed, 54 insertions(+), 2 deletions(-) diff --git a/src/perl/lib/OpenSRF/Server.pm b/src/perl/lib/OpenSRF/Server.pm index 8034a1a..35d135a 100644 --- a/src/perl/lib/OpenSRF/Server.pm +++ b/src/perl/lib/OpenSRF/Server.pm @@ -43,6 +43,7 @@ sub new { $self->{routers} = []; # list of registered routers $self->{active_list} = []; # list of active children $self->{idle_list} = []; # list of idle children + $self->{sighup_pending} = []; $self->{pid_map} = {}; # map of child pid to child for cleaner access $self->{sig_pipe} = 0; # true if last syswrite failed @@ -86,6 +87,35 @@ sub cleanup { exit(0) unless $no_exit; } +# ---------------------------------------------------------------- +# SIGHUP handler. Kill all idle children. Copy list of active +# children into sighup_pending list for later cleanup. +# ---------------------------------------------------------------- +sub handle_sighup { + my $self = shift; + $logger->info("server: caught SIGHUP; reloading children"); + + # reload the opensrf config + # note: calling ::Config->load() results in ever-growing + # package names, which eventually causes an exception + OpenSRF::Utils::Config->current->_load( + force => 1, + config_file => OpenSRF::Utils::Config->current->FILE + ); + + # force-reload the logger config + OpenSRF::Utils::Logger::set_config(1); + + # copy active list into pending list for later cleanup + $self->{sighup_pending} = [ @{$self->{active_list}} ]; + + # idle_list will be modified as children are reaped. + my @idle = @{$self->{idle_list}}; + + # idle children are the reaper's plaything + $self->kill_child($_) for @idle; +} + # ---------------------------------------------------------------- # Waits on the jabber socket for inbound data from the router. # Each new message is passed off to a child process for handling. @@ -98,6 +128,7 @@ sub run { $SIG{$_} = sub { $self->cleanup; } for (qw/INT TERM QUIT/); $SIG{CHLD} = sub { $self->reap_children(); }; + $SIG{HUP} = sub { $self->handle_sighup(); }; $self->spawn_children; $self->build_osrf_handle; @@ -313,6 +344,27 @@ sub check_status { $chatty and $logger->internal(sprintf( "server: %d idle and %d active children after status update", scalar(@{$self->{idle_list}}), scalar(@{$self->{active_list}}))); + + # some children just went from active to idle. let's see + # if any of them need to be killed from a previous sighup. + + for my $child (@{$self->{sighup_pending}}) { + if (grep {$_ == $child->{pid}} @pids) { + + $chatty and $logger->internal( + "server: killing previously-active ". + "child after receiving SIGHUP: $child"); + + # remove the pending child + $self->{sighup_pending} = [ + grep {$_->{pid} != $child->{pid}} + @{$self->{sighup_pending}} + ]; + + # kill the pending child + $self->kill_child($child) + } + } } # ---------------------------------------------------------------- @@ -348,7 +400,6 @@ sub reap_children { $chatty and $logger->internal(sprintf( "server: %d idle and %d active children after reap_children", scalar(@{$self->{idle_list}}), scalar(@{$self->{active_list}}))); - } # ---------------------------------------------------------------- diff --git a/src/perl/lib/OpenSRF/Utils/Logger.pm b/src/perl/lib/OpenSRF/Utils/Logger.pm index f51f16e..f97d557 100644 --- a/src/perl/lib/OpenSRF/Utils/Logger.pm +++ b/src/perl/lib/OpenSRF/Utils/Logger.pm @@ -57,8 +57,9 @@ my $isclient; # true if we control the osrf_xid # load up our config options sub set_config { + my $force = shift; - return if defined $config; + return if defined $config and !$force; $config = OpenSRF::Utils::Config->current; if( !defined($config) ) { -- 2.43.2