lib/sidekiq/api.rb in sidekiq-3.2.1 vs lib/sidekiq/api.rb in sidekiq-3.2.2

- old
+ new

@@ -314,10 +314,27 @@ msg['retry_count'] = msg['retry_count'] - 1 Sidekiq::Client.push(msg) end end + ## + # Place job in the dead set + def kill + raise 'Kill not available on jobs which have not failed' unless item['failed_at'] + remove_job do |message| + Sidekiq.logger.info { "Killing job #{message['jid']}" } + now = Time.now.to_f + Sidekiq.redis do |conn| + conn.multi do + conn.zadd('dead', now, message) + conn.zremrangebyscore('dead', '-inf', now - DeadSet::DEAD_JOB_TIMEOUT) + conn.zremrangebyrank('dead', 0, - DeadSet::MAX_JOBS) + end + end + end + end + private def remove_job Sidekiq.redis do |conn| results = conn.multi do @@ -336,16 +353,17 @@ msg['jid'] == jid else false end end - message = hash[true].first - yield message + msg = hash.fetch(true, []).first + yield msg if msg + # push the rest back onto the sorted set conn.multi do - hash[false].each do |message| + hash.fetch(false, []).each do |message| conn.zadd(parent.name, score.to_f.to_s, message) end end end end @@ -496,11 +514,17 @@ each(&:retry) end end end + ## + # Allows enumeration of dead jobs within Sidekiq. + # class DeadSet < JobSet + DEAD_JOB_TIMEOUT = 180 * 24 * 60 * 60 # 6 months + MAX_JOBS = 10_000 + def initialize super 'dead' end def retry_all @@ -573,9 +597,17 @@ # 'beat' => <last heartbeat>, # } class Process def initialize(hash) @attribs = hash + end + + def tag + self['tag'] + end + + def labels + Array(self['labels']) end def [](key) @attribs[key] end