Skip to content

Instantly share code, notes, and snippets.

@seanlinsley
Last active August 29, 2015 14:19
Show Gist options
  • Select an option

  • Save seanlinsley/57e6cc215c313763c91d to your computer and use it in GitHub Desktop.

Select an option

Save seanlinsley/57e6cc215c313763c91d to your computer and use it in GitHub Desktop.
Rails: retry connecting to Postgres
development: &default
adapter: postgresql
database: app_development
connect_retry_timeout: 1
# Make Postgres gracefully retry to create a connection. This lets us
# restart Postgres during our deploys without causing page loads to fail.
module PGConnectRetryTimeout
def initialize(*args)
error = nil
timeout = args.first.delete :connect_retry_timeout
Timeout.timeout timeout do
begin
super
rescue => error
timeout && timeout > 0 ? retry : raise
end
end
rescue Timeout::Error
raise error
end
end
PG::Connection.prepend PGConnectRetryTimeout
params = ActiveRecord::ConnectionHandling::VALID_CONN_PARAMS
if params.include? :connect_retry_timeout
raise '???'
else
params << :connect_retry_timeout
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment