Class: Sidekiq::SortedEntry

Inherits:
JobRecord show all
Defined in:
lib/sidekiq/api.rb

Overview

Represents a job within a Redis sorted set where the score represents a timestamp associated with the job. This timestamp could be the scheduled time for it to run (e.g. scheduled set), or the expiration date after which the entry should be deleted (e.g. dead set).

Instance Attribute Summary collapse

Attributes inherited from JobRecord

#Item, #Queue, #Value, #item, #queue, #value

Instance Method Summary collapse

Methods inherited from JobRecord

#[], #args, #bid, #created_at, #display_args, #display_class, #enqueued_at, #error_backtrace, #failed_at, #jid, #klass, #latency, #parse, #retried_at, #tags

Methods included from ApiUtils

#calculate_latency

Constructor Details

#initialize(parent, score, item) ⇒ SortedEntry

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:



591
592
593
594
595
# File 'lib/sidekiq/api.rb', line 591

def initialize(parent, score, item)
  super(item)
  @score = score
  @parent = parent
end

Instance Attribute Details

#parentObject (readonly)

Returns the value of attribute parent.



587
588
589
# File 'lib/sidekiq/api.rb', line 587

def parent
  @parent
end

Instance Method Details

#add_to_queueObject

Enqueue this job from the scheduled or dead set so it will be executed at some point in the near future.



630
631
632
633
634
635
# File 'lib/sidekiq/api.rb', line 630

def add_to_queue
  remove_job do |message|
    msg = Sidekiq.load_json(message)
    Sidekiq::Client.push(msg)
  end
end

#atObject

The timestamp associated with this entry



606
607
608
# File 'lib/sidekiq/api.rb', line 606

def at
  Time.at(score).utc
end

#deleteObject

remove this entry from the sorted set



611
612
613
614
615
616
617
# File 'lib/sidekiq/api.rb', line 611

def delete
  if @value
    @parent.delete_by_value(@parent.name, @value)
  else
    @parent.delete_by_jid(@score, jid)
  end
end

#error?Boolean

Returns:

  • (Boolean)


654
655
656
# File 'lib/sidekiq/api.rb', line 654

def error?
  !!item["error_class"]
end

#idObject



601
602
603
# File 'lib/sidekiq/api.rb', line 601

def id
  "#{@score}|#{item["jid"]}"
end

#killObject

Move this job from its current set into the Dead set.



648
649
650
651
652
# File 'lib/sidekiq/api.rb', line 648

def kill
  remove_job do |message|
    DeadSet.new.kill(message)
  end
end

#reschedule(at) ⇒ Object

Change the scheduled time for this job.

Parameters:

  • at (Time)

    the new timestamp for this job



622
623
624
625
626
# File 'lib/sidekiq/api.rb', line 622

def reschedule(at)
  Sidekiq.redis do |conn|
    conn.zincrby(@parent.name, at.to_f - score, Sidekiq.dump_json(@item))
  end
end

#retryObject

enqueue this job from the retry set so it will be executed at some point in the near future.



639
640
641
642
643
644
645
# File 'lib/sidekiq/api.rb', line 639

def retry
  remove_job do |message|
    msg = Sidekiq.load_json(message)
    msg["retry_count"] -= 1 if msg["retry_count"]
    Sidekiq::Client.push(msg)
  end
end

#scoreObject



597
598
599
# File 'lib/sidekiq/api.rb', line 597

def score
  Float(@score)
end