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]
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
49 50 51 52 |
# File 'lib/rapns/daemon/store/active_record/reconnectable.rb', line 49 def check_database_is_connected # Simply asking the adapter for the connection state is not sufficient. Rapns::Notification.count end |
#database_connection_lost ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/rapns/daemon/store/active_record/reconnectable.rb', line 27 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
44 45 46 47 |
# File 'lib/rapns/daemon/store/active_record/reconnectable.rb', line 44 def reconnect_database ::ActiveRecord::Base.clear_all_connections! ::ActiveRecord::Base.establish_connection end |
#sleep_to_avoid_thrashing ⇒ Object
54 55 56 |
# File 'lib/rapns/daemon/store/active_record/reconnectable.rb', line 54 def sleep_to_avoid_thrashing sleep 2 end |
#with_database_reconnect_and_retry ⇒ Object
15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/rapns/daemon/store/active_record/reconnectable.rb', line 15 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 |