13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
# File 'lib/freedom_patches/rails_multisite.rb', line 13
def self.safe_each_connection
each_active_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")}"
end
end
end
|