Class: RuboCop::Cop::Sidekiq::RedisInJob

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

Overview

Checks for direct Redis connections inside Sidekiq jobs.

Examples:

# bad
redis = Redis.new

# good
Sidekiq.redis { |conn| conn.get('key') }

Constant Summary collapse

MSG =
'Use Sidekiq.redis instead of creating a new Redis connection in jobs.'

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
# File 'lib/rubocop/cop/sidekiq/redis_in_job.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 == 'Redis' && send.method?(:new)

    add_offense(send)
  end
end