Class: Mimic::Server
Instance Method Summary collapse
-
#listening?(host, port) ⇒ Boolean
courtesy of is.gd/eoYho.
- #logger ⇒ Object
- #serve(host_app, port, should_fork) ⇒ Object
- #shutdown ⇒ Object
- #start_service(host_app, port) ⇒ Object
- #wait_for_service(host, port, timeout = 5) ⇒ Object
Instance Method Details
#listening?(host, port) ⇒ Boolean
courtesy of is.gd/eoYho
81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/mimic.rb', line 81 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
43 44 45 |
# File 'lib/mimic.rb', line 43 def logger @logger ||= Logger.new(StringIO.new) end |
#serve(host_app, port, should_fork) ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/mimic.rb', line 47 def serve(host_app, port, should_fork) if should_fork @thread = Thread.fork do start_service(host_app, port) end wait_for_service(host_app.hostname, port) else start_service(host_app, port) end end |
#shutdown ⇒ Object
74 75 76 77 |
# File 'lib/mimic.rb', line 74 def shutdown Thread.kill(@thread) if @thread @server.shutdown if @server end |
#start_service(host_app, port) ⇒ Object
60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/mimic.rb', line 60 def start_service(host_app, port) Rack::Handler::WEBrick.run(host_app.url_map, { :Port => port, :Logger => logger, :AccessLog => logger, }) do |server| @server = server trap("TERM") { @server.shutdown } trap("INT") { @server.shutdown } end end |
#wait_for_service(host, port, timeout = 5) ⇒ Object
93 94 95 96 97 98 99 100 101 |
# File 'lib/mimic.rb', line 93 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 |