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.



618
619
620
621
622
# File 'lib/mockserver/binary_launcher.rb', line 618

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



616
617
618
# File 'lib/mockserver/binary_launcher.rb', line 616

def launcher
  @launcher
end

#pidInteger (readonly)

Returns the process ID.

Returns:

  • (Integer)

    the process ID



610
611
612
# File 'lib/mockserver/binary_launcher.rb', line 610

def pid
  @pid
end

#portInteger (readonly)

Returns the server port.

Returns:

  • (Integer)

    the server port



613
614
615
# File 'lib/mockserver/binary_launcher.rb', line 613

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



650
651
652
653
654
655
656
657
658
659
660
661
# File 'lib/mockserver/binary_launcher.rb', line 650

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)



628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# File 'lib/mockserver/binary_launcher.rb', line 628

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