class Sidekiq::Job

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.

Attributes

item[R]
value[R]

Public Class Methods

new(item, queue_name=nil) click to toggle source
# File lib/sidekiq/api.rb, line 318
def initialize(item, queue_name=nil)
  @args = nil
  @value = item
  @item = item.is_a?(Hash) ? item : parse(item)
  @queue = queue_name || @item['queue']
end

Public Instance Methods

[](name) click to toggle source
# File lib/sidekiq/api.rb, line 418
def [](name)
  # nil will happen if the JSON fails to parse.
  # We don't guarantee Sidekiq will work with bad job JSON but we should
  # make a best effort to minimize the damage.
  @item ? @item[name] : nil
end
args() click to toggle source
# File lib/sidekiq/api.rb, line 384
def args
  @args || @item['args']
end
created_at() click to toggle source
# File lib/sidekiq/api.rb, line 396
def created_at
  Time.at(self['created_at'] || self['enqueued_at'] || 0).utc
end
delete() click to toggle source

Remove this job from the queue.

# File lib/sidekiq/api.rb, line 411
def delete
  count = Sidekiq.redis do |conn|
    conn.lrem("queue:#{@queue}", 1, @value)
  end
  count != 0
end
display_args() click to toggle source
# File lib/sidekiq/api.rb, line 360
def display_args
  # Unwrap known wrappers so they show up in a human-friendly manner in the Web UI
  @display_args ||= case klass
            when /\ASidekiq::Extensions::Delayed/
              safe_load(args[0], args) do |_, _, arg|
                arg
              end
            when "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
              job_args = self['wrapped'] ? args[0]["arguments"] : []
              if 'ActionMailer::DeliveryJob' == (self['wrapped'] || args[0])
                # remove MailerClass, mailer_method and 'deliver_now'
                job_args.drop(3)
              else
                job_args
              end
            else
              if self['encrypt']
                # no point in showing 150+ bytes of random garbage
                args[-1] = '[encrypted data]'
              end
              args
            end
end
display_class() click to toggle source
# File lib/sidekiq/api.rb, line 340
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_at() click to toggle source
# File lib/sidekiq/api.rb, line 392
def enqueued_at
  self['enqueued_at'] ? Time.at(self['enqueued_at']).utc : nil
end
jid() click to toggle source
# File lib/sidekiq/api.rb, line 388
def jid
  self['jid']
end
klass() click to toggle source
# File lib/sidekiq/api.rb, line 336
def klass
  self['class']
end
latency() click to toggle source
# File lib/sidekiq/api.rb, line 404
def latency
  now = Time.now.to_f
  now - (@item['enqueued_at'] || @item['created_at'] || now)
end
parse(item) click to toggle source
# File lib/sidekiq/api.rb, line 325
def parse(item)
  Sidekiq.load_json(item)
rescue JSON::ParserError
  # If the job payload in Redis is invalid JSON, we'll load
  # the item as an empty hash and store the invalid JSON as
  # the job 'args' for display in the Web UI.
  @invalid = true
  @args = [item]
  {}
end
queue() click to toggle source
# File lib/sidekiq/api.rb, line 400
def queue
  @queue
end

Private Instance Methods

safe_load(content, default) { |*load| ... } click to toggle source
# File lib/sidekiq/api.rb, line 427
def safe_load(content, default)
  begin
    yield(*YAML.load(content))
  rescue => ex
    # #1761 in dev mode, it's possible to have jobs enqueued which haven't been loaded into
    # memory yet so the YAML can't be loaded.
    Sidekiq.logger.warn "Unable to load YAML: #{ex.message}" unless Sidekiq.options[:environment] == 'development'
    default
  end
end