<commit_before>require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/postgresql_adapter'
require 'discourse'

class PostgreSQLFallbackHandler
  include Singleton

  def initialize
    @masters_down = {}
    @mutex = Mutex.new
  end

  def verify_master
    synchronize { return if @thread && @thread.alive? }

    @thread = Thread.new do
      while true do
        begin
          thread = Thread.new { initiate_fallback_to_master }
          thread.join
          break if synchronize { @masters_down.empty? }
          sleep 10
        ensure
          thread.kill
        end
      end
    end
  end

  def master_down?
    synchronize { @masters_down[namespace] }
  end

  def master_down=(args)
    synchronize { @masters_down[namespace] = args }
  end

  def master_up(namespace)
    synchronize { @masters_down.delete(namespace) }
  end

  def running?
    synchronize { @thread.alive? }
  end

  def initiate_fallback_to_master
    @masters_down.keys.each do |key|
      RailsMultisite::ConnectionManagement.with_connection(key) do
        begin
          logger.warn "#{log_prefix}: Checking master server..."
          connection = ActiveRecord::Base.postgresql_connection(config)

          if connection.active?
            connection.disconnect!
            ActiveRecord::Base.clear_all_connections!
            logger.warn "#{log_prefix}: Master server is active. Reconnecting..."

            self.master_up(key)
            Discourse.disable_readonly_mode
          end
        rescue => e
          byebug
          logger.warn "#{log_prefix}: Connection to master PostgreSQL server failed with '#{e.message}'"
        end
      end
    end
  end

  # Use for testing
  def setup!
    @masters_down = {}
  end

  private

  def config
    ActiveRecord::Base.connection_config
  end

  def logger
    Rails.logger
  end

  def log_prefix
    "#{self.class} [#{namespace}]"
  end

  def namespace
    RailsMultisite::ConnectionManagement.current_db
  end

  def synchronize
    @mutex.synchronize { yield }<commit_msg>FIX: Remove unused code.<commit_after>require 'active_record/connection_adapters/abstract_adapter'
require 'active_record/connection_adapters/postgresql_adapter'
require 'discourse'

class PostgreSQLFallbackHandler
  include Singleton

  def initialize
    @masters_down = {}
    @mutex = Mutex.new
  end

  def verify_master
    synchronize { return if @thread && @thread.alive? }

    @thread = Thread.new do
      while true do
        begin
          thread = Thread.new { initiate_fallback_to_master }
          thread.join
          break if synchronize { @masters_down.empty? }
          sleep 10
        ensure
          thread.kill
        end
      end
    end
  end

  def master_down?
    synchronize { @masters_down[namespace] }
  end

  def master_down=(args)
    synchronize { @masters_down[namespace] = args }
  end

  def master_up(namespace)
    synchronize { @masters_down.delete(namespace) }
  end

  def initiate_fallback_to_master
    @masters_down.keys.each do |key|
      RailsMultisite::ConnectionManagement.with_connection(key) do
        begin
          logger.warn "#{log_prefix}: Checking master server..."
          connection = ActiveRecord::Base.postgresql_connection(config)

          if connection.active?
            connection.disconnect!
            ActiveRecord::Base.clear_all_connections!
            logger.warn "#{log_prefix}: Master server is active. Reconnecting..."

            self.master_up(key)
            Discourse.disable_readonly_mode
          end
        rescue => e
          logger.warn "#{log_prefix}: Connection to master PostgreSQL server failed with '#{e.message}'"
        end
      end
    end
  end

  # Use for testing
  def setup!
    @masters_down = {}
  end

  private

  def config
    ActiveRecord::Base.connection_config
  end

  def logger
    Rails.logger
  end

  def log_prefix
    "#{self.class} [#{namespace}]"
  end

  def namespace
    RailsMultisite::ConnectionManagement.current_db
  end

  def synchronize
    @mutex.synchronize { yield }
{'fraction_non_alphanumeric': 0.07844092570036541, 'fraction_numerical': 0.00097442143727162, 'mean_word_length': 2.542709232096635, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6203304', 'n_tokens_mistral': 1232, 'n_tokens_neox': 1192, 'n_words': 281}