Class: RuboCop::Cop::SidekiqPro::EmptyBatch

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

Overview

Checks for batch.jobs blocks that might be empty.

Empty batches cause errors in Sidekiq Pro versions before 7.1. Even in newer versions, creating empty batches is often unintentional.

Examples:

# bad - block might add no jobs
batch = Sidekiq::Batch.new
batch.jobs do
  items.each do |item|
    ProcessJob.perform_async(item.id) if item.active?
  end
end

# good - ensure at least one job is added or check before creating batch
active_items = items.select(&:active?)
if active_items.any?
  batch = Sidekiq::Batch.new
  batch.jobs do
    active_items.each do |item|
      ProcessJob.perform_async(item.id)
    end
  end
end

Constant Summary collapse

MSG =
'Batch jobs block may be empty. Ensure jobs are added or guard against empty batches.'
CONDITIONAL_METHODS =
%i[if unless case].freeze

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



36
37
38
39
40
# File 'lib/rubocop/cop/sidekiq_pro/empty_batch.rb', line 36

def on_block(node)
  batch_jobs_block?(node) do |_receiver, body|
    add_offense(node) if potentially_empty_block?(body)
  end
end