Class: Safeconsole::SessionWatcher

Inherits:
Object
  • Object
show all
Includes:
Messages, Singleton
Defined in:
lib/safeconsole/session_watcher.rb

Class Attribute Summary collapse

Class Method Summary collapse

Methods included from Messages

app_name, commands, commit, done, included, invalid_query, method_missing, nevermind, refresh, respond_to_missing?, session_expired, session_start, session_stats, transaction_start, unsafe_env, welcome

Class Attribute Details

.initialized_atObject

Returns the value of attribute initialized_at.



14
15
16
# File 'lib/safeconsole/session_watcher.rb', line 14

def initialized_at
  @initialized_at
end

.last_command_atObject

Returns the value of attribute last_command_at.



14
15
16
# File 'lib/safeconsole/session_watcher.rb', line 14

def last_command_at
  @last_command_at
end

.total_commandsObject

Returns the value of attribute total_commands.



14
15
16
# File 'lib/safeconsole/session_watcher.rb', line 14

def total_commands
  @total_commands
end

.transaction_commandsObject

Returns the value of attribute transaction_commands.



14
15
16
# File 'lib/safeconsole/session_watcher.rb', line 14

def transaction_commands
  @transaction_commands
end

Class Method Details

.command_ranObject



16
17
18
19
20
# File 'lib/safeconsole/session_watcher.rb', line 16

def command_ran
  @total_commands += 1
  @transaction_commands += 1
  @last_command_at = Time.now
end

.session_limit_reached?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
# File 'lib/safeconsole/session_watcher.rb', line 36

def session_limit_reached?
  return false unless Safeconsole.config.session_timeout

  @initialized_at < Safeconsole.config.session_timeout.ago
end

.timeout_reached?Boolean

Returns:

  • (Boolean)


42
43
44
45
46
# File 'lib/safeconsole/session_watcher.rb', line 42

def timeout_reached?
  return false unless Safeconsole.config.command_timeout

  last_command_at < Safeconsole.config.command_timeout.ago
end

.watch_session!Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/safeconsole/session_watcher.rb', line 22

def watch_session!
  Thread.start do
    loop do
      if timeout_reached? || session_limit_reached?
        print_message(:session_expired)
        Console.__console_commit = false
        break binding.eval("exit")
      end

      sleep 2
    end
  end
end