Class: RuboCop::Cop::Sidekiq::ThreadInJob

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/sidekiq/thread_in_job.rb

Overview

Checks for thread creation inside Sidekiq jobs.

Creating threads inside Sidekiq jobs is problematic because:

  • Sidekiq already manages its own thread pool
  • Threads may not complete before the job finishes
  • It can lead to resource leaks and unexpected behavior

Examples:

# bad
class MyJob
  include Sidekiq::Job

  def perform
    Thread.new { do_work }
  end
end

# good - use separate jobs instead
class MyJob
  include Sidekiq::Job

  def perform
    SubJob.perform_async
  end
end

Constant Summary collapse

MSG =
'Do not create threads inside Sidekiq jobs. ' \
"Use separate jobs or Sidekiq's built-in concurrency instead."
RESTRICT_ON_SEND =
i[new fork].freeze

Instance Method Summary collapse

Methods included from Sidekiq::Language

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

Instance Method Details

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



43
44
45
46
47
48
# File 'lib/rubocop/cop/sidekiq/thread_in_job.rb', line 43

def on_send(node)
  return unless thread_creation?(node)
  return unless in_sidekiq_job?(node)

  add_offense(node)
end

#thread_creation?(node) ⇒ Object



39
40
41
# File 'lib/rubocop/cop/sidekiq/thread_in_job.rb', line 39

def_node_matcher :thread_creation?, "(send (const {nil? cbase} :Thread) {:new :fork} ...)\n"