Class: RuboCop::Cop::Sidekiq::DatabaseConnectionLeak

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sidekiq/database_connection_leak.rb

Overview

Checks for explicit ActiveRecord connection usage in Sidekiq jobs.

Examples:

# bad
ActiveRecord::Base.connection.execute('SELECT 1')

# good
ActiveRecord::Base.connection_pool.with_connection { |conn| conn.execute('SELECT 1') }

Constant Summary collapse

MSG =
'Avoid using ActiveRecord::Base.connection directly in jobs. Use connection_pool.with_connection.'

Instance Method Summary collapse

Methods included from Sidekiq::Language

#active_job_class?, #perform_call?, #sidekiq_include?, #sidekiq_options_call?

Instance Method Details

#on_def(node) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubocop/cop/sidekiq/database_connection_leak.rb', line 18

def on_def(node)
  return unless node.method?(:perform)
  return unless in_sidekiq_job?(node)

  node.each_descendant(:send) do |send|
    receiver = send.receiver
    next unless receiver&.const_name == 'ActiveRecord::Base'
    next unless send.method?(:connection)

    add_offense(send)
  end
end