]> git.proxmox.com Git - pve-manager.git/commitdiff
pvescheduler: implement graceful reloading
authorDominik Csapak <d.csapak@proxmox.com>
Thu, 18 Nov 2021 13:28:31 +0000 (14:28 +0100)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Mon, 22 Nov 2021 16:19:12 +0000 (17:19 +0100)
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@proxmox.com>
PVE/Service/pvescheduler.pm
services/pvescheduler.service

index 466cc59906237d32f650f0f09d27d2811b52d6f6..700c96ec8e4eb5af0a6ff2c6a90a2158816a47da 100755 (executable)
@@ -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";} ],
 };
 
index 11769e80cba0eb1029fd2914d940b4b8521742e2..e6f108322ab118b7c73341dd6bf1d7a8929095a2 100644 (file)
@@ -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