2018-12-12 11:29:48 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module RailsMultisite
|
|
|
|
class ConnectionManagement
|
|
|
|
def self.safe_each_connection
|
|
|
|
self.each_connection do |db|
|
|
|
|
begin
|
|
|
|
yield(db) if block_given?
|
2020-06-23 16:28:21 +08:00
|
|
|
rescue PG::ConnectionBad, PG::UnableToSend, PG::ServerError
|
|
|
|
break if !defined?(RailsFailover::ActiveRecord)
|
|
|
|
break if db == RailsMultisite::ConnectionManagement::DEFAULT
|
|
|
|
|
2022-03-21 22:28:52 +08:00
|
|
|
reading_role = :"#{db}_#{ActiveRecord.reading_role}"
|
2020-06-23 16:28:21 +08:00
|
|
|
spec = RailsMultisite::ConnectionManagement.connection_spec(db: db)
|
|
|
|
|
|
|
|
ActiveRecord::Base.connection_handlers[reading_role] ||= begin
|
|
|
|
handler = ActiveRecord::ConnectionAdapters::ConnectionHandler.new
|
|
|
|
RailsFailover::ActiveRecord.establish_reading_connection(handler, spec)
|
|
|
|
handler
|
|
|
|
end
|
|
|
|
|
|
|
|
ActiveRecord::Base.connected_to(role: reading_role) do
|
|
|
|
yield(db) if block_given?
|
|
|
|
end
|
2018-12-12 11:29:48 +08: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 do |env|
|
|
|
|
env["PATH_INFO"] == "/srv/status" ? "default" : nil
|
|
|
|
end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|