Class: RuboCop::Cop::SidekiqEnt::UniqueJobTooShortTTL

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

Overview

Checks that unique jobs have sufficient TTL.

A too short unique_for TTL may expire during retries, allowing duplicate jobs to be enqueued while the original is still retrying.

Examples:

# bad - TTL too short
class MyJob
  include Sidekiq::Job
  sidekiq_options unique_for: 30
end

# good - adequate TTL
class MyJob
  include Sidekiq::Job
  sidekiq_options unique_for: 3600
end

Constant Summary collapse

MSG =
'Unique job TTL is too short (minimum: %<minimum>s seconds). ' \
'Consider increasing `unique_for` to cover retry period.'
MINIMUM_TTL =
60

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

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



35
36
37
38
39
40
41
42
# File 'lib/rubocop/cop/sidekiq_ent/unique_job_too_short_ttl.rb', line 35

def on_send(node)
  unique_for_value(node) do |value_node|
    ttl = extract_seconds(value_node)
    return unless ttl && ttl < minimum_ttl

    add_offense(value_node, message: format(MSG, minimum: minimum_ttl))
  end
end

#unique_for_value(node) ⇒ Object



31
32
33
# File 'lib/rubocop/cop/sidekiq_ent/unique_job_too_short_ttl.rb', line 31

def_node_matcher :unique_for_value, <<~PATTERN
  (send nil? :sidekiq_options (hash <(pair (sym :unique_for) $_) ...>))
PATTERN