class Microget::ServerRunner

A simplistic runner for external web servers within a separate process.

Constants

SHOULD_CONNECT_WITHIN

Public Instance Methods

command() click to toggle source
Calls superclass method
# File lib/microget/server_runner.rb, line 7
def command
  super % [port, rackup_file_path]
end
running?() click to toggle source

Tells whether the server is currently running

@return [TrueClass, FalseClass]

# File lib/microget/server_runner.rb, line 56
def running?
  !!@running
end
start!(timeout: SHOULD_CONNECT_WITHIN) click to toggle source

Start the server as a subprocess and store its PID.

@param timeout the number of seconds to wait for the server to boot up @return [TrueClass] true

# File lib/microget/server_runner.rb, line 15
def start!(timeout: SHOULD_CONNECT_WITHIN)
  # Boot Puma in a forked process
  @pid = fork do
    $stderr.puts "Spinning up with #{command.inspect}"
    
    # Do not pollute the test suite output with the Puma logs,
    # save the stuff to logfiles instead
    $stdout.reopen(File.open('server_runner_%s_stdout.log' % name, 'a'))
    $stderr.reopen(File.open('server_runner_%s_stderr.log' % name, 'a'))
    
    # Since we have to do with timing tolerances, having the output drip in ASAP is useful
    $stdout.sync = true
    $stderr.sync = true
    exec(command)
  end
  
  Thread.abort_on_exception = true
  
  t = Time.now
  # Wait for Puma to be online, poll the alive URL until it stops responding
  loop do
    sleep 0.5
    begin
      alive_check_url = "http://0.0.0.0:%d/" % port
      response = Net::HTTP.get_response(URI(alive_check_url))
      @running = true
      break
    rescue Errno::ECONNREFUSED
      if (Time.now - t) > timeout # The server is still not on, bail out
        raise "Could not get the server started in 2 seconds, something might be misconfigured"
      end
    end
  end
  
  trap("TERM") { stop! }
  true
end
stop!() click to toggle source

Stops the server by issuing progressively harsher signals to it’s process (in the order of TERM, TERM, KILL).

@return [TrueClass]

# File lib/microget/server_runner.rb, line 64
def stop!
  return unless @pid
  
  # Tell the webserver to quit, twice (we do not care if there are running responses)
  %W( TERM TERM KILL ).each {|sig| Process.kill(sig, @pid); sleep 0.5 }
  @pid = nil
  @running = false
  true
end