class Knjappserver::Threadding_timeout

Attributes

timeout[R]

Public Class Methods

new(args) click to toggle source
# File lib/include/class_knjappserver_threadding_timeout.rb, line 4
def initialize(args)
  @args = args
  @kas = @args[:kas]
  raise "No time given." if !@args.key?(:time)
  @mutex = Mutex.new
  @running = false
end

Public Instance Methods

[](key) click to toggle source

Returns various data.

# File lib/include/class_knjappserver_threadding_timeout.rb, line 86
def [](key)
  return @timeout if key == :knjappserver_timeout
  raise "No such key: '#{key}'."
end
alive?() click to toggle source

Returns true if the thread is alive or not.

# File lib/include/class_knjappserver_threadding_timeout.rb, line 92
def alive?
  return @thread.alive? if @thread
  return false
end
running?() click to toggle source

Returns true if the timeout is running or not.

# File lib/include/class_knjappserver_threadding_timeout.rb, line 98
def running?
  return @running
end
start() click to toggle source

Starts the timeout.

# File lib/include/class_knjappserver_threadding_timeout.rb, line 21
def start
  @run = true
  @thread = Thread.new do
    loop do
      begin
        if @args[:counting]
          @timeout = @args[:time]
          
          while @timeout > 0
            @timeout += -1
            break if @kas.should_restart or !@run
            sleep 1
          end
        else
          sleep @args[:time]
        end
        
        break if @kas.should_restart or !@run
        
        @mutex.synchronize do
          @kas.threadpool.run do
            @kas.ob.db.get_and_register_thread if @kas.ob.db.opts[:threadsafe]
            @kas.db_handler.get_and_register_thread if @kas.db_handler.opts[:threadsafe]
            
            Thread.current[:knjappserver] = {
              :kas => @kas,
              :db => @kas.db_handler
            }
            
            begin
              @running = true
              
              if @args.key?(:timeout)
                Timeout.timeout(@args[:timeout]) do
                  @args[:block].call(*@args[:args])
                end
              else
                @args[:block].call(*@args[:args])
              end
            ensure
              @running = false
              @kas.ob.db.free_thread if @kas.ob.db.opts[:threadsafe]
              @kas.db_handler.free_thread if @kas.db_handler.opts[:threadsafe]
            end
          end
        end
      rescue => e
        @kas.handle_error(e)
      end
    end
  end
  
  return self
end
stop() click to toggle source

Stops the timeout.

# File lib/include/class_knjappserver_threadding_timeout.rb, line 77
def stop
  @run = false
  @mutex.synchronize do
    @thread.kill if @thread.alive?
    @thread = nil
  end
end
time() click to toggle source
# File lib/include/class_knjappserver_threadding_timeout.rb, line 16
def time
  return @args[:time]
end
time=(newtime) click to toggle source
# File lib/include/class_knjappserver_threadding_timeout.rb, line 12
def time=(newtime)
  @args[:time] = newtime.to_s.to_i
end