Class: RuboCop::Cop::SidekiqPro::NestedBatchWithoutParent

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

Overview

Checks that nested batches have proper parent relationship.

When creating nested batches inside a batch.jobs block, the child batch should reference the parent for proper tracking.

Examples:

# bad - nested batch without parent reference
class ProcessBatchJob
  include Sidekiq::Job

  def perform
    batch.jobs do
      child_batch = Sidekiq::Batch.new
      child_batch.jobs do
        SomeJob.perform_async
      end
    end
  end
end

# good - explicit parent reference
class ProcessBatchJob
  include Sidekiq::Job

  def perform
    parent_batch = batch
    parent_batch.jobs do
      child_batch = Sidekiq::Batch.new(parent_batch.bid)
      child_batch.jobs do
        SomeJob.perform_async
      end
    end
  end
end

Constant Summary collapse

MSG =
'Nested batch should reference parent batch for proper tracking.'

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_block(node) ⇒ Object Also known as: on_numblock



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

def on_block(node)
  return unless batch_jobs_block?(node)

  node.body&.each_descendant(:send) do |send_node|
    add_offense(send_node) if batch_new?(send_node) && send_node.arguments.empty?
  end
end