Module: Push::Daemon::DatabaseReconnectable

Included in:
Feedback::FeedbackFeeder, Feeder, Feedback, Message
Defined in:
lib/push/daemon/database_reconnectable.rb

Instance Method Summary collapse

Instance Method Details

#adaptor_errorsObject



4
5
6
7
8
9
# File 'lib/push/daemon/database_reconnectable.rb', line 4

def adaptor_errors
  errors = [ActiveRecord::StatementInvalid, ActiveRecord::ConnectionNotEstablished]
  errors << PGError if defined?(PGError)
  errors << Mysql2::Error if defined?(Mysql2)
  errors
end

#check_database_is_connectedObject



43
44
45
46
# File 'lib/push/daemon/database_reconnectable.rb', line 43

def check_database_is_connected
  # Simply asking the adapter for the connection state is not sufficient.
  Push::Message.count
end

#database_connection_lost(name) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/push/daemon/database_reconnectable.rb', line 21

def database_connection_lost(name)
  Push::Daemon.logger.warn("[#{name}] Lost connection to database, reconnecting...")
  attempts = 0
  loop do
    begin
      Push::Daemon.logger.warn("[#{name}] Attempt #{attempts += 1}")
      reconnect_database
      check_database_is_connected
      break
    rescue *adaptor_errors => e
      Push::Daemon.logger.error(e, :error_notification => false)
      sleep_to_avoid_thrashing
    end
  end
  Push::Daemon.logger.warn("[#{name}] Database reconnected")
end

#reconnect_databaseObject



38
39
40
41
# File 'lib/push/daemon/database_reconnectable.rb', line 38

def reconnect_database
  ActiveRecord::Base.clear_all_connections!
  ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ENV['RAILS_ENV']])
end

#sleep_to_avoid_thrashingObject



48
49
50
# File 'lib/push/daemon/database_reconnectable.rb', line 48

def sleep_to_avoid_thrashing
  sleep 2
end

#with_database_reconnect_and_retry(name) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/push/daemon/database_reconnectable.rb', line 11

def with_database_reconnect_and_retry(name)
  begin
    yield
  rescue *adaptor_errors => e
    Push::Daemon.logger.error(e)
    database_connection_lost(name)
    retry
  end
end