Class: RealWeb::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/realweb/server.rb

Direct Known Subclasses

ForkingServer, ThreadServer

Constant Summary collapse

DEFAULT_PORT_RANGE =
8000..10000
DEFAULT_HOST =
'127.0.0.1'
DEFAULT_LOGGER =
Logger.new(StringIO.new)
DEFAULT_TIMEOUT =

seconds

2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config_ru, options = {}) {|_self| ... } ⇒ Server

Create a RealWeb::Server object, either a RealWeb::ForkingServer or a RealWeb::ThreadServer.

:port_range - Range specifying acceptable tcp ports to boot on. :logger - An instance of Logger. :host - Alternative host. Default is 127.0.0.1. :verbose - Print server logs and errors to stdout/err :timeout - Timout in seconds to wait for the server to boot. :ready - A Proc that returns true when the server is ready, or false

if it is not ready yet. This proc will be called every 100ms with the
server as an argument. If blank, an open-uri based check will be used.

:pre_spawn_callback - a lambda to be called with the server object before

spawning the server.

Any remaining options will be passed to Rack::Server on boot.

Yields:

  • (_self)

Yield Parameters:



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/realweb/server.rb', line 50

def initialize(config_ru, options = {})
  @config_ru  = config_ru
  @running    = false

  @port_range = options.delete(:port_range) || DEFAULT_PORT_RANGE
  @logger     = options.delete(:logger)     || DEFAULT_LOGGER
  @host       = options.delete(:host)       || DEFAULT_HOST
  @verbose    = options.delete(:verbose)    || false
  @timeout    = options.delete(:timeout)    || DEFAULT_TIMEOUT
  @ready      = options.delete(:ready)      || self.class.method(:server_ready?)

  @pre_spawn_callback = options.delete(:pre_spawn_callback)
  @rack_options = options

  yield self if block_given?
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



10
11
12
# File 'lib/realweb/server.rb', line 10

def host
  @host
end

Class Method Details

.server_ready?(server) ⇒ Boolean

return true if available, false if still waiting

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
# File 'lib/realweb/server.rb', line 18

def self.server_ready?(server)
  open(server.base_uri)
  true
rescue OpenURI::HTTPError
  true
rescue Errno::ECONNREFUSED => e
  false
end

.with_rackup(*args) ⇒ Object



27
28
29
30
31
32
33
# File 'lib/realweb/server.rb', line 27

def self.with_rackup(*args)
  new(*args) do |server|
    server.start
    yield server if block_given?
    server.stop
  end
end

Instance Method Details

#base_uriObject



87
88
89
# File 'lib/realweb/server.rb', line 87

def base_uri
  URI.parse("http://#{host}:#{port}/")
end

#portObject



67
68
69
# File 'lib/realweb/server.rb', line 67

def port
  @port ||= find_port
end

#running?Boolean

Returns:

  • (Boolean)


71
72
73
# File 'lib/realweb/server.rb', line 71

def running?
  @running
end

#startObject



75
76
77
78
79
80
81
# File 'lib/realweb/server.rb', line 75

def start
  return if running?
  run_pre_spawn
  spawn_server
  wait_for_server
  @running = true
end

#stopObject



83
84
85
# File 'lib/realweb/server.rb', line 83

def stop
  @running = false
end