Class: Postburner::Tube

Inherits:
Object
  • Object
show all
Defined in:
lib/postburner/tube.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(tube) ⇒ Tube

Returns a new instance of Tube.



3
4
5
# File 'lib/postburner/tube.rb', line 3

def initialize(tube)
  @tube = tube
end

Class Method Details

.allObject

Get all tubes as Postburner::Tube instances.



9
10
11
# File 'lib/postburner/tube.rb', line 9

def self.all
  Postburner.connection.tubes.all.map { |tube| self.new(tube) }
end

.peek_idsObject

Get all peeked ids across all known tubes.



15
16
17
# File 'lib/postburner/tube.rb', line 15

def self.peek_ids
  self.all.map(&:peek_ids).flatten.sort
end

Instance Method Details

#jobs(count = 20, limit: 1000, after: nil) ⇒ Object

Get paginated array of jobs.

Attempts to do this efficiently as possible, by peeking at known ids and exiting when count has been fulfilled.

Just pass the last known id to after for the next batch.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/postburner/tube.rb', line 33

def jobs(count=20, limit: 1000, after: nil)
  stats = @tube.stats
  jobs = Array.new

  min_known = (
    peek_ids.any? ? self.peek_ids.min : self.class.peek_ids.min
  ).to_i
  min = after ? after + 1 : min_known
  max = min + limit

  for i in min..max
    job = @tube.client.jobs.find(i)
    jobs << job if job && stats[:name] == job.stats[:tube]
    break if jobs.length >= count
  end

  return jobs
end

#peek_idsObject

Get all peeked ids.



21
22
23
24
# File 'lib/postburner/tube.rb', line 21

def peek_ids
  [ :buried, :ready, :delayed ].map { |type| @tube.peek(type) }.
    reject(&:nil?).map(&:id).map(&:to_i)
end