Class: SidekiqUniqueJobs::OnConflict::Reject

Inherits:
Strategy
  • Object
show all
Includes:
Timing
Defined in:
lib/sidekiq_unique_jobs/on_conflict/reject.rb

Overview

Strategy to send jobs to dead queue

Author:

Instance Attribute Summary

Attributes inherited from Strategy

#item, #redis_pool

Instance Method Summary collapse

Methods included from Timing

clock_stamp, now_f, time_source, timed

Methods inherited from Strategy

#initialize, #replace?

Methods included from Script::Caller

call_script, debug_lua, do_call, extract_args, max_history, normalize_argv, now_f, redis_version

Methods included from Logging

#build_message, included, #log_debug, #log_error, #log_fatal, #log_info, #log_warn, #logger, #logging_context, #with_configured_loggers_context, #with_logging_context

Methods included from JSON

dump_json, load_json, safe_load_json

Constructor Details

This class inherits a constructor from SidekiqUniqueJobs::OnConflict::Strategy

Instance Method Details

#callObject

Send jobs to dead queue



12
13
14
15
16
17
18
19
20
# File 'lib/sidekiq_unique_jobs/on_conflict/reject.rb', line 12

def call
  log_info { "Adding dead #{item[CLASS]} job #{item[JID]}" }

  if kill_with_options?
    kill_job_with_options
  else
    kill_job_without_options
  end
end

#deadsetSidekiq::Deadset

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

An instance of Sidekiq::Deadset

Returns:

  • (Sidekiq::Deadset)

    >



69
70
71
# File 'lib/sidekiq_unique_jobs/on_conflict/reject.rb', line 69

def deadset
  @deadset ||= Sidekiq::DeadSet.new
end

#kill_job_with_optionsvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Executes the kill instructions with arguments



59
60
61
# File 'lib/sidekiq_unique_jobs/on_conflict/reject.rb', line 59

def kill_job_with_options
  deadset.kill(payload, notify_failure: false)
end

#kill_job_without_optionsvoid

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

This method returns an undefined value.

Executes the kill instructions without arguments



49
50
51
# File 'lib/sidekiq_unique_jobs/on_conflict/reject.rb', line 49

def kill_job_without_options
  deadset.kill(payload)
end

#kill_with_options?true, false

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Sidekiq version compatibility check

Returns:

  • (true)

    when Sidekiq::Deadset#kill takes more than 1 argument

  • (false)

    when Sidekiq::Deadset#kill does not take multiple arguments



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/sidekiq_unique_jobs/on_conflict/reject.rb', line 30

def kill_with_options?
  kill_arity = Sidekiq::DeadSet.instance_method(:kill).arity
  # Method#arity returns:
  #   1. a nonnegative number for methods that take a fixed number of arguments.
  #   2. A negative number if it takes a variable number of arguments.
  # Keyword arguments are considered a single argument, and are considered optional unless one of the kwargs is
  # required.
  # Therefore, to determine if `Sidekiq::DeadSet#kill` accepts options beyond the single positional payload
  # argument, we need to check whether the absolute value of the arity is greater than 1.
  # See: https://apidock.com/ruby/Method/arity
  kill_arity > 1 || kill_arity < -1
end

#payloadString

The Sidekiq job hash as JSON

Returns:

  • (String)

    a JSON formatted string



79
80
81
# File 'lib/sidekiq_unique_jobs/on_conflict/reject.rb', line 79

def payload
  @payload ||= dump_json(item)
end