Class: ActiveRecordStats::SidekiqServerMiddleware

Inherits:
Object
  • Object
show all
Defined in:
lib/active_record_stats/sidekiq_server_middleware.rb

Instance Method Summary collapse

Instance Method Details

#call(worker, job, queue) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/active_record_stats/sidekiq_server_middleware.rb', line 5

def call(worker, job, queue)
  totals = {}

  gather_sql = ->(_name, _started_at, _finished_at, _unique_id, payload) {
    return if payload[:name] == 'SCHEMA' || payload[:sql].blank?
    return unless type = ActiveRecordStats.statement_type(payload[:sql])
    totals[type] ||= 0
    totals[type] += 1
  }

  sub = ActiveSupport::Notifications.subscribe('sql.active_record', &gather_sql)
  yield

ensure
  ActiveSupport::Notifications.unsubscribe(sub)
  emit(worker.class.to_s, totals.dup)
end