Skip to content

Instantly share code, notes, and snippets.

@playerconnect
Created June 2, 2010 22:36
Show Gist options
  • Select an option

  • Save playerconnect/423118 to your computer and use it in GitHub Desktop.

Select an option

Save playerconnect/423118 to your computer and use it in GitHub Desktop.
Resolve thin backend server port from Rails controller
# 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