class RackCheck::ServerManager

Attributes

context[R]
ru_file[R]

Public Class Methods

new(context, ru_file) click to toggle source
# File lib/rack_check/server_manager.rb, line 11
def initialize(context, ru_file)
  @ru_file = ru_file
  @stop_var = Concurrent::MVar.new
  @context = context
end

Public Instance Methods

errors() click to toggle source
# File lib/rack_check/server_manager.rb, line 46
def errors
  @err&.read.strip
end
output() click to toggle source
# File lib/rack_check/server_manager.rb, line 50
def output
  @out&.read.strip
end
run() click to toggle source
# File lib/rack_check/server_manager.rb, line 17
def run
  Thread.new do
    begin
      with_environment(ru_file) do |dir|
        Dir.chdir(dir) do
          @pid, @in, @out, @err = Open4.popen4 context.app_server_command
          @stop_var.take
          Process.kill 'TERM', @pid
          Process.wait @pid
          @pid = nil
        end
      end
    rescue => e
      $stderr.puts "App server thread exited: #{e.message}\n#{e.backtrace.join("\n")}"
    end
  end

  port = wait_for_port

  "http://localhost:#{port}"
end
stop() click to toggle source
# File lib/rack_check/server_manager.rb, line 39
def stop
  @stop_var.put true
  while check_port
    sleep 0.1
  end
end

Private Instance Methods

check_port() click to toggle source
# File lib/rack_check/server_manager.rb, line 70
def check_port
  TCPSocket.new('localhost', context.app_server_port).close
  true
rescue
  false
end
wait_for_port() click to toggle source
# File lib/rack_check/server_manager.rb, line 56
def wait_for_port
  time = Time.now
  while Time.now - time < context.startup_timeout && !check_port
    sleep 0.1
  end

  if !check_port
    Process.kill 'TERM', @pid if @pid
    raise "Application server did not start up in time. Errors:\n#{errors}"
  end

  context.app_server_port
end