Class: Hi::Server

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

Overview

Wrapper for Thin server with retries and a friendly error message

Constant Summary collapse

CantStartServerError =
Class.new(RuntimeError)
MAX_ATTEMPTS =
5

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Server

Returns a new instance of Server.



12
13
14
# File 'lib/hi/server.rb', line 12

def initialize(app)
  @app = app
end

Instance Attribute Details

#appObject (readonly)

Returns the value of attribute app.



6
7
8
# File 'lib/hi/server.rb', line 6

def app
  @app
end

Instance Method Details

#start(port = app.port, attempts = 1) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'lib/hi/server.rb', line 16

def start(port = app.port, attempts = 1)
  app.log "Starting server on port #{port}...\n\n"
  start! port
rescue RuntimeError => e
  if attempts < MAX_ATTEMPTS
    app.log "\nUnable to start server, trying random port instead."
    start random_port, attempts + 1
  else
    raise CantStartServerError.new(e)
  end
end

#start!(port) ⇒ Object



28
29
30
# File 'lib/hi/server.rb', line 28

def start!(port)
  Thin::Server.start '0.0.0.0', port, app
end