Skip to content

Commit 8f34e19

Browse files
committed
Fix stopping of old test cluster when pg_ctl is not in the path
If the pg_ctl binary was not in the path we would fail to stop the running database clusters so let's use our helper for getting the path to the binary. To keep the code simple we also move the stopping of the existing cluster to the code which starts it since that is only where we, at least currently, knows the path the the binaries.
1 parent 30ed119 commit 8f34e19

1 file changed

Lines changed: 20 additions & 28 deletions

File tree

spec/helpers.rb

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -188,32 +188,6 @@ def log_and_run( logpath, *cmd )
188188

189189
extend Loggable
190190

191-
### Check the current directory for directories that look like they're
192-
### testing directories from previous tests, and tell any postgres instances
193-
### running in them to shut down.
194-
def stop_existing_postmasters
195-
# tmp_test_0.22329534700318
196-
pat = Pathname.getwd + 'tmp_test_*'
197-
Pathname.glob( pat.to_s ).each do |testdir|
198-
datadir = testdir + 'data'
199-
pidfile = datadir + 'postmaster.pid'
200-
if pidfile.exist? && pid = pidfile.read.chomp.to_i
201-
trace "pidfile (%p) exists: %d" % [ pidfile, pid ]
202-
begin
203-
Process.kill( 0, pid )
204-
rescue Errno::ESRCH
205-
trace "No postmaster running for %s" % [ datadir ]
206-
# Process isn't alive, so don't try to stop it
207-
else
208-
trace "Stopping lingering database at PID %d" % [ pid ]
209-
run 'pg_ctl', '-D', datadir.to_s, '-m', 'fast', 'stop'
210-
end
211-
else
212-
trace "No pidfile (%p)" % [ pidfile ]
213-
end
214-
end
215-
end
216-
217191
class PostgresServer
218192
include Loggable
219193

@@ -238,6 +212,7 @@ def initialize(name, port: 23456, postgresql_conf: '')
238212

239213
begin
240214
@pgdata.mkpath
215+
stop_existing_cluster
241216
setup_cluster(postgresql_conf)
242217
start_cluster
243218
rescue => err
@@ -317,6 +292,25 @@ def start_cluster
317292
log_and_run @logfile, pg_bin_path('pg_ctl'), '-w', '-o', sopt, '-D', @pgdata.to_s, 'start'
318293
end
319294

295+
def stop_existing_cluster
296+
pidfile = @pgdata + 'postmaster.pid'
297+
298+
if pidfile.exist? && pid = pidfile.read.chomp.to_i
299+
trace "pidfile (%p) exists: %d" % [ pidfile, pid ]
300+
begin
301+
Process.kill( 0, pid )
302+
rescue Errno::ESRCH
303+
trace "No postmaster running for %s" % [ @pgdata ]
304+
# Process isn't alive, so don't try to stop it
305+
else
306+
trace "Stopping lingering database at PID %d" % [ pid ]
307+
run pg_bin_path('pg_ctl'), '-D', @pgdata.to_s, '-m', 'fast', 'stop'
308+
end
309+
else
310+
trace "No pidfile (%p)" % [ pidfile ]
311+
end
312+
end
313+
320314
def generate_ssl_certs(output_dir)
321315
gen = CertGenerator.new(output_dir)
322316

@@ -701,8 +695,6 @@ def set_etc_hosts(hostaddr, hostname)
701695

702696
### Automatically set up and tear down the database
703697
config.before(:suite) do |*args|
704-
PG::TestingHelpers.stop_existing_postmasters
705-
706698
ENV['PGHOST'] = 'localhost'
707699
ENV['PGPORT'] ||= "23456"
708700
port = ENV['PGPORT'].to_i

0 commit comments

Comments
 (0)