Class: Roundhouse::Job

Inherits:
Object
  • Object
show all
Defined in:
lib/roundhouse/api.rb

Overview

Encapsulates a pending job within a Roundhouse queue or sorted set.

The job should be considered immutable but may be removed from the queue via Job#delete.

Direct Known Subclasses

SortedEntry

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(item, queue_name = nil) ⇒ Job

Returns a new instance of Job.



320
321
322
323
324
# File 'lib/roundhouse/api.rb', line 320

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

Instance Attribute Details

#itemObject (readonly)

Returns the value of attribute item.



318
319
320
# File 'lib/roundhouse/api.rb', line 318

def item
  @item
end

Instance Method Details

#[](name) ⇒ Object



391
392
393
# File 'lib/roundhouse/api.rb', line 391

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

#argsObject



358
359
360
# File 'lib/roundhouse/api.rb', line 358

def args
  @item['args']
end

#created_atObject



370
371
372
# File 'lib/roundhouse/api.rb', line 370

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

#deleteObject

Remove this job from the queue.



384
385
386
387
388
389
# File 'lib/roundhouse/api.rb', line 384

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

#display_argsObject



344
345
346
347
348
349
350
351
352
353
354
355
356
# File 'lib/roundhouse/api.rb', line 344

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

#display_classObject



330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/roundhouse/api.rb', line 330

def display_class
  # Unwrap known wrappers so they show up in a human-friendly manner in the Web UI
  @klass ||= case klass
             when /\ARoundhouse::Extensions::Delayed/
               safe_load(args[0], klass) do |target, method, _|
                 "#{target}.#{method}"
               end
             when "ActiveJob::QueueAdapters::RoundhouseAdapter::JobWrapper"
               @item['wrapped'] || args[0]
             else
               klass
             end
end

#enqueued_atObject



366
367
368
# File 'lib/roundhouse/api.rb', line 366

def enqueued_at
  @item['enqueued_at'] ? Time.at(@item['enqueued_at']).utc : nil
end

#jidObject



362
363
364
# File 'lib/roundhouse/api.rb', line 362

def jid
  @item['jid']
end

#klassObject



326
327
328
# File 'lib/roundhouse/api.rb', line 326

def klass
  @item['class']
end

#latencyObject



378
379
380
# File 'lib/roundhouse/api.rb', line 378

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

#queue_idObject



374
375
376
# File 'lib/roundhouse/api.rb', line 374

def queue_id
  @queue_id
end