Class: Sidekiq::Queue
- Inherits:
-
Object
- Object
- Sidekiq::Queue
- Includes:
- Enumerable, ApiUtils
- Defined in:
- lib/sidekiq/api.rb
Overview
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
Instance Attribute Summary collapse
-
#name ⇒ Object
(also: #id)
readonly
Returns the value of attribute name.
Class Method Summary collapse
-
.all ⇒ Array<Sidekiq::Queue>
Fetch all known queues within Redis.
Instance Method Summary collapse
-
#as_json(options = nil) ⇒ Object
private
:nodoc:.
-
#clear ⇒ Boolean
(also: #💣)
delete all jobs within this queue.
- #each ⇒ Object
-
#find_job(jid) ⇒ Sidekiq::JobRecord?
Find the job with the given JID within this queue.
-
#initialize(name = "default") ⇒ Queue
constructor
A new instance of Queue.
-
#latency ⇒ Float
Calculates this queue’s latency, the difference in seconds since the oldest job in the queue was enqueued.
-
#paused? ⇒ Boolean
If the queue is currently paused.
-
#size ⇒ Integer
The current size of the queue within Redis.
Methods included from ApiUtils
Constructor Details
#initialize(name = "default") ⇒ Queue
Returns a new instance of Queue.
301 302 303 304 |
# File 'lib/sidekiq/api.rb', line 301 def initialize(name = "default") @name = name.to_s @rname = "queue:#{name}" end |
Instance Attribute Details
#name ⇒ Object (readonly) Also known as: id
Returns the value of attribute name.
297 298 299 |
# File 'lib/sidekiq/api.rb', line 297 def name @name end |
Class Method Details
Instance Method Details
#as_json(options = nil) ⇒ Object
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.
:nodoc:
383 384 385 |
# File 'lib/sidekiq/api.rb', line 383 def as_json( = nil) {name: name} # 5336 end |
#clear ⇒ Boolean Also known as: 💣
delete all jobs within this queue
370 371 372 373 374 375 376 377 378 |
# File 'lib/sidekiq/api.rb', line 370 def clear Sidekiq.redis do |conn| conn.multi do |transaction| transaction.unlink(@rname) transaction.srem("queues", [name]) end end true end |
#each ⇒ Object
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 |
# File 'lib/sidekiq/api.rb', line 334 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_job(jid) ⇒ Sidekiq::JobRecord?
Find the job with the given JID within this queue.
This is a *slow, inefficient* operation. Do not use under normal conditions.
364 365 366 |
# File 'lib/sidekiq/api.rb', line 364 def find_job(jid) detect { |j| j.jid == jid } end |
#latency ⇒ Float
Calculates this queue’s latency, the difference in seconds since the oldest job in the queue was enqueued.
324 325 326 327 328 329 330 331 332 |
# File 'lib/sidekiq/api.rb', line 324 def latency entry = Sidekiq.redis { |conn| conn.lindex(@rname, -1) } return 0.0 unless entry job = Sidekiq.load_json(entry) calculate_latency(job) end |
#paused? ⇒ Boolean
Returns if the queue is currently paused.
315 316 317 |
# File 'lib/sidekiq/api.rb', line 315 def paused? false end |