Sha256: 09f277d262e3ab17faa73941adfb0dd35dc39148e74573d66e2593b899613978

Contents?: true

Size: 1.06 KB

Versions: 1

Compression:

Stored size: 1.06 KB

Contents

require 'pact/mock_service/server/wait_for_server_up'
require 'pact/mock_service/os'

module Pact
  module MockService
    module Server
      class WindowsSafeSpawn

        class PortUnavailableError < StandardError; end

        def self.call command, pidfile, port, ssl = false
          if pidfile.can_start?
            if port_available? port
              pid = spawn(command, spawn_options)
              pidfile.pid = pid
              Process.detach(pid)
              Server::WaitForServerUp.(port, {ssl: ssl})
              pidfile.write
            else
              raise PortUnavailableError.new("ERROR: Port #{port} already in use.")
            end
          end
        end

        def self.spawn_options
          if Pact::MockService::OS.windows?
            {new_pgroup: true}
          else
            {}
          end
        end

        def self.port_available? port
          server = TCPServer.new('127.0.0.1', port)
          true
        rescue
          false
        ensure
          server.close if server
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pact-mock_service-2.1.1.pre.alpha.2 lib/pact/mock_service/server/windows_safe_spawn.rb