Sha256: 76d31a50d95212980ba0feadcf1be615d7024f20d5d892e99ff20c4ef647bac1

Contents?: true

Size: 1.42 KB

Versions: 5

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module ActiveRecordQueryCounter
  # Sidekiq middleware to count queries on a job.
  #
  # Notification thresholds can be set per worker with the `active_record_query_counter.thresholds` key in the
  # `sidekiq_options` hash. Valid keys are:
  # * `:query_time` - The minimum query time to send a notification for.
  # * `:row_count` - The minimum row count to send a notification for.
  # * `:transaction_time` - The minimum transaction time to send a notification for.
  # * `:transaction_count` - The minimum transaction count to send a notification for.
  #
  # Thresholds can be disabled for a worker by setting `active_record_query_counter.thresholds` to `false`.
  #
  # @example
  #
  #   class MyWorker
  #     include Sidekiq::Worker
  #
  #     sidekiq_options active_record_query_counter: {thresholds: {query_time: 1.5}}
  #
  #     def perform
  #       # ...
  #     end
  #   end
  class SidekiqMiddleware
    if defined?(Sidekiq::ServerMiddleware)
      include Sidekiq::ServerMiddleware
    end

    def call(job_instance, job_payload, queue)
      ActiveRecordQueryCounter.count_queries do
        thresholds = job_payload.dig("active_record_query_counter", "thresholds")
        if thresholds.is_a?(Hash)
          ActiveRecordQueryCounter.thresholds.set(thresholds)
        elsif thresholds == false
          ActiveRecordQueryCounter.thresholds.clear
        end

        yield
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
active_record_query_counter-2.3.0 lib/active_record_query_counter/sidekiq_middleware.rb
active_record_query_counter-2.2.1 lib/active_record_query_counter/sidekiq_middleware.rb
active_record_query_counter-2.2.0 lib/active_record_query_counter/sidekiq_middleware.rb
active_record_query_counter-2.1.0 lib/active_record_query_counter/sidekiq_middleware.rb
active_record_query_counter-2.0.0 lib/active_record_query_counter/sidekiq_middleware.rb