Class: RailsMultisite::ConnectionManagement

Inherits:
Object
  • Object
show all
Defined in:
lib/freedom_patches/rails_multisite.rb

Class Method Summary collapse

Class Method Details

.safe_each_connectionObject



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/freedom_patches/rails_multisite.rb', line 5

def self.safe_each_connection
  self.each_connection do |db|
    begin
      yield(db) if block_given?
    rescue PG::ConnectionBad, PG::UnableToSend, PG::ServerError
      break if !defined?(RailsFailover::ActiveRecord)
      break if db == RailsMultisite::ConnectionManagement::DEFAULT

      reading_role = :"#{db}_#{ActiveRecord.reading_role}"
      spec = RailsMultisite::ConnectionManagement.connection_spec(db: db)
      handler = ActiveRecord::Base.connection_handler

      RailsFailover::ActiveRecord.establish_reading_connection(
        handler,
        spec.to_hash,
        role: reading_role,
      )
      ActiveRecord::Base.connected_to(role: reading_role) { yield(db) if block_given? }
    rescue => e
      STDERR.puts "URGENT: Failed to initialize site #{db}: " \
                    "#{e.class} #{e.message}\n#{e.backtrace.join("\n")}"
      # the show must go on, don't stop startup if multisite fails
    end
  end
end