Sha256: 5c1b522837b42e659b6705bf2155cf204795d2a394262fa1e08f75101924b5b9

Contents?: true

Size: 1.13 KB

Versions: 1

Compression:

Stored size: 1.13 KB

Contents

require "singleton"

module Safeconsole
  class SessionWatcher
    include Singleton
    include Messages

    @initialized_at = Time.now
    @last_command_at = Time.now
    @total_commands = 0
    @transaction_commands = 0

    class << self
      attr_accessor :initialized_at, :last_command_at, :total_commands, :transaction_commands

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

      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

      def session_limit_reached?
        return false unless Safeconsole.config.session_timeout

        @initialized_at < Safeconsole.config.session_timeout.ago
      end

      def timeout_reached?
        return false unless Safeconsole.config.command_timeout

        last_command_at < Safeconsole.config.command_timeout.ago
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
safeconsole-1.0.0 lib/safeconsole/session_watcher.rb