Class: RuboCop::Cop::Sequra::AsyncJobPattern

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sequra/async_job_pattern.rb

Overview

Detects Sidekiq jobs that haven’t adopted the ApplicationOperation pattern.

A job is considered migrated when it meets ALL conditions:

  1. Has exactly ONE ‘*Operation.call` invocation (delegates to single operation)

  2. Class length ≤ 10 “smart lines” (using CountAsOne semantics)

A job is considered unmigrated (offense) when:

  • Zero ‘Operation.call` → business logic lives in the job itself

  • Multiple ‘Operation.call` → job orchestrates multiple operations

  • Exceeds class length → job has too much logic beyond delegation

Examples:

# bad - no operation delegation
class MyJob < ApplicationJob
  def perform(id)
    user = User.find(id)
    user.update!(status: :active)
    UserMailer.welcome(user).deliver_later
  end
end

# bad - multiple operations
class MyJob < ApplicationJob
  def perform(id, type)
    if type == :a
      OperationA.call(id: id)
    else
      OperationB.call(id: id)
    end
  end
end

# good - single operation delegation
class MyJob < ApplicationJob
  def perform(id)
    MyOperation.call(id:)
  end
end

Constant Summary collapse

MSG =
"Sidekiq job should delegate to exactly one Operation".freeze
MSG_CLASS_LENGTH =
"Job has too much logic to be a simple Operation delegate. " \
"Consider moving logic into the Operation".freeze
MAX_CLASS_LENGTH =
10
COUNT_AS_ONE =
["array", "hash", "heredoc", "method_call"].freeze
OPERATION_CLASS_PATTERN =
/Operation$/

Instance Method Summary collapse

Instance Method Details

#on_class(node) ⇒ Object



68
69
70
71
72
73
# File 'lib/rubocop/cop/sequra/async_job_pattern.rb', line 68

def on_class(node)
  return unless sidekiq_job?(node)

  check_operation_delegation(node)
  check_class_length(node)
end