Class: Pact::MockService::Server::Spawn

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/mock_service/server/spawn.rb

Defined Under Namespace

Classes: PortUnavailableError

Class Method Summary collapse

Class Method Details

.call(pidfile, host, port, ssl = false) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/pact/mock_service/server/spawn.rb', line 10

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

.port_available?(host, port) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
29
30
31
32
33
# File 'lib/pact/mock_service/server/spawn.rb', line 26

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