Sha256: e80abc88389e0a5e17f25b991ad288c27a7580931c05f4bb851d8fd323eccb29

Contents?: true

Size: 729 Bytes

Versions: 1

Compression:

Stored size: 729 Bytes

Contents

# frozen_string_literal: true

module Nonnative
  class Process
    def initialize(configuration)
      @configuration = configuration
    end

    def start
      @child_pid = spawn(configuration.process)
      return if port_open?

      stop
      raise Nonnative::Error, 'Could not start the process'
    end

    def stop
      ::Process.kill('SIGHUP', child_pid)
    end

    private

    attr_reader :configuration, :child_pid

    def port_open?
      Timeout.timeout(configuration.timeout) do
        TCPSocket.new('127.0.0.1', configuration.port).close
        true
      rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH
        sleep 0.01
        retry
      end
    rescue Timeout::Error
      false
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
nonnative-0.2.0 lib/nonnative/process.rb