Class: MultiDb::Scheduler

Inherits:
Object
  • Object
show all
Extended by:
ThreadLocalAccessors
Defined in:
lib/multi_db/scheduler.rb

Defined Under Namespace

Classes: NoMoreItems

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(items, blacklist_timeout = 1.minute) ⇒ Scheduler

Returns a new instance of Scheduler.



10
11
12
13
14
15
16
# File 'lib/multi_db/scheduler.rb', line 10

def initialize(items, blacklist_timeout = 1.minute)
  @n = items.length
  @items     = items
  @blacklist = Array.new(@n, Time.at(0))
  @blacklist_timeout = blacklist_timeout
  self.current_index = rand(@n)
end

Instance Attribute Details

#itemsObject (readonly)

Returns the value of attribute items.



6
7
8
# File 'lib/multi_db/scheduler.rb', line 6

def items
  @items
end

Instance Method Details

#blacklist!(item) ⇒ Object



18
19
20
# File 'lib/multi_db/scheduler.rb', line 18

def blacklist!(item)
  @blacklist[@items.index(item)] = Time.now
end

#currentObject



22
23
24
# File 'lib/multi_db/scheduler.rb', line 22

def current
  @items[current_index]
end

#nextObject



26
27
28
29
30
31
32
# File 'lib/multi_db/scheduler.rb', line 26

def next
  previous = current_index
  until(@blacklist[next_index!] < Time.now - @blacklist_timeout) do
    raise NoMoreItems, 'All items are blacklisted' if current_index == previous
  end
  current
end