2018-12-11 22:29:48 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RailsMultisite
|
|
|
|
class ConnectionManagement
|
2024-09-03 01:56:46 -04:00
|
|
|
def self.each_active_connection(&blk)
|
|
|
|
if ENV["RAILS_DB"]
|
|
|
|
blk.call(current_db)
|
|
|
|
else
|
|
|
|
each_connection(&blk)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-12-11 22:29:48 -05:00
|
|
|
def self.safe_each_connection
|
2024-09-03 01:56:46 -04:00
|
|
|
each_active_connection do |db|
|
2018-12-11 22:29:48 -05:00
|
|
|
begin
|
|
|
|
yield(db) if block_given?
|
2020-06-23 04:28:21 -04:00
|
|
|
rescue PG::ConnectionBad, PG::UnableToSend, PG::ServerError
|
|
|
|
break if !defined?(RailsFailover::ActiveRecord)
|
|
|
|
break if db == RailsMultisite::ConnectionManagement::DEFAULT
|
|
|
|
|
2022-03-21 10:28:52 -04:00
|
|
|
reading_role = :"#{db}_#{ActiveRecord.reading_role}"
|
2020-06-23 04:28:21 -04:00
|
|
|
spec = RailsMultisite::ConnectionManagement.connection_spec(db: db)
|
2023-05-25 06:23:18 -04:00
|
|
|
handler = ActiveRecord::Base.connection_handler
|
2020-06-23 04:28:21 -04:00
|
|
|
|
2023-05-25 06:23:18 -04:00
|
|
|
RailsFailover::ActiveRecord.establish_reading_connection(
|
|
|
|
handler,
|
|
|
|
spec.to_hash,
|
|
|
|
role: reading_role,
|
|
|
|
)
|
2020-06-23 04:28:21 -04:00
|
|
|
ActiveRecord::Base.connected_to(role: reading_role) { yield(db) if block_given? }
|
2018-12-11 22:29:48 -05:00
|
|
|
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
|
|
|
|
end
|
|
|
|
|
|
|
|
class DiscoursePatches
|
|
|
|
def self.config
|
|
|
|
{ db_lookup: lambda { |env| env["PATH_INFO"] == "/srv/status" ? "default" : nil } }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|