Skip to content

Instantly share code, notes, and snippets.

@csexton
Created August 6, 2016 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save csexton/ec901875282fd927698a10fb2c5bd168 to your computer and use it in GitHub Desktop.
Save csexton/ec901875282fd927698a10fb2c5bd168 to your computer and use it in GitHub Desktop.
A ruby script that will wait for a port to be connectable on a host, normally used to check if another Docker container has booted up
#!/usr/bin/env ruby
require "socket"
require "uri"
if ENV["DATABASE_URL"]
url = URI.parse ENV["DATABASE_URL"]
puts "Waiting for DB on #{url}..."
30.times do
begin
TCPSocket.new(url.host, url.port || 5432)
puts "Success!"
exit 0
rescue => e
print "."
end
sleep 1
end
exit 1
else
STDERR.puts "No DATABASE_URL env var is set, skipping"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment