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.



289
290
291
292
293
# File 'lib/sidekiq/api.rb', line 289

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.



286
287
288
# File 'lib/sidekiq/api.rb', line 286

def item
  @item
end

#valueObject (readonly)

Returns the value of attribute value.



287
288
289
# File 'lib/sidekiq/api.rb', line 287

def value
  @value
end

Instance Method Details

#[](name) ⇒ Object



373
374
375
# File 'lib/sidekiq/api.rb', line 373

def [](name)
  @item[name]
end

#argsObject



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

def args
  @item['args']
end

#created_atObject



351
352
353
# File 'lib/sidekiq/api.rb', line 351

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

#deleteObject

Remove this job from the queue.



366
367
368
369
370
371
# File 'lib/sidekiq/api.rb', line 366

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

#display_argsObject



319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'lib/sidekiq/api.rb', line 319

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"
              job_args = @item['wrapped'] ? args[0]["arguments"] : []
              if 'ActionMailer::DeliveryJob' == (@item['wrapped'] || args[0])
               # remove MailerClass, mailer_method and 'deliver_now'
               job_args.drop(3)
              else
               job_args
              end
            else
              args
            end
end

#display_classObject



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
# File 'lib/sidekiq/api.rb', line 299

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"
               job_class = @item['wrapped'] || args[0]
               if 'ActionMailer::DeliveryJob' == job_class
                 # MailerClass#mailer_method
                 args[0]['arguments'][0..1].join('#')
               else
                job_class
               end
             else
               klass
             end
end

#enqueued_atObject



347
348
349
# File 'lib/sidekiq/api.rb', line 347

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

#jidObject



343
344
345
# File 'lib/sidekiq/api.rb', line 343

def jid
  @item['jid']
end

#klassObject



295
296
297
# File 'lib/sidekiq/api.rb', line 295

def klass
  @item['class']
end

#latencyObject



359
360
361
362
# File 'lib/sidekiq/api.rb', line 359

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

#queueObject



355
356
357
# File 'lib/sidekiq/api.rb', line 355

def queue
  @queue
end