Class: Solanum::Schedule

Inherits:
Object
  • Object
show all
Defined in:
lib/solanum/schedule.rb

Instance Method Summary collapse

Constructor Details

#initializeSchedule

Returns a new instance of Schedule.



5
6
7
8
# File 'lib/solanum/schedule.rb', line 5

def initialize
  @lock = Mutex.new
  @timetable = []
end

Instance Method Details

#insert!(time, id) ⇒ Object

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



45
46
47
48
49
50
51
52
# 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_waitObject

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



21
22
23
24
25
26
27
28
29
# 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_nextObject

Peek at the next scheduled entry.



12
13
14
15
16
# File 'lib/solanum/schedule.rb', line 12

def peek_next
  @lock.synchronize do
    @timetable.first
  end
end

#pop_ready!Object

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.



34
35
36
37
38
39
40
41
# 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