Class: Qmore::Reservers::Strategies::Sources::Background

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/qmore/reservers/strategies/sources.rb

Overview

Background Queue source runs in a background thread to periodically update the queues available.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(delegate, delay) ⇒ Background

Returns a new instance of Background.

Parameters:

  • delegate (Enumerator)

    queue source to load the queues from.

  • delay (Integer)
    • how long between updates



25
26
27
28
# File 'lib/qmore/reservers/strategies/sources.rb', line 25

def initialize(delegate, delay)
  @delegate = delegate
  @delay = delay
end

Instance Attribute Details

#delayObject (readonly)

Returns the value of attribute delay.



22
23
24
# File 'lib/qmore/reservers/strategies/sources.rb', line 22

def delay
  @delay
end

#delegateObject (readonly)

Returns the value of attribute delegate.



22
23
24
# File 'lib/qmore/reservers/strategies/sources.rb', line 22

def delegate
  @delegate
end

Instance Method Details

#each(&block) ⇒ Object



53
54
55
# File 'lib/qmore/reservers/strategies/sources.rb', line 53

def each(&block)
  @queues.each { |q| block.call(q) }
end

#startThread

Spawns a thread to periodically update the queues.

Returns:

  • (Thread)

    returns the spawned thread.



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/qmore/reservers/strategies/sources.rb', line 33

def start
  @stop   = false
  @queues = delegate.to_a
  Thread.new do
    begin
      loop do
        sleep delay
        break if @stop
        @queues = delegate.to_a
      end
    rescue => e
      retry
    end
  end
end

#stopObject



49
50
51
# File 'lib/qmore/reservers/strategies/sources.rb', line 49

def stop
  @stop = true
end