Class: Sidekiq::JobRecord

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

Overview

Represents a pending job within a Sidekiq queue.

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

Methods included from ApiUtils

#calculate_latency

Constructor Details

#initialize(item, queue_name = nil) ⇒ JobRecord

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:



408
409
410
411
412
413
# File 'lib/sidekiq/api.rb', line 408

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)

the parsed Hash of job data



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

def item
  @item
end

#ItemObject (readonly)

the parsed Hash of job data



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

attr_reader :item

#queueObject (readonly)

the queue associated with this job



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

def queue
  @queue
end

#QueueObject (readonly)

the queue associated with this job



404
# File 'lib/sidekiq/api.rb', line 404

attr_reader :queue

#ValueObject (readonly)

the underlying String in Redis



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

attr_reader :value

#valueObject (readonly)

the underlying String in Redis



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

def value
  @value
end

Instance Method Details

#[](name) ⇒ Object

Access arbitrary attributes within the job hash



534
535
536
537
538
539
# File 'lib/sidekiq/api.rb', line 534

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



473
474
475
# File 'lib/sidekiq/api.rb', line 473

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

#bidObject



481
482
483
# File 'lib/sidekiq/api.rb', line 481

def bid
  self["bid"]
end

#created_atObject



503
504
505
# File 'lib/sidekiq/api.rb', line 503

def created_at
  time_from_timestamp(self["created_at"] || self["enqueued_at"] || 0)
end

#deleteObject

Remove this job from the queue



526
527
528
529
530
531
# File 'lib/sidekiq/api.rb', line 526

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

#display_argsObject



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'lib/sidekiq/api.rb', line 451

def display_args
  # Unwrap known wrappers so they show up in a human-friendly manner in the Web UI
  @display_args ||= if klass == "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper" || klass == "Sidekiq::ActiveJob::Wrapper"
    job_args = self["wrapped"] ? deserialize_argument(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.values_at("params", "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



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/sidekiq/api.rb', line 434

def display_class
  # Unwrap known wrappers so they show up in a human-friendly manner in the Web UI
  @klass ||= self["display_class"] || begin
    if klass == "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper" || klass == "Sidekiq::ActiveJob::Wrapper"
      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



497
498
499
500
501
# File 'lib/sidekiq/api.rb', line 497

def enqueued_at
  if self["enqueued_at"]
    time_from_timestamp(self["enqueued_at"])
  end
end

#error_backtraceObject



511
512
513
514
515
516
517
518
519
# File 'lib/sidekiq/api.rb', line 511

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

#failed_atObject



485
486
487
488
489
# File 'lib/sidekiq/api.rb', line 485

def failed_at
  if self["failed_at"]
    time_from_timestamp(self["failed_at"])
  end
end

#jidObject



477
478
479
# File 'lib/sidekiq/api.rb', line 477

def jid
  self["jid"]
end

#klassObject

This is the job class which Sidekiq will execute. If using ActiveJob, this class will be the ActiveJob adapter class rather than a specific job.



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

def klass
  self["class"]
end

#latencyObject



521
522
523
# File 'lib/sidekiq/api.rb', line 521

def latency
  calculate_latency(@item)
end

#parse(item) ⇒ 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:



417
418
419
420
421
422
423
424
425
426
# File 'lib/sidekiq/api.rb', line 417

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

#retried_atObject



491
492
493
494
495
# File 'lib/sidekiq/api.rb', line 491

def retried_at
  if self["retried_at"]
    time_from_timestamp(self["retried_at"])
  end
end

#tagsObject



507
508
509
# File 'lib/sidekiq/api.rb', line 507

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