Sha256: e6011fc82d034deda92e8728082b050f560643b9e8614f1c01ea763555d925c8

Contents?: true

Size: 1.2 KB

Versions: 1

Compression:

Stored size: 1.2 KB

Contents

module RackCheck

class ServerManager
  include RackCheck::Helpers

  attr_reader :context, :ru_file

  def initialize(context, ru_file)
    @ru_file = ru_file
    @stop_var = Concurrent::MVar.new
    @context = context
  end

  def run
    Thread.new do
      begin
        with_environment(ru_file) do |dir|
          @pid = Process.spawn context.app_server_command,
            in: '/dev/null',
            out: '/dev/null',
            err: '/dev/null',
            chdir: dir
          @stop_var.take
          Process.kill 'TERM', @pid
          Process.wait @pid
          @pid = nil
        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

  def stop
    @stop_var.put true
  end

  private

  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"
    end

    context.app_server_port
  end

  def check_port
    `lsof -n -i :#{context.app_server_port}`.lines.count > 0
  end
end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rack_check-0.1.0 lib/rack_check/server_manager.rb