Last active
August 29, 2015 14:19
-
-
Save seanlinsley/57e6cc215c313763c91d to your computer and use it in GitHub Desktop.
Rails: retry connecting to Postgres
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| development: &default | |
| adapter: postgresql | |
| database: app_development | |
| connect_retry_timeout: 1 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # 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