Class: Sidekiq::JobRecord

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 JobRecord#delete.

Direct Known Subclasses

SortedEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, queue_name = nil) ⇒ JobRecord

Returns a new instance of JobRecord.



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

def initialize(item, queue_name = nil)
  @args = nil
  @value = item
  @item = item.is_a?(Hash) ? item : parse(item)
  @queue = queue_name || @item["queue"]
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



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

def item
  @item
end

#queueObject (readonly)

Returns the value of attribute queue.



419
420
421
# File 'lib/sidekiq/api.rb', line 419

def queue
  @queue
end

#valueObject (readonly)

Returns the value of attribute value.



312
313
314
# File 'lib/sidekiq/api.rb', line 312

def value
  @value
end

Instance Method Details

#[](name) ⇒ Object



435
436
437
438
439
440
# File 'lib/sidekiq/api.rb', line 435

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

#argsObject



389
390
391
# File 'lib/sidekiq/api.rb', line 389

def args
  @args || @item["args"]
end

#created_atObject



401
402
403
# File 'lib/sidekiq/api.rb', line 401

def created_at
  Time.at(self["created_at"] || self["enqueued_at"] || 0).utc
end

#deleteObject

Remove this job from the queue.



428
429
430
431
432
433
# File 'lib/sidekiq/api.rb', line 428

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

#display_argsObject



358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
# File 'lib/sidekiq/api.rb', line 358

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, kwarg|
      if !kwarg || kwarg.empty?
        arg
      else
        [arg, kwarg]
      end
    end
  when "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
    job_args = self["wrapped"] ? args[0]["arguments"] : []
    if (self["wrapped"] || args[0]) == "ActionMailer::DeliveryJob"
      # remove MailerClass, mailer_method and 'deliver_now'
      job_args.drop(3)
    elsif (self["wrapped"] || args[0]) == "ActionMailer::MailDeliveryJob"
      # remove MailerClass, mailer_method and 'deliver_now'
      job_args.drop(3).first["args"]
    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_classObject



336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/sidekiq/api.rb', line 336

def display_class
  # Unwrap known wrappers so they show up in a human-friendly manner in the Web UI
  @klass ||= self["display_class"] || begin
    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 job_class == "ActionMailer::DeliveryJob" || job_class == "ActionMailer::MailDeliveryJob"
        # MailerClass#mailer_method
        args[0]["arguments"][0..1].join("#")
      else
        job_class
      end
    else
      klass
    end
  end
end

#enqueued_atObject



397
398
399
# File 'lib/sidekiq/api.rb', line 397

def enqueued_at
  self["enqueued_at"] ? Time.at(self["enqueued_at"]).utc : nil
end

#error_backtraceObject



409
410
411
412
413
414
415
416
417
# File 'lib/sidekiq/api.rb', line 409

def error_backtrace
  # Cache nil values
  if defined?(@error_backtrace)
    @error_backtrace
  else
    value = self["error_backtrace"]
    @error_backtrace = value && uncompress_backtrace(value)
  end
end

#jidObject



393
394
395
# File 'lib/sidekiq/api.rb', line 393

def jid
  self["jid"]
end

#klassObject



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

def klass
  self["class"]
end

#latencyObject



421
422
423
424
# File 'lib/sidekiq/api.rb', line 421

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

#parse(item) ⇒ Object



321
322
323
324
325
326
327
328
329
330
# File 'lib/sidekiq/api.rb', line 321

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

#tagsObject



405
406
407
# File 'lib/sidekiq/api.rb', line 405

def tags
  self["tags"] || []
end