module Rpush::Daemon::Store::ActiveRecord::Reconnectable

Constants

ADAPTER_ERRORS

Public Instance Methods

check_database_is_connected() click to toggle source
# File lib/rpush/daemon/store/active_record/reconnectable.rb, line 68
def check_database_is_connected
  # Simply asking the adapter for the connection state is not sufficient.
  Rpush::Client::ActiveRecord::Notification.count
end
database_connection_lost() click to toggle source
# File lib/rpush/daemon/store/active_record/reconnectable.rb, line 46
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() click to toggle source
# File lib/rpush/daemon/store/active_record/reconnectable.rb, line 63
def reconnect_database
  ::ActiveRecord::Base.clear_all_connections!
  ::ActiveRecord::Base.establish_connection
end
sleep_to_avoid_thrashing() click to toggle source
# File lib/rpush/daemon/store/active_record/reconnectable.rb, line 73
def sleep_to_avoid_thrashing
  sleep 2
end
with_database_reconnect_and_retry() { || ... } click to toggle source
# File lib/rpush/daemon/store/active_record/reconnectable.rb, line 35
def with_database_reconnect_and_retry
  ::ActiveRecord::Base.connection_pool.with_connection do
    yield
  end
rescue *ADAPTER_ERRORS => e
  Rpush.logger.error(e)
  sleep_to_avoid_thrashing
  database_connection_lost
  retry
end