Class: RuboCop::Cop::SidekiqEnt::PeriodicJobWithArguments

Inherits:
Base
  • Object
show all
Includes:
Sidekiq::Language
Defined in:
lib/rubocop/cop/sidekiq_ent/periodic_job_with_arguments.rb

Overview

Checks that periodic jobs do not require arguments without defaults.

Periodic jobs are scheduled by cron and cannot receive dynamic arguments unless specified via the args option in the periodic registration. A perform method requiring arguments will fail when called periodically.

Examples:

# bad - requires arguments
class HourlyReportJob
  include Sidekiq::Job

  def perform(user_id)
  end
end

# good - no arguments
class HourlyReportJob
  include Sidekiq::Job

  def perform
  end
end

# good - optional arguments with defaults
class HourlyReportJob
  include Sidekiq::Job

  def perform(scope = 'all')
  end
end

Constant Summary collapse

MSG =
'Periodic job `perform` should not require arguments. ' \
'Use optional arguments or the `args` option in periodic registration.'

Instance Method Summary collapse

Methods included from Sidekiq::Language

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

Methods inherited from Base

#limiter_creation?, #unique_for_option?, #unique_until_option?

Instance Method Details

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



48
49
50
51
52
53
# File 'lib/rubocop/cop/sidekiq_ent/periodic_job_with_arguments.rb', line 48

def on_send(node)
  periodic_register?(node) do |job_class_name|
    job_class_name = job_class_name.to_s
    check_job_class(node, job_class_name)
  end
end

#periodic_register?(node) ⇒ Object



44
45
46
# File 'lib/rubocop/cop/sidekiq_ent/periodic_job_with_arguments.rb', line 44

def_node_matcher :periodic_register?, <<~PATTERN
  (send _ :register (str _) {(str $_) (const ... $_)} ...)
PATTERN