Class: RuboCop::Cop::SidekiqPro::BatchRetryInCallback

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

Overview

Checks that jobs enqueued in batch callbacks have retry enabled.

When a batch succeeds but the callback job fails without retry, the overall workflow may be incomplete without any automatic recovery.

Examples:

# bad - callback enqueues a job without retry
class MyCallback
  def on_success(status, options)
    FinalizeJob.perform_async(options['order_id'])
  end
end

class FinalizeJob
  include Sidekiq::Job
  sidekiq_options retry: false
end

# good - callback enqueues a job with retry enabled
class FinalizeJob
  include Sidekiq::Job
  sidekiq_options retry: 5
end

Constant Summary collapse

MSG =
'Jobs enqueued in batch callbacks should have retry enabled.'

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

#callback_method?(node) ⇒ Object



34
35
36
# File 'lib/rubocop/cop/sidekiq_pro/batch_retry_in_callback.rb', line 34

def_node_matcher :callback_method?, "(def {:on_complete :on_success :on_death} (args _ _) ...)\n"

#on_def(node) ⇒ Object Also known as: on_defs



43
44
45
46
47
48
49
# File 'lib/rubocop/cop/sidekiq_pro/batch_retry_in_callback.rb', line 43

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

  node.each_descendant(:send) do |send_node|
    add_offense(send_node) if perform_async_call?(send_node)
  end
end

#perform_async_call?(node) ⇒ Object



39
40
41
# File 'lib/rubocop/cop/sidekiq_pro/batch_retry_in_callback.rb', line 39

def_node_matcher :perform_async_call?, "(send (const ...) {:perform_async :perform_in :perform_at} ...)\n"