Module: Push::Daemon::DatabaseReconnectable
- Included in:
- Push::Daemon, App, Feedback::FeedbackFeeder, Feeder, Feedback, Message
- Defined in:
- lib/push/daemon/database_reconnectable.rb
Instance Method Summary collapse
- #adaptor_errors ⇒ Object
- #check_database_is_connected ⇒ Object
- #database_connection_lost(name) ⇒ Object
- #reconnect_database ⇒ Object
- #sleep_to_avoid_thrashing ⇒ Object
- #with_database_reconnect_and_retry(name) ⇒ Object
Instance Method Details
#adaptor_errors ⇒ Object
4 5 6 7 8 9 10 11 12 13 |
# File 'lib/push/daemon/database_reconnectable.rb', line 4 def adaptor_errors errors = [ActiveRecord::StatementInvalid, ActiveRecord::ConnectionNotEstablished] if defined?(::PG::Error) errors << ::PG::Error elsif defined?(::PGError) errors << ::PGError end errors << ::Mysql2::Error if defined?(::Mysql2) errors end |
#check_database_is_connected ⇒ Object
52 53 54 55 |
# File 'lib/push/daemon/database_reconnectable.rb', line 52 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
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
# File 'lib/push/daemon/database_reconnectable.rb', line 25 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_database ⇒ Object
42 43 44 45 46 47 48 49 50 |
# File 'lib/push/daemon/database_reconnectable.rb', line 42 def reconnect_database begin ActiveRecord::Base.clear_all_connections! rescue Push::Daemon.logger.error("ActiveRecord::Base.clear_all_connections! failed") ensure ActiveRecord::Base.establish_connection(ActiveRecord::Base.configurations[ENV['RAILS_ENV']]) end end |
#sleep_to_avoid_thrashing ⇒ Object
57 58 59 |
# File 'lib/push/daemon/database_reconnectable.rb', line 57 def sleep_to_avoid_thrashing sleep 2 end |
#with_database_reconnect_and_retry(name) ⇒ Object
15 16 17 18 19 20 21 22 23 |
# File 'lib/push/daemon/database_reconnectable.rb', line 15 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 |