Class: Sidekiq::DeadSet

Inherits:
JobSet show all
Defined in:
lib/sidekiq/api.rb

Overview

The set of dead jobs within Sidekiq. Dead jobs have failed all of their retries and are helding in this set pending some sort of manual fix. They will be removed after 6 months (dead_timeout) if not.

Instance Attribute Summary

Attributes inherited from SortedSet

#Name, #name

Instance Method Summary collapse

Methods inherited from JobSet

#each, #fetch, #find_job, #kill_all, #pop_each, #retry_all, #schedule

Methods inherited from SortedSet

#clear, #scan, #size

Constructor Details

#initializeDeadSet

Returns a new instance of DeadSet.



827
828
829
# File 'lib/sidekiq/api.rb', line 827

def initialize
  super("dead")
end

Instance Method Details

#kill(message, opts = {}) ⇒ Object

Add the given job to the Dead set.

Parameters:

  • message (String)

    the job data as JSON

  • opts (Hash) (defaults to: {})

    a customizable set of options

Options Hash (opts):

  • :notify_failure (Boolean) — default: true

    Whether death handlers should be called

  • :trim (Boolean) — default: true

    Whether Sidekiq should trim the structure to keep it within configuration

  • :ex (Exception) — default: RuntimeError

    An exception to pass to the death handlers



848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
# File 'lib/sidekiq/api.rb', line 848

def kill(message, opts = {})
  now = Time.now.to_f
  Sidekiq.redis do |conn|
    conn.zadd(name, now.to_s, message)
  end

  trim if opts[:trim] != false

  if opts[:notify_failure] != false
    job = Sidekiq.load_json(message)
    if opts[:ex]
      ex = opts[:ex]
    else
      ex = RuntimeError.new("Job killed by API")
      ex.set_backtrace(caller)
    end
    Sidekiq.default_configuration.death_handlers.each do |handle|
      handle.call(job, ex)
    end
  end
  true
end

#trimObject

Trim dead jobs which are over our storage limits



832
833
834
835
836
837
838
839
840
841
# File 'lib/sidekiq/api.rb', line 832

def trim
  hash = Sidekiq.default_configuration
  now = Time.now.to_f
  Sidekiq.redis do |conn|
    conn.multi do |transaction|
      transaction.zremrangebyscore(name, "-inf", now - hash[:dead_timeout_in_seconds])
      transaction.zremrangebyrank(name, 0, - hash[:dead_max_jobs])
    end
  end
end