Class: RuboCop::Cop::Sidekiq::ActiveRecordArgument

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

Overview

Checks for ActiveRecord objects passed to Sidekiq job methods.

Passing ActiveRecord objects to Sidekiq jobs is problematic because:

  • Objects cannot be properly serialized to JSON
  • The object may change or be deleted before the job runs
  • It increases Redis memory usage

Instead, pass the record's ID and reload it in the job.

Examples:

# bad
MyJob.perform_async(user)
MyJob.perform_async(User.find(1))
MyJob.perform_async(User.first)

# good
MyJob.perform_async(user.id)
MyJob.perform_async(user_id)

Constant Summary collapse

MSG =
'Do not pass ActiveRecord objects to Sidekiq jobs. ' \
'Pass the id and fetch the record in the job instead.'
RESTRICT_ON_SEND =
PerformMethods.all
FINDER_METHODS =
i[find find_by find_by! first last take where].freeze

Instance Method Summary collapse

Methods included from Sidekiq::Language

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

Instance Method Details

#active_record_finder?(node) ⇒ Object



41
42
43
# File 'lib/rubocop/cop/sidekiq/active_record_argument.rb', line 41

def_node_matcher :active_record_finder?, "(send (const ...) {\#{FINDER_METHODS.map(&:inspect).join(' ')}} ...)\n"

#chained_finder?(node) ⇒ Object



46
47
48
# File 'lib/rubocop/cop/sidekiq/active_record_argument.rb', line 46

def_node_matcher :chained_finder?, "(send (send (const ...) ...) {\#{FINDER_METHODS.map(&:inspect).join(' ')}} ...)\n"

#on_send(node) ⇒ Object Also known as: on_csend



50
51
52
53
54
# File 'lib/rubocop/cop/sidekiq/active_record_argument.rb', line 50

def on_send(node)
  sidekiq_perform_call?(node) do |args|
    check_arguments(args)
  end
end

#sidekiq_perform_call?(node) ⇒ Object



36
37
38
# File 'lib/rubocop/cop/sidekiq/active_record_argument.rb', line 36

def_node_matcher :sidekiq_perform_call?, "(send _ {\#{PerformMethods.all.map(&:inspect).join(' ')}} $...)\n"