class Sidekiq::Queue
Represents a queue within Sidekiq
. Allows enumeration of all jobs within the queue and deletion of jobs. NB: this queue data is real-time and is changing within Redis moment by moment.
queue = Sidekiq::Queue.new("mailer") queue.each do |job| job.klass # => 'MyWorker' job.args # => [1, 2, 3] job.delete if job.jid == 'abcdef1234567890' end
Attributes
Public Class Methods
Fetch all known queues within Redis.
@return [Array<Sidekiq::Queue>]
# File lib/sidekiq/api.rb, line 249 def self.all Sidekiq.redis { |c| c.sscan_each("queues").to_a }.sort.map { |q| Sidekiq::Queue.new(q) } end
@param name [String] the name of the queue
# File lib/sidekiq/api.rb, line 256 def initialize(name = "default") @name = name.to_s @rname = "queue:#{name}" end
Public Instance Methods
delete all jobs within this queue @return [Boolean] true
# File lib/sidekiq/api.rb, line 326 def clear Sidekiq.redis do |conn| conn.multi do |transaction| transaction.unlink(@rname) transaction.srem("queues", [name]) end end true end
# File lib/sidekiq/api.rb, line 290 def each initial_size = size deleted_size = 0 page = 0 page_size = 50 loop do range_start = page * page_size - deleted_size range_end = range_start + page_size - 1 entries = Sidekiq.redis { |conn| conn.lrange @rname, range_start, range_end } break if entries.empty? page += 1 entries.each do |entry| yield JobRecord.new(entry, @name) end deleted_size = initial_size - size end end
Find the job with the given JID within this queue.
This is a *slow, inefficient* operation. Do not use under normal conditions.
@param jid [String] the job_id to look for @return [Sidekiq::JobRecord] @return [nil] if not found
# File lib/sidekiq/api.rb, line 320 def find_job(jid) detect { |j| j.jid == jid } end
Calculates this queue's latency, the difference in seconds since the oldest job in the queue was enqueued.
@return [Float] in seconds
# File lib/sidekiq/api.rb, line 279 def latency entry = Sidekiq.redis { |conn| conn.lrange(@rname, -1, -1) }.first return 0 unless entry job = Sidekiq.load_json(entry) now = Time.now.to_f thence = job["enqueued_at"] || now now - thence end
@return [Boolean] if the queue is currently paused
# File lib/sidekiq/api.rb, line 270 def paused? false end
The current size of the queue within Redis. This value is real-time and can change between calls.
@return [Integer] the size
# File lib/sidekiq/api.rb, line 265 def size Sidekiq.redis { |con| con.llen(@rname) } end