Module: Rpush::Daemon::Store::ActiveRecord::Reconnectable
- Included in:
- Rpush::Daemon::Store::ActiveRecord
- Defined in:
- lib/rpush/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
56 57 58 59 |
# File 'lib/rpush/daemon/store/active_record/reconnectable.rb', line 56 def check_database_is_connected # Simply asking the adapter for the connection state is not sufficient. Rpush::Notification.count end |
#database_connection_lost ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/rpush/daemon/store/active_record/reconnectable.rb', line 34 def database_connection_lost Rpush.logger.warn("Lost connection to database, reconnecting...") attempts = 0 loop do begin Rpush.logger.warn("Attempt #{attempts += 1}") reconnect_database check_database_is_connected break rescue *ADAPTER_ERRORS => e Rpush.logger.error(e) sleep_to_avoid_thrashing end end Rpush.logger.warn("Database reconnected") end |
#reconnect_database ⇒ Object
51 52 53 54 |
# File 'lib/rpush/daemon/store/active_record/reconnectable.rb', line 51 def reconnect_database ::ActiveRecord::Base.clear_all_connections! ::ActiveRecord::Base.establish_connection end |
#sleep_to_avoid_thrashing ⇒ Object
61 62 63 |
# File 'lib/rpush/daemon/store/active_record/reconnectable.rb', line 61 def sleep_to_avoid_thrashing sleep 2 end |
#with_database_reconnect_and_retry ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/rpush/daemon/store/active_record/reconnectable.rb', line 22 def with_database_reconnect_and_retry begin ::ActiveRecord::Base.connection_pool.with_connection do yield end rescue *ADAPTER_ERRORS => e Rpush.logger.error(e) database_connection_lost retry end end |