Class: Sidekiq::Queue

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/sidekiq/api.rb

Overview

Encapsulates a queue within Sidekiq. Allows enumeration of all jobs within the queue and deletion of jobs.

queue = Sidekiq::Queue.new("mailer")
queue.each do |job|
  job.klass # => 'MyWorker'
  job.args # => [1, 2, 3]
  job.delete if job.jid == 'abcdef1234567890'
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name = "default") ⇒ Queue

Returns a new instance of Queue.


115
116
117
118
# File 'lib/sidekiq/api.rb', line 115

def initialize(name="default")
  @name = name
  @rname = "queue:#{name}"
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name


113
114
115
# File 'lib/sidekiq/api.rb', line 113

def name
  @name
end

Instance Method Details

#clearObject


140
141
142
143
144
145
146
147
# File 'lib/sidekiq/api.rb', line 140

def clear
  Sidekiq.redis do |conn|
    conn.multi do
      conn.del("queue:#{name}")
      conn.srem("queues", name)
    end
  end
end

#each(&block) ⇒ Object


124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/sidekiq/api.rb', line 124

def each(&block)
  page = 0
  page_size = 50

  loop do
    entries = Sidekiq.redis do |conn|
      conn.lrange @rname, page * page_size, (page * page_size) + page_size - 1
    end
    break if entries.empty?
    page += 1
    entries.each do |entry|
      block.call Job.new(entry, @name)
    end
  end
end

#sizeObject


120
121
122
# File 'lib/sidekiq/api.rb', line 120

def size
  Sidekiq.redis { |con| con.llen(@rname) }
end