Class: RuboCop::Cop::SidekiqPro::BatchCallbackMethod

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sidekiq_pro/batch_callback_method.rb

Overview

Checks that batch callback methods are named correctly.

Sidekiq Pro batch callbacks require specific method names:

  • :complete callback requires on_complete method
  • :success callback requires on_success method
  • :death callback requires on_death method

Examples:

# bad - callback method name is incorrect
class MyCallback
  def complete(status, options)
  end
end
batch.on(:complete, MyCallback)

# good
class MyCallback
  def on_complete(status, options)
  end
end
batch.on(:complete, MyCallback)

# good - method specified as string
batch.on(:complete, 'MyCallback#handle_complete')

Constant Summary collapse

CALLBACK_METHODS =
{
  complete: :on_complete,
  success: :on_success,
  death: :on_death
}.freeze
MSG =
'Batch callback method should be named `%<expected>s`, not `%<actual>s`.'

Instance Method Summary collapse

Methods inherited from Base

#batch_description_set?, #batch_jobs_block?, #batch_new?, #batch_on_callback?

Methods included from Sidekiq::Language

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

Instance Method Details

#on_def(node) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/sidekiq_pro/batch_callback_method.rb', line 40

def on_def(node)
  return unless potential_callback_method?(node)

  method_name = node.method_name
  expected_name = expected_method_name_for(method_name)
  return unless expected_name

  add_offense(node.loc.name, message: format(MSG, expected: expected_name, actual: method_name))
end