Skip to content

Instantly share code, notes, and snippets.

@jwieringa
Forked from csexton/wait-for-db
Created August 6, 2016 14:05
Show Gist options
  • Save jwieringa/43f7b28f18a7cbfa3ec9f8be613e793e to your computer and use it in GitHub Desktop.
Save jwieringa/43f7b28f18a7cbfa3ec9f8be613e793e 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