Class: Sidekiq::Job

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

Overview

Encapsulates a pending job within a Sidekiq queue or sorted set.

The job should be considered immutable but may be removed from the queue via Job#delete.

Direct Known Subclasses

SortedEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, queue_name = nil) ⇒ Job

Returns a new instance of Job.



268
269
270
271
272
# File 'lib/sidekiq/api.rb', line 268

def initialize(item, queue_name=nil)
  @value = item
  @item = item.is_a?(Hash) ? item : Sidekiq.load_json(item)
  @queue = queue_name || @item['queue']
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



266
267
268
# File 'lib/sidekiq/api.rb', line 266

def item
  @item
end

Instance Method Details

#[](name) ⇒ Object



339
340
341
# File 'lib/sidekiq/api.rb', line 339

def [](name)
  @item.__send__(:[], name)
end

#argsObject



306
307
308
# File 'lib/sidekiq/api.rb', line 306

def args
  @item['args']
end

#created_atObject



318
319
320
# File 'lib/sidekiq/api.rb', line 318

def created_at
  Time.at(@item['created_at'] || @item['enqueued_at'] || 0).utc
end

#deleteObject

Remove this job from the queue.



332
333
334
335
336
337
# File 'lib/sidekiq/api.rb', line 332

def delete
  count = Sidekiq.redis do |conn|
    conn.lrem("queue:#{@queue}", 1, @value)
  end
  count != 0
end

#display_argsObject



292
293
294
295
296
297
298
299
300
301
302
303
304
# File 'lib/sidekiq/api.rb', line 292

def display_args
  # Unwrap known wrappers so they show up in a human-friendly manner in the Web UI
  @args ||= case klass
            when /\ASidekiq::Extensions::Delayed/
              safe_load(args[0], args) do |_, _, arg|
                arg
              end
            when "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
              @item['wrapped'] ? args[0]["arguments"] : []
            else
              args
            end
end

#display_classObject



278
279
280
281
282
283
284
285
286
287
288
289
290
# File 'lib/sidekiq/api.rb', line 278

def display_class
  # Unwrap known wrappers so they show up in a human-friendly manner in the Web UI
  @klass ||= case klass
             when /\ASidekiq::Extensions::Delayed/
               safe_load(args[0], klass) do |target, method, _|
                 "#{target}.#{method}"
               end
             when "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
               @item['wrapped'] || args[0]
             else
               klass
             end
end

#enqueued_atObject



314
315
316
# File 'lib/sidekiq/api.rb', line 314

def enqueued_at
  @item['enqueued_at'] ? Time.at(@item['enqueued_at']).utc : nil
end

#jidObject



310
311
312
# File 'lib/sidekiq/api.rb', line 310

def jid
  @item['jid']
end

#klassObject



274
275
276
# File 'lib/sidekiq/api.rb', line 274

def klass
  @item['class']
end

#latencyObject



326
327
328
# File 'lib/sidekiq/api.rb', line 326

def latency
  Time.now.to_f - (@item['enqueued_at'] || @item['created_at'])
end

#queueObject



322
323
324
# File 'lib/sidekiq/api.rb', line 322

def queue
  @queue
end