Class: RuboCop::Cop::SidekiqPro::BatchStatusPolling

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

Overview

Checks for batch status polling anti-patterns.

Polling batch status in a loop wastes resources and can cause issues. Use batch callbacks instead.

Examples:

# bad - polling for status
loop do
  status = Sidekiq::Batch::Status.new(bid)
  break if status.complete?
  sleep 5
end

# good - use callbacks
batch = Sidekiq::Batch.new
batch.on(:complete, MyCallback)
batch.jobs do
  SomeJob.perform_async
end

Constant Summary collapse

MSG =
'Avoid polling batch status. Use batch callbacks instead.'

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

#batch_status_new?(node) ⇒ Object



30
31
32
# File 'lib/rubocop/cop/sidekiq_pro/batch_status_polling.rb', line 30

def_node_matcher :batch_status_new?, <<~PATTERN
  (send (const (const (const {nil? cbase} :Sidekiq) :Batch) :Status) :new ...)
PATTERN

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



39
40
41
42
43
44
# File 'lib/rubocop/cop/sidekiq_pro/batch_status_polling.rb', line 39

def on_send(node)
  return unless batch_status_new?(node)
  return unless inside_loop?(node)

  add_offense(node)
end

#status_complete_check?(node) ⇒ Object



35
36
37
# File 'lib/rubocop/cop/sidekiq_pro/batch_status_polling.rb', line 35

def_node_matcher :status_complete_check?, <<~PATTERN
  (send _ {:complete? :pending :failures :total} ...)
PATTERN