[pve-devel] [PATCH manager 3/3] pvescheduler: implement graceful reloading

Dominik Csapak d.csapak at proxmox.com
Thu Nov 18 14:28:31 CET 2021


utilize PVE::Daemons 'hup' functionality to reload gracefully.

Leaves the children running (if any) and give them to the new instance
via ENV variables. After loading, check if they are still around

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
the only weird behaviour is that the re-exec can be up to one minute
after the reload, since we only get into the loop once a minute

we can shorten the loop cycle if we want though..

 PVE/Service/pvescheduler.pm   | 22 +++++++++++++++++++++-
 services/pvescheduler.service |  1 +
 2 files changed, 22 insertions(+), 1 deletion(-)

diff --git a/PVE/Service/pvescheduler.pm b/PVE/Service/pvescheduler.pm
index 466cc599..700c96ec 100755
--- a/PVE/Service/pvescheduler.pm
+++ b/PVE/Service/pvescheduler.pm
@@ -24,19 +24,35 @@ my $finish_jobs = sub {
     for my $type (@types) {
 	if (my $cpid = $self->{jobs}->{$type}) {
 	    my $waitpid = waitpid($cpid, WNOHANG);
-	    if (defined($waitpid) && ($waitpid == $cpid)) {
+	    if (defined($waitpid) && ($waitpid == $cpid) || $waitpid == -1) {
 		$self->{jobs}->{$type} = undef;
 	    }
 	}
     }
 };
 
+sub hup {
+    my ($self) = @_;
+
+    for my $type (@types) {
+	my $pid = $self->{jobs}->{$type};
+	next if !defined($pid);
+	$ENV{"PVE_DAEMON_${type}_PID"} = $pid;
+    }
+}
+
 sub run {
     my ($self) = @_;
 
     my $jobs = {};
     $self->{jobs} = $jobs;
 
+    for my $type (@types) {
+	$self->{jobs}->{$type} = delete $ENV{"PVE_DAEMON_${type}_PID"};
+	# check if children finished in the meantime
+	$finish_jobs->($self);
+    }
+
     my $old_sig_chld = $SIG{CHLD};
     local $SIG{CHLD} = sub {
 	local ($@, $!, $?); # do not overwrite error vars
@@ -82,6 +98,8 @@ sub run {
 
     for (my $count = 1000;;$count++) {
 	last if $self->{shutdown_request};
+	# we got a reload signal, return gracefully and leave the forks running
+	return if $self->{got_hup_signal};
 
 	$run_jobs->();
 
@@ -125,11 +143,13 @@ sub shutdown {
 
 $daemon->register_start_command();
 $daemon->register_stop_command();
+$daemon->register_restart_command(1);
 $daemon->register_status_command();
 
 our $cmddef = {
     start => [ __PACKAGE__, 'start', []],
     stop => [ __PACKAGE__, 'stop', []],
+    restart => [ __PACKAGE__, 'restart', []],
     status => [ __PACKAGE__, 'status', [], undef, sub { print shift . "\n";} ],
 };
 
diff --git a/services/pvescheduler.service b/services/pvescheduler.service
index 11769e80..e6f10832 100644
--- a/services/pvescheduler.service
+++ b/services/pvescheduler.service
@@ -8,6 +8,7 @@ After=pve-storage.target
 [Service]
 ExecStart=/usr/bin/pvescheduler start
 ExecStop=/usr/bin/pvescheduler stop
+ExecReload=/usr/bin/pvescheduler restart
 PIDFile=/var/run/pvescheduler.pid
 KillMode=process
 Type=forking
-- 
2.30.2






More information about the pve-devel mailing list