Created
June 2, 2010 22:36
-
-
Save playerconnect/423118 to your computer and use it in GitHub Desktop.
Resolve thin backend server port from Rails controller
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # request.port is unfortunately useless from within the app, as it | |
| # does not reflect the backend bind port, but the frontend proxy. | |
| # So we have to reverse lookup the PID to figure out our port. | |
| def backend_server_port | |
| Thread.current[:backend_server_port] ||= begin | |
| logger.debug "Attempting to reverse-lookup server port from PID file" | |
| port = nil | |
| Dir["#{RAILS_ROOT}/tmp/pids/thin.*.pid"].each do |f| | |
| pid = File.read(f).chomp.to_i | |
| if pid == $$ | |
| port = f.gsub(/.*\.([0-9]+)\.pid$/, '\1').to_i | |
| raise "Malformed PID file does not contain port: #{f}" if port == 0 | |
| break | |
| end | |
| end | |
| port || 3000 # guess 3000 for local dev | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment