Class: Jets::RackServer
- Inherits:
-
Object
- Object
- Jets::RackServer
- Defined in:
- lib/jets/rack_server.rb
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(options = {}) ⇒ RackServer
constructor
A new instance of RackServer.
- #rack_project ⇒ Object
-
#serve ⇒ Object
Runs in the child process.
- #start ⇒ Object
-
#wait_for_socket ⇒ Object
blocks until rack server is up.
Constructor Details
#initialize(options = {}) ⇒ RackServer
Returns a new instance of RackServer.
7 8 9 |
# File 'lib/jets/rack_server.rb', line 7 def initialize(={}) @options = end |
Class Method Details
.start(options = {}) ⇒ Object
3 4 5 |
# File 'lib/jets/rack_server.rb', line 3 def self.start(={}) new().start end |
Instance Method Details
#rack_project ⇒ Object
76 77 78 |
# File 'lib/jets/rack_server.rb', line 76 def rack_project "#{Jets.root}rack" end |
#serve ⇒ Object
Runs in the child process
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/jets/rack_server.rb', line 34 def serve # Note, looks like stopping jets server with Ctrl-C sends the TERM signal # down to the sub bin/rackup command cleans up the child process fine. Bundler.with_clean_env do args = '' args << " --host #{@options[:host]}" if @options[:host] # only forward the host option # port is always 9292 for simplicity command = "cd #{rack_project} && bin/rackup#{args}" # leads to the same wrapper rack scripts puts "=> #{command}".colorize(:green) system(command) end end |
#start ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/jets/rack_server.rb', line 11 def start return unless File.exist?("#{rack_project}/config.ru") puts "Starting additional rack server for the project under the rack subfolder..." if ENV['JETS_DEBUG'] if ENV['FOREGROUND'] serve return end # Reaching here means we'll run the server in the background. # Handle daemonzing ourselves because it keeps the stdout of the 2nd # rack server. The rackup --daemonize option ends up hiding the output. pid = Process.fork if pid.nil? # we're in the child process serve else # we're in the parent process Process.detach(pid) # dettached but still in the "foreground" since bin/rackup runs in the foreground end end |
#wait_for_socket ⇒ Object
blocks until rack server is up
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/jets/rack_server.rb', line 48 def wait_for_socket return unless Jets.rack? retries = 0 max_retries = 30 # 15 seconds at a delay of 0.5s delay = 0.5 if ENV['C9_USER'] # overrides for local testing max_retries = 3 delay = 3 end begin server = TCPSocket.new('localhost', 9292) server.close rescue Errno::ECONNREFUSED puts "Unable to connect to localhost:9292. Delay for #{delay} and will try to connect again." if ENV['JETS_DEBUG'] sleep(delay) retries += 1 if retries < max_retries retry else puts "Giving up on trying to connect to localhost:9292" return false end end puts "Connected to localhost:9292 successfully" true end |