Module: Rapns::Daemon::Store::ActiveRecord::Reconnectable
- Included in:
- Rapns::Daemon::Store::ActiveRecord
- Defined in:
- lib/rapns/daemon/store/active_record/reconnectable.rb
Constant Summary collapse
- ADAPTER_ERRORS =
[::ActiveRecord::StatementInvalid, PGError, Mysql::Error, Mysql2::Error, ::ActiveRecord::JDBCError, SQLite3::Exception]
Instance Method Summary collapse
- #check_database_is_connected ⇒ Object
- #database_connection_lost ⇒ Object
- #reconnect_database ⇒ Object
- #sleep_to_avoid_thrashing ⇒ Object
- #with_database_reconnect_and_retry ⇒ Object
Instance Method Details
#check_database_is_connected ⇒ Object
54 55 56 57 |
# File 'lib/rapns/daemon/store/active_record/reconnectable.rb', line 54 def check_database_is_connected # Simply asking the adapter for the connection state is not sufficient. Rapns::Notification.count end |
#database_connection_lost ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/rapns/daemon/store/active_record/reconnectable.rb', line 32 def database_connection_lost Rapns.logger.warn("Lost connection to database, reconnecting...") attempts = 0 loop do begin Rapns.logger.warn("Attempt #{attempts += 1}") reconnect_database check_database_is_connected break rescue *ADAPTER_ERRORS => e Rapns.logger.error(e, :airbrake_notify => false) sleep_to_avoid_thrashing end end Rapns.logger.warn("Database reconnected") end |
#reconnect_database ⇒ Object
49 50 51 52 |
# File 'lib/rapns/daemon/store/active_record/reconnectable.rb', line 49 def reconnect_database ::ActiveRecord::Base.clear_all_connections! ::ActiveRecord::Base.establish_connection end |
#sleep_to_avoid_thrashing ⇒ Object
59 60 61 |
# File 'lib/rapns/daemon/store/active_record/reconnectable.rb', line 59 def sleep_to_avoid_thrashing sleep 2 end |
#with_database_reconnect_and_retry ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/rapns/daemon/store/active_record/reconnectable.rb', line 20 def with_database_reconnect_and_retry begin ::ActiveRecord::Base.connection_pool.with_connection do yield end rescue *ADAPTER_ERRORS => e Rapns.logger.error(e) database_connection_lost retry end end |