Class: Aaron::Scheduler

Inherits:
Object
  • Object
show all
Defined in:
lib/aaron/scheduler.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScheduler

Returns a new instance of Scheduler.



7
8
9
# File 'lib/aaron/scheduler.rb', line 7

def initialize
  @tasks = []
end

Instance Attribute Details

#tasksObject (readonly)

Returns the value of attribute tasks.



5
6
7
# File 'lib/aaron/scheduler.rb', line 5

def tasks
  @tasks
end

Instance Method Details

#<<(task) ⇒ Object



28
29
30
# File 'lib/aaron/scheduler.rb', line 28

def << task
  @tasks << task
end

#add_task(task) ⇒ Object



24
25
26
# File 'lib/aaron/scheduler.rb', line 24

def add_task task
  self << task
end

#next_sleepObject



20
21
22
# File 'lib/aaron/scheduler.rb', line 20

def next_sleep
  tasks.map { |task| task.next_sleep }.sort.first
end

#runObject



11
12
13
14
15
16
17
18
# File 'lib/aaron/scheduler.rb', line 11

def run
  while true do
    n = next_sleep
    t = tasks.each { |task| task.apply_time!(n) }.select &:ready_to_trigger?
    sleep(n)
    t.each &:trigger
  end
end