Class: Redwood::IdleManager

Inherits:
Object show all
Includes:
Singleton
Defined in:
lib/sup/idle.rb

Constant Summary collapse

IDLE_THRESHOLD =
60

Instance Method Summary collapse

Methods included from Singleton

included

Constructor Details

#initializeIdleManager

Returns a new instance of IdleManager.



10
11
12
13
14
# File 'lib/sup/idle.rb', line 10

def initialize
  @no_activity_since = Time.now
  @idle = false
  @thread = nil
end

Instance Method Details

#pingObject



16
17
18
19
20
21
22
# File 'lib/sup/idle.rb', line 16

def ping
  if @idle
    UpdateManager.relay self, :unidle, Time.at(@no_activity_since)
    @idle = false
  end
  @no_activity_since = Time.now
end

#startObject



24
25
26
27
28
29
30
31
32
33
34
# File 'lib/sup/idle.rb', line 24

def start
  @thread = Redwood::reporting_thread("checking for idleness") do
    while true
      sleep 1
      if !@idle and Time.now.to_i - @no_activity_since.to_i >= IDLE_THRESHOLD
        UpdateManager.relay self, :idle, Time.at(@no_activity_since)
        @idle = true
      end
    end
  end
end

#stopObject



36
37
38
39
# File 'lib/sup/idle.rb', line 36

def stop
  @thread.kill if @thread
  @thread = nil
end