class MultiDb::Scheduler

Attributes

items[R]

Public Class Methods

new(items, blacklist_timeout = 1.minute) click to toggle source
# 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

Public Instance Methods

blacklist!(item) click to toggle source
# File lib/multi_db/scheduler.rb, line 18
def blacklist!(item)
  @blacklist[@items.index(item)] = Time.now
end
current() click to toggle source
# File lib/multi_db/scheduler.rb, line 22
def current
  @items[current_index]
end
next() click to toggle source
# 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

Protected Instance Methods

next_index!() click to toggle source
# File lib/multi_db/scheduler.rb, line 36
def next_index!
  self.current_index = (current_index + 1) % @n
end