class Solanum::Schedule

Public Class Methods

new() click to toggle source
# File lib/solanum/schedule.rb, line 5
def initialize
  @lock = Mutex.new
  @timetable = []
end

Public Instance Methods

insert!(time, id) click to toggle source

Schedule the given id for later running. Returns the scheduled entry.

# File lib/solanum/schedule.rb, line 45
def insert!(time, id)
  entry = [time, id]
  @lock.synchronize do
    @timetable << entry
    @timetable.sort_by! {|e| e[0] }
  end
  entry
end
next_wait() click to toggle source

Time to spend waiting until the next scheduled entry. Returns a number of seconds, or nil if the schedule is empty.

# File lib/solanum/schedule.rb, line 21
def next_wait
  entry = peek_next
  if entry
    next_time, next_id = *entry
    duration = next_time - Time.now
    #puts "Next scheduled run for #{next_id} at #{next_time} in #{duration} seconds" # DEBUG
    duration
  end
end
peek_next() click to toggle source

Peek at the next scheduled entry.

# File lib/solanum/schedule.rb, line 12
def peek_next
  @lock.synchronize do
    @timetable.first
  end
end
pop_ready!() click to toggle source

Try to get the next ready entry. Returns the id if it is ready and removes it from the scheudle, otherwise nil if no entries are ready to run.

# File lib/solanum/schedule.rb, line 34
def pop_ready!
  @lock.synchronize do
    if @timetable.first && Time.now >= @timetable.first[0]
      entry = @timetable.shift
      entry[1]
    end
  end
end