Class: MockServer::BinaryLauncher::ServerHandle

Inherits:
Object
  • Object
show all
Defined in:
lib/mockserver/binary_launcher.rb

Overview

Handle to a running MockServer process.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid:, port:, launcher:) ⇒ ServerHandle

Returns a new instance of ServerHandle.



588
589
590
591
592
# File 'lib/mockserver/binary_launcher.rb', line 588

def initialize(pid:, port:, launcher:)
  @pid = pid
  @port = port
  @launcher = launcher
end

Instance Attribute Details

#launcherString (readonly)

Returns path to the launcher executable.

Returns:

  • (String)

    path to the launcher executable



586
587
588
# File 'lib/mockserver/binary_launcher.rb', line 586

def launcher
  @launcher
end

#pidInteger (readonly)

Returns the process ID.

Returns:

  • (Integer)

    the process ID



580
581
582
# File 'lib/mockserver/binary_launcher.rb', line 580

def pid
  @pid
end

#portInteger (readonly)

Returns the server port.

Returns:

  • (Integer)

    the server port



583
584
585
# File 'lib/mockserver/binary_launcher.rb', line 583

def port
  @port
end

Instance Method Details

#running?Boolean

Returns true if the process is still running.

Returns:

  • (Boolean)

    true if the process is still running



620
621
622
623
624
625
626
627
628
629
630
631
# File 'lib/mockserver/binary_launcher.rb', line 620

def running?
  return false unless @pid

  Process.kill(0, @pid)
  true
rescue Errno::ESRCH
  # Process does not exist
  false
rescue Errno::EPERM
  # Process exists but we lack permission to signal it — it IS running
  true
end

#stop(timeout: 10) ⇒ void

This method returns an undefined value.

Stop the server by terminating the process.

Parameters:

  • timeout (Numeric) (defaults to: 10)

    seconds to wait before SIGKILL (default 10)



598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
# File 'lib/mockserver/binary_launcher.rb', line 598

def stop(timeout: 10)
  return unless @pid

  begin
    Process.kill('TERM', @pid)
    deadline = Time.now + timeout
    loop do
      Process.waitpid(@pid, Process::WNOHANG) && break
      if Time.now > deadline
        Process.kill('KILL', @pid)
        Process.waitpid(@pid)
        break
      end
      sleep 0.1
    end
  rescue Errno::ESRCH, Errno::ECHILD
    # Process already gone
  end
  @pid = nil
end