Class: Mimic::Server
Instance Method Summary collapse
-
#listening?(host, port) ⇒ Boolean
courtesy of is.gd/eoYho.
- #logger ⇒ Object
- #serve(app, options) ⇒ Object
- #shutdown ⇒ Object
- #start_service(app, options) ⇒ Object
- #wait_for_service(host, port, timeout = 5) ⇒ Object
Instance Method Details
#listening?(host, port) ⇒ Boolean
courtesy of is.gd/eoYho
77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/mimic.rb', line 77 def listening?(host, port) begin socket = TCPSocket.new(host, port) socket.close unless socket.nil? true rescue Errno::ECONNREFUSED, SocketError, Errno::EBADF, # Windows Errno::EADDRNOTAVAIL # Windows false end end |
#logger ⇒ Object
46 47 48 |
# File 'lib/mimic.rb', line 46 def logger @logger ||= Logger.new(StringIO.new) end |
#serve(app, options) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/mimic.rb', line 50 def serve(app, ) if [:fork] @thread = Thread.fork do start_service(app, ) end wait_for_service(app.hostname, [:port], [:wait_timeout]) else start_service(app, ) end end |
#shutdown ⇒ Object
71 72 73 |
# File 'lib/mimic.rb', line 71 def shutdown Thread.kill(@thread) if @thread end |
#start_service(app, options) ⇒ Object
63 64 65 66 67 68 69 |
# File 'lib/mimic.rb', line 63 def start_service(app, ) Rack::Handler::Thin.run(app.url_map, { :Port => [:port], :Logger => logger, :AccessLog => logger, }) end |
#wait_for_service(host, port, timeout = 5) ⇒ Object
89 90 91 92 93 94 95 96 97 |
# File 'lib/mimic.rb', line 89 def wait_for_service(host, port, timeout = 5) start_time = Time.now until listening?(host, port) if timeout && (Time.now > (start_time + timeout)) raise SocketError.new("Socket did not open within #{timeout} seconds") end end end |