Class: Hi::Server
- Inherits:
-
Object
- Object
- Hi::Server
- 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
-
#app ⇒ Object
readonly
Returns the value of attribute app.
Instance Method Summary collapse
-
#initialize(app) ⇒ Server
constructor
A new instance of Server.
- #start(port = app.port, attempts = 1) ⇒ Object
- #start!(port) ⇒ Object
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
#app ⇒ Object (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 |