Class: RuboCop::Cop::SidekiqEnt::LeaderElectionWithoutBlock

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sidekiq_ent/leader_election_without_block.rb

Overview

Checks for potentially problematic leader election usage.

Using Sidekiq.leader? for long-running operations can be problematic if leadership changes during execution. Prefer delegating work to a job.

Examples:

# bad - long-running operation in leader check
if Sidekiq.leader?
  do_long_running_work
end

# good - enqueue job for leader work
if Sidekiq.leader?
  LeaderOnlyJob.perform_async
end

Constant Summary collapse

MSG =
'Avoid long-running operations in leader checks. ' \
'Consider delegating work to a job.'

Instance Method Summary collapse

Methods inherited from Base

#limiter_creation?, #unique_for_option?, #unique_until_option?

Methods included from Sidekiq::Language

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

Instance Method Details

#if_leader_condition?(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/sidekiq_ent/leader_election_without_block.rb', line 33

def_node_matcher :if_leader_condition?, <<~PATTERN
  (if (send (const {nil? cbase} :Sidekiq) :leader?) $_ $_)
PATTERN

#leader_check?(node) ⇒ Object



28
29
30
# File 'lib/rubocop/cop/sidekiq_ent/leader_election_without_block.rb', line 28

def_node_matcher :leader_check?, <<~PATTERN
  (send (const {nil? cbase} :Sidekiq) :leader?)
PATTERN

#on_if(node) ⇒ Object



37
38
39
40
41
42
43
# File 'lib/rubocop/cop/sidekiq_ent/leader_election_without_block.rb', line 37

def on_if(node)
  if_leader_condition?(node) do |then_branch, _else_branch|
    return unless then_branch

    add_offense(node) if contains_non_job_calls?(then_branch)
  end
end