Class: Sidekiq::Job

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

Direct Known Subclasses

SortedEntry

Constant Summary collapse

KNOWN_WRAPPERS =
[/\ASidekiq::Extensions::Delayed/, "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, queue_name = nil) ⇒ Job

Returns a new instance of Job.



195
196
197
198
199
# File 'lib/sidekiq/api.rb', line 195

def initialize(item, queue_name=nil)
  @value = item
  @item = item.is_a?(Hash) ? item : Sidekiq.load_json(item)
  @queue = queue_name || @item['queue']
end

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



193
194
195
# File 'lib/sidekiq/api.rb', line 193

def item
  @item
end

Instance Method Details

#[](name) ⇒ Object



262
263
264
# File 'lib/sidekiq/api.rb', line 262

def [](name)
  @item.__send__(:[], name)
end

#argsObject



233
234
235
# File 'lib/sidekiq/api.rb', line 233

def args
  @item['args']
end

#deleteObject

Remove this job from the queue.



255
256
257
258
259
260
# File 'lib/sidekiq/api.rb', line 255

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

#display_argsObject



219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/sidekiq/api.rb', line 219

def display_args
  # Unwrap known wrappers so they show up in a human-friendly manner in the Web UI
  @args ||= case klass
            when /\ASidekiq::Extensions::Delayed/
              safe_load(args[0], args) do |_, _, arg|
                arg
              end
            when "ActiveJob::QueueAdapters::SidekiqAdapter::JobWrapper"
              args[1..-1]
            else
              args
            end
end

#display_classObject



205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/sidekiq/api.rb', line 205

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"
               args[0]
             else
               klass
             end
end

#enqueued_atObject



241
242
243
# File 'lib/sidekiq/api.rb', line 241

def enqueued_at
  Time.at(@item['enqueued_at'] || 0).utc
end

#jidObject



237
238
239
# File 'lib/sidekiq/api.rb', line 237

def jid
  @item['jid']
end

#klassObject



201
202
203
# File 'lib/sidekiq/api.rb', line 201

def klass
  @item['class']
end

#latencyObject



249
250
251
# File 'lib/sidekiq/api.rb', line 249

def latency
  Time.now.to_f - @item['enqueued_at']
end

#queueObject



245
246
247
# File 'lib/sidekiq/api.rb', line 245

def queue
  @queue
end