From 2f055fdab1f97b194135ca1580d498bd210fbc5f Mon Sep 17 00:00:00 2001 From: Christoph Berg <christoph.berg@credativ.de> Date: Fri, 18 Dec 2015 14:55:44 +0100 Subject: [PATCH] pg_lsclusters --wait-stop waits for the clusters to terminate Unfortunately this doesn't work; ExecStopPost is executed *before* the postgresql@* services stop --- pg_lsclusters | 44 ++++++++++++++++++++++++++++++++++++-- systemd/postgresql.service | 3 ++- 2 files changed, 44 insertions(+), 3 deletions(-) diff --git a/pg_lsclusters b/pg_lsclusters index d9f14c3e..37128f56 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 bcaf8141..c61cc924 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] -- GitLab