diff --git a/pg_lsclusters b/pg_lsclusters index d9f14c3eb4d5fa21a02624d1b8a837a5f7aad590..37128f56111076110a5711c0c9d39a55b694c285 100755 --- a/pg_lsclusters +++ b/pg_lsclusters @@ -15,11 +15,51 @@ # GNU General Public License for more details. use strict; +use warnings; use PgCommon; use Getopt::Long; +use Time::HiRes qw(usleep); + +my ($no_header, $wait_stop); +exit 1 unless GetOptions ( + 'h|no-header' => \$no_header, + 'wait-stopping' => \$wait_stop, +); + +if ($wait_stop) { + my $waiting = 0; + + # collect list of running "auto" clusters + my %clusters = (); + foreach my $v (get_versions()) { + foreach my $c (get_version_clusters $v) { + my %info = cluster_info $v, $c; + next unless ($info{start} eq 'auto'); + next unless ($info{running}); + $clusters{$v}->{$c} = 1; + $waiting = 1; # there is a cluster to wait for + } + } -my $no_header; -exit 1 unless GetOptions ('h|no-header' => \$no_header); + my $sleep = 100_000; # initial delay 0.1s + do { + $waiting = 0; + # wait for all running clusters to stop + foreach my $v (keys %clusters) { + foreach my $c (keys %{$clusters{$v}}) { + my %info = cluster_info $v, $c; + if ($info{running}) { + print "Waiting $sleep for $v $c to shut down ...\n"; + usleep $sleep; + $sleep *= 1.5; + $waiting = 1; + } else { + delete $clusters{$v}->{$c}; + } + } + } + } while ($waiting and $sleep < 30_000_000); +} my @lines; push @lines, ['Ver', 'Cluster', 'Port', 'Status', 'Owner', 'Data directory', 'Log file'] diff --git a/systemd/postgresql.service b/systemd/postgresql.service index bcaf8141711fe17d2748e5073e7e85102f300502..c61cc92499d88d2b9381bd8351eaddeaa9ea3dc4 100644 --- a/systemd/postgresql.service +++ b/systemd/postgresql.service @@ -7,8 +7,9 @@ Description=PostgreSQL RDBMS [Service] Type=oneshot -ExecStart=/bin/true +ExecStart=/usr/bin/pg_lsclusters ExecReload=/bin/true +ExecStopPost=/usr/bin/pg_lsclusters --wait-stopping RemainAfterExit=on [Install]