Sha256: 84edf7bd512712b73b7dba7e9c761fab09a440534ed7a5a4e6605934d7aedb97

Contents?: true

Size: 1.38 KB

Versions: 5

Compression:

Stored size: 1.38 KB

Contents

require "set"

module ActiveRecord
  module SqlAnalyzer
    class BackgroundProcessor
      def initialize
        @queue = Queue.new
      end

      def <<(event)
        processor_thread
        @queue << event
      end

      private

      MUTEX = Mutex.new

      def process_queue
        event = @queue.pop

        event[:caller] = SqlAnalyzer.config[:backtrace_filter_proc].call(event[:caller])
        event[:sql] = SqlAnalyzer.config[:sql_redactor_complex_proc].call(event[:sql].dup)

        logger = event.delete(:logger)
        logger.filter_event(event)
        logger.log(event)
      end

      def processor_thread
        # Avoid grabbing a mutex unless we really need to
        return if @thread && @thread.alive?

        MUTEX.synchronize do
          # Double check to avoid a race condition
          return if @thread && @thread.alive?

          @thread = Thread.new do
            Rails.logger.info "[SQL-Analyzer] Starting background query thread id #{Thread.current.object_id} in pid #{Process.pid}"

            begin
              loop do
                process_queue
              end
            rescue => ex
              Rails.logger.warn "[SQL-Analyzer] Exception in thread #{Thread.current.object_id}: #{ex.class}, #{ex.message}"
              Rails.logger.warn "[SQL-Analyzer] #{ex.backtrace.join(", ")}"
            end
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_record-sql_analyzer-0.1.0 lib/active_record/sql_analyzer/background_processor.rb
active_record-sql_analyzer-0.0.8 lib/active_record/sql_analyzer/background_processor.rb
active_record-sql_analyzer-0.0.7 lib/active_record/sql_analyzer/background_processor.rb
active_record-sql_analyzer-0.0.6 lib/active_record/sql_analyzer/background_processor.rb
active_record-sql_analyzer-0.0.5 lib/active_record/sql_analyzer/background_processor.rb