Class: Steady::Scheduler

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeScheduler

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

#dataObject (readonly)

Returns the value of attribute data.



38
39
40
# File 'lib/steady.rb', line 38

def data
  @data
end

#tasksObject (readonly)

Returns the value of attribute tasks.



38
39
40
# File 'lib/steady.rb', line 38

def tasks
  @tasks
end

Instance Method Details

#drowsinessObject



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

#runObject



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

#scheduleObject



77
78
79
80
81
# File 'lib/steady.rb', line 77

def schedule
  Thread.new do 
    loop { sleep; run }
  end
end

#sleepObject



89
90
91
# File 'lib/steady.rb', line 89

def sleep      
  sleep(drowsiness)
end