Class: Sidekiq::JobRecord

Inherits:
Object
  • Object
show all
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

Instance Attribute Details

#itemObject (readonly)

the parsed Hash of job data



357
358
359
# File 'lib/sidekiq/api.rb', line 357

def item
  @item
end

#ItemObject (readonly)

the parsed Hash of job data



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

attr_reader :item

#queueObject (readonly)

the queue associated with this job



363
364
365
# File 'lib/sidekiq/api.rb', line 363

def queue
  @queue
end

#QueueObject (readonly)

the queue associated with this job



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

attr_reader :queue

#ValueObject (readonly)

the underlying String in Redis



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

attr_reader :value

#valueObject (readonly)

the underlying String in Redis



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

def value
  @value
end

Instance Method Details

#[](name) ⇒ Object

Access arbitrary attributes within the job hash



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

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



432
433
434
# File 'lib/sidekiq/api.rb', line 432

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

#bidObject



440
441
442
# File 'lib/sidekiq/api.rb', line 440

def bid
  self["bid"]
end

#created_atObject



462
463
464
# File 'lib/sidekiq/api.rb', line 462

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

#deleteObject

Remove this job from the queue



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

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

#display_argsObject



410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
# File 'lib/sidekiq/api.rb', line 410

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



393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# File 'lib/sidekiq/api.rb', line 393

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



456
457
458
459
460
# File 'lib/sidekiq/api.rb', line 456

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

#error_backtraceObject



470
471
472
473
474
475
476
477
478
# File 'lib/sidekiq/api.rb', line 470

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



444
445
446
447
448
# File 'lib/sidekiq/api.rb', line 444

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

#jidObject



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

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.



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

def klass
  self["class"]
end

#latencyObject



480
481
482
483
484
485
486
487
488
489
490
491
492
# File 'lib/sidekiq/api.rb', line 480

def latency
  timestamp = @item["enqueued_at"] || @item["created_at"]
  if timestamp
    if timestamp.is_a?(Float)
      # old format
      Time.now.to_f - timestamp
    else
      (::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond) - timestamp) / 1000.0
    end
  else
    0.0
  end
end

#retried_atObject



450
451
452
453
454
# File 'lib/sidekiq/api.rb', line 450

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

#tagsObject



466
467
468
# File 'lib/sidekiq/api.rb', line 466

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