Class: Microget::ServerRunner

Inherits:
Struct
  • Object
show all
Defined in:
lib/microget/server_runner.rb

Overview

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

Constant Summary collapse

SHOULD_CONNECT_WITHIN =
2

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandObject

Returns the value of attribute command

Returns:

  • (Object)

    the current value of command



4
5
6
# File 'lib/microget/server_runner.rb', line 4

def command
  @command
end

#nameObject

Returns the value of attribute name

Returns:

  • (Object)

    the current value of name



4
5
6
# File 'lib/microget/server_runner.rb', line 4

def name
  @name
end

#portObject

Returns the value of attribute port

Returns:

  • (Object)

    the current value of port



4
5
6
# File 'lib/microget/server_runner.rb', line 4

def port
  @port
end

#rackup_file_pathObject

Returns the value of attribute rackup_file_path

Returns:

  • (Object)

    the current value of rackup_file_path



4
5
6
# File 'lib/microget/server_runner.rb', line 4

def rackup_file_path
  @rackup_file_path
end

Instance Method Details

#running?TrueClass, FalseClass

Tells whether the server is currently running

Returns:

  • (TrueClass, FalseClass)


56
57
58
# File 'lib/microget/server_runner.rb', line 56

def running?
  !!@running
end

#start!(timeout: SHOULD_CONNECT_WITHIN) ⇒ TrueClass

Start the server as a subprocess and store its PID.

Parameters:

  • timeout (Fixnum) (defaults to: SHOULD_CONNECT_WITHIN)

    the number of seconds to wait for the server to boot up

Returns:

  • (TrueClass)

    true



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 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!TrueClass

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

Returns:

  • (TrueClass)


64
65
66
67
68
69
70
71
72
# 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