"Fossies" - the Fresh Open Source Software Archive

Member "discourse-2.8.3/config/initializers/002-rails_failover.rb" (14 Apr 2022, 2875 Bytes) of package /linux/www/discourse-2.8.3.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 
    3 if defined?(RailsFailover::Redis)
    4   message_bus_keepalive_interval = nil
    5 
    6   RailsFailover::Redis.on_failover do
    7     message_bus_keepalive_interval = MessageBus.keepalive_interval
    8     MessageBus.keepalive_interval = -1 # Disable MessageBus keepalive_interval
    9     Discourse.received_redis_readonly!
   10   end
   11 
   12   RailsFailover::Redis.on_fallback do
   13     Discourse.clear_redis_readonly!
   14     Discourse.request_refresh!
   15     MessageBus.keepalive_interval = message_bus_keepalive_interval
   16 
   17     ObjectSpace.each_object(DistributedCache) do |cache|
   18       cache.clear
   19     end
   20 
   21     SiteSetting.refresh!
   22   end
   23 
   24   if Rails.logger.respond_to? :chained
   25     RailsFailover::Redis.logger = Rails.logger.chained.first
   26   end
   27 end
   28 
   29 if defined?(RailsFailover::ActiveRecord)
   30   return unless Rails.configuration.active_record_rails_failover
   31 
   32   if Rails.configuration.multisite
   33     if ActiveRecord::Base.current_role == ActiveRecord::Base.reading_role
   34       RailsMultisite::ConnectionManagement.default_connection_handler =
   35         ActiveRecord::Base.connection_handlers[ActiveRecord::Base.reading_role]
   36     end
   37   end
   38 
   39   RailsFailover::ActiveRecord.on_failover do |role|
   40     if role == ActiveRecord::Base.writing_role # Multisite master
   41       RailsMultisite::ConnectionManagement.each_connection do
   42         Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
   43       end
   44     else
   45       ActiveRecord::Base.connected_to(role: role) do
   46         Discourse.enable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
   47       end
   48 
   49       # Test connection to the master, and trigger master failover if needed
   50       ActiveRecord::Base.connected_to(role: ActiveRecord::Base.writing_role) do
   51         ActiveRecord::Base.connection.active?
   52       rescue PG::ConnectionBad, PG::UnableToSend, PG::ServerError
   53         RailsFailover::ActiveRecord.verify_primary(ActiveRecord::Base.writing_role)
   54       end
   55     end
   56   end
   57 
   58   RailsFailover::ActiveRecord.on_fallback do |role|
   59     if role == ActiveRecord::Base.writing_role # Multisite master
   60       RailsMultisite::ConnectionManagement.each_connection do
   61         Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
   62       end
   63     else
   64       ActiveRecord::Base.connected_to(role: role) do
   65         Discourse.disable_readonly_mode(Discourse::PG_READONLY_MODE_KEY)
   66       end
   67     end
   68 
   69     if Rails.configuration.multisite
   70       RailsMultisite::ConnectionManagement.default_connection_handler =
   71         ActiveRecord::Base.connection_handlers[ActiveRecord::Base.writing_role]
   72     end
   73   end
   74 
   75   RailsFailover::ActiveRecord.register_force_reading_role_callback do
   76     Discourse.redis.exists?(
   77       Discourse::PG_READONLY_MODE_KEY,
   78       Discourse::PG_FORCE_READONLY_MODE_KEY
   79     )
   80   rescue => e
   81     if !e.is_a?(Redis::CannotConnectError)
   82       Rails.logger.warn "#{e.class} #{e.message}: #{e.backtrace.join("\n")}"
   83     end
   84 
   85     false
   86   end
   87 end