Class: Steady::Scheduler
- Inherits:
-
Object
- Object
- Steady::Scheduler
- Defined in:
- lib/steady.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#tasks ⇒ Object
readonly
Returns the value of attribute tasks.
Instance Method Summary collapse
- #drowsiness ⇒ Object
- #every(interval, &block) ⇒ Object
-
#initialize ⇒ Scheduler
constructor
A new instance of Scheduler.
- #run ⇒ Object
- #schedule ⇒ Object
- #sleep ⇒ Object
Constructor Details
#initialize ⇒ Scheduler
Returns a new instance of Scheduler.
40 41 42 43 |
# File 'lib/steady.rb', line 40 def initialize @tasks = [] @data = SyncronizedHash.new end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
38 39 40 |
# File 'lib/steady.rb', line 38 def data @data end |
#tasks ⇒ Object (readonly)
Returns the value of attribute tasks.
38 39 40 |
# File 'lib/steady.rb', line 38 def tasks @tasks end |
Instance Method Details
#drowsiness ⇒ Object
83 84 85 86 87 |
# File 'lib/steady.rb', line 83 def drowsiness drowsiness = (t = @tasks.first) ? t.next_run - Speedytime.current : 1 drowsiness = 1 if drowsiness < 1 drowsiness end |
#every(interval, &block) ⇒ Object
45 46 47 |
# File 'lib/steady.rb', line 45 def every(interval, &block) push Task.new(interval, &block) end |
#run ⇒ Object
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/steady.rb', line 49 def run changes = DirtyTrackingHash.new runs = false @tasks.each do |task| if task.needs_running? task.run(changes) runs = true else # tasks are sorted, if a tasks doesn't need running # then no task behind it will need running either at # the moment. break end end # if tasks ran we resort the tasks and apply # any changes to the data hash if runs @tasks.sort! @data.apply(changes) if changes.dirty? true else false end end |
#schedule ⇒ Object
77 78 79 80 81 |
# File 'lib/steady.rb', line 77 def schedule Thread.new do loop { sleep; run } end end |
#sleep ⇒ Object
89 90 91 |
# File 'lib/steady.rb', line 89 def sleep sleep(drowsiness) end |