return if $self->{terminate};
- my $count = 0;
- foreach my $cpid (keys %{$self->{workers}}) {
- $count++;
- }
-
+ my $count = scalar keys %{$self->{workers}};
my $need = $self->{max_workers} - $count;
return if $need <= 0;
}
};
+my $terminate_old_workers = sub {
+ my ($self) = @_;
+
+ # if list is empty kill sends no signal, so no checks needed
+ kill 15, keys %{$self->{old_workers}};
+};
+
my $terminate_server = sub {
my ($self, $allow_open_children) = @_;
eval { $self->shutdown(); };
warn $@ if $@;
- # we have workers - send TERM signal
-
- foreach my $cpid (keys %{$self->{workers}}) {
- kill(15, $cpid); # TERM childs
- }
# if configured, leave children running on HUP
- return if $allow_open_children &&
- $self->{leave_children_open_on_reload};
+ return if $allow_open_children && $self->{leave_children_open_on_reload};
- # else, send TERM to old workers
- foreach my $cpid (keys %{$self->{old_workers}}) {
- kill(15, $cpid); # TERM childs
- }
+ # else send TERM to all (old and current) child workers
+ kill 15, keys %{$self->@{'workers','old_workers'}};
# nicely shutdown childs (give them max 10 seconds to shut down)
my $previous_alarm = alarm(10);
if ($restart && $self->{max_workers}) {
if (my $wpids = $ENV{PVE_DAEMON_WORKER_PIDS}) {
- foreach my $pid (split(':', $wpids)) {
- if ($pid =~ m/^(\d+)$/) {
- $self->{old_workers}->{$1} = 1;
- }
- }
+ $self->{old_workers}->{$_} = 1 foreach (split(':', $wpids));
}
}
&$old_sig_chld(@_) if $old_sig_chld;
};
- # catch worker finished during restart phase
- &$finish_workers($self);
-
# now loop forever (until we receive terminate signal)
for (;;) {
&$start_workers($self);
sleep(5);
+ &$terminate_old_workers($self);
&$finish_workers($self);
last if $self->{terminate};
}
}
if ($self->{max_workers}) {
- my @workers = keys %{$self->{workers}};
- push @workers, keys %{$self->{old_workers}};
+ my @workers = keys %{$self->{workers}}, keys %{$self->{old_workers}};
$ENV{PVE_DAEMON_WORKER_PIDS} = join(':', @workers);
}
code => sub {
my ($param) = @_;
- if (&$init_ppid()) {
+ if (&$init_ppid() || $param->{debug}) {
$self->start($param->{debug});
} else {
PVE::Tools::run_command(['systemctl', 'start', $self->{name}]);