Class: Aaron::Scheduler
- Inherits:
-
Object
- Object
- Aaron::Scheduler
- Defined in:
- lib/aaron/scheduler.rb
Instance Attribute Summary collapse
-
#tasks ⇒ Object
readonly
Returns the value of attribute tasks.
Instance Method Summary collapse
- #<<(task) ⇒ Object
- #add_task(task) ⇒ Object
-
#initialize ⇒ Scheduler
constructor
A new instance of Scheduler.
- #next_sleep ⇒ Object
- #run ⇒ Object
Constructor Details
#initialize ⇒ Scheduler
Returns a new instance of Scheduler.
7 8 9 |
# File 'lib/aaron/scheduler.rb', line 7 def initialize @tasks = [] end |
Instance Attribute Details
#tasks ⇒ Object (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_sleep ⇒ Object
20 21 22 |
# File 'lib/aaron/scheduler.rb', line 20 def next_sleep tasks.map { |task| task.next_sleep }.sort.first end |
#run ⇒ Object
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 |