Sha256: 91fed2f023d3fc1d165fde4216f601d9bbbe4515efecd652b8766a9f9b07d1bc

Contents?: true

Size: 1.32 KB

Versions: 6

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

require "socket"
require "uri"

module CypressRails
  class Server
    extend Forwardable

    attr_reader :host, :port, :command, :healthcheck_url, :log_path

    def initialize(host, command, healthcheck_url, log_path)
      @host = host
      @port = find_free_port
      @command = command
      @healthcheck_url = URI(healthcheck_url || "http://#{host}").tap { |uri| uri.port = port }
      @log_path = log_path
    end

    def start
      spawn_server
      until server_responsive?
        puts "Pinging #{healthcheck_url.to_s}..."
        sleep 1
      end
      yield(host, port) if block_given?
    ensure
      stop_server
    end

    private

    attr_reader :pid

    def spawn_server
      @pid = Process.spawn(
        build_command,
        out: [
          log_path,
          File::WRONLY | File::CREAT | File::TRUNC,
          0o600
        ],
        err: [:child, :out]
      )
      Process.detach(pid)
    end

    def server_responsive?
      system("curl #{healthcheck_url}", [:out, :err] => "/dev/null")
    end

    def stop_server
      Process.kill("SIGINT", pid) if pid
    end

    def build_command
      "#{command} --port #{port}"
    end

    def find_free_port
      server = ::TCPServer.new(host, 0)
      server.addr[1]
    ensure
      server.close if server
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cypress_rails-0.9.2 lib/cypress_rails/server.rb
cypress_rails-0.9.1 lib/cypress_rails/server.rb
cypress_rails-0.9.0 lib/cypress_rails/server.rb
cypress_rails-0.8.0 lib/cypress_rails/server.rb
cypress_rails-0.7.0 lib/cypress_rails/server.rb
cypress_rails-0.6.0 lib/cypress_rails/server.rb