Class: MockBeaneater::Tube

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pool, name) ⇒ Tube

Returns a new instance of Tube.



5
6
7
8
9
10
11
# File 'lib/mock_beaneater/tube/record.rb', line 5

def initialize(pool, name)
  @name = name.to_s
  @pool = pool
  @delayed = Containers::PriorityQueue.new
  @ready = Containers::PriorityQueue.new
  @reserved = []
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#poolObject (readonly)

Returns the value of attribute pool.



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

def pool
  @pool
end

Instance Method Details

#clearObject



42
43
44
45
# File 'lib/mock_beaneater/tube/record.rb', line 42

def clear
  @delayed.clear
  @ready.clear
end

#delete(job) ⇒ Object



47
48
49
# File 'lib/mock_beaneater/tube/record.rb', line 47

def delete(job)
  @reserved.delete(job)
end

#peek(state) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/mock_beaneater/tube/record.rb', line 26

def peek(state)
  refresh_tube
  if state == 'ready'
    @ready.next
  elsif state == 'delayed'
    @delayed.next
  else
    nil
  end
end

#put(body, options = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/mock_beaneater/tube/record.rb', line 13

def put(body, options={})
  visible_at = (options[:delay] || 0) + Time.now.to_i
  job = Job.new(body, self, visible_at, options[:pri] || 65536)
  if options[:delay]
    @delayed.push job, -1*visible_at
  else
    @ready.push job, job.pri
  end
  {:status => "INSERTED",
   :body   => nil,
   :id     => job.id}
end

#release(job) ⇒ Object



51
52
53
54
# File 'lib/mock_beaneater/tube/record.rb', line 51

def release(job)
  @reserved.delete(job)
  @ready.push(job, job.pri)
end

#reserve(timeout = nil) ⇒ Object



37
38
39
40
# File 'lib/mock_beaneater/tube/record.rb', line 37

def reserve(timeout=nil)
  refresh_tube
  @ready.pop.tap { |j| @reserved << j if j }
end