Skip to content

Instantly share code, notes, and snippets.

@troelskn
Last active January 28, 2019 12:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save troelskn/63ae515eb8952849b2efba946f151421 to your computer and use it in GitHub Desktop.
Save troelskn/63ae515eb8952849b2efba946f151421 to your computer and use it in GitHub Desktop.
Wait for db. Useful in a dockerized setup
namespace :db do
desc "Wait for db. Used for initial boot up"
task wait: :environment do
puts "*** Giving DB time to boot"
catch :done do
10.times do
Timeout.timeout(1) do
begin
ActiveRecord::Base.establish_connection
ActiveRecord::Base.connection.execute("SELECT NOW()")
puts "*** DB responded"
throw :done
rescue Timeout::Error, PG::ConnectionBad, Mysql2::Error, ActiveRecord::NoDatabaseError => err
puts err
end
end
sleep 1
end
fail "Did not get a connection"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment