Foreman/Puma isn't using the specified port in dev env

怎甘沉沦 提交于 2019-12-21 07:18:07

问题


I set the port as 3000 in my application.yml (figaro managing env variables) rails s uses port 3000 but when I run foreman start (as recommended by Heroku) I get the following output

14:53:23 web.1  | started with pid 24425
14:53:23 web.1  | [24425] Puma starting in cluster mode...
14:53:23 web.1  | [24425] * Version 2.11.1 (ruby 2.2.0-p0), codename: Intrepid Squirrel
14:53:23 web.1  | [24425] * Min threads: 5, max threads: 5
14:53:23 web.1  | [24425] * Environment: development
14:53:23 web.1  | [24425] * Process workers: 2
14:53:23 web.1  | [24425] * Preloading application
14:53:24 web.1  | WARNING: Skipping key "PORT". Already set in ENV.
14:53:25 web.1  | [24425] * Listening on tcp://0.0.0.0:5000
14:53:25 web.1  | [24425] Use Ctrl-C to stop
14:53:25 web.1  | [24425] - Worker 0 (pid: 24426) booted, phase: 0
14:53:25 web.1  | [24425] - Worker 1 (pid: 24427) booted, phase: 0

Procfile

web: bundle exec puma -C config/puma.rb

config/puma.rb

workers Integer(ENV['WEB_CONCURRENCY'] || 2)
threads_count = Integer(ENV['MAX_THREADS'] || 5)
threads threads_count, threads_count

preload_app!

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

on_worker_boot do
  ActiveRecord::Base.establish_connection
end

config/application.yml

PORT: "3000"
  • Rails 4.2.0
  • Foreman 0.78.0
  • Ruby 2.2.0p0
  • Puma 2.11.1

回答1:


Mystery on the puma port solved.

Put this print in your config/puma.rb

Then you'll see that somehow the port is mysteriously set to 5000 even though it is not in your ENV.

Fix at bottom.

puma_port = ENV['PORT'] || 3000
puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ"
puts "puma_port is #{puma_port}"
puts "ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ”

That prints out

16:49:28 web.1  | ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ
16:49:28 web.1  | puma_port is 5000
ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ

Then place this line in your Procfile (not the one you use for Heroku). I've got one called Procfile.dev

web: PORT=3000 bundle exec puma -C config/puma.rb

And I run it with this command:

foreman start -f Procfile.dev



回答2:


Use a .foreman file with:

port: 3000

or a .env file with:

PORT=3000

Either one should work, then you can just use foreman start




回答3:


port ENV['PORT'] || 3000

This line says that you will attempt to use ENV['PORT'] first and if that doesn't exist, you will fall back to using 3000 as the port number. So are you defining PORT environment variable anywhere prior to foreman starting? config/application.yml environment variables only will be loaded after the rails server starts, so it's no use defining port number here.

You can try adding

PORT=3000 in .env file in the root of the project directory.

Source: https://devcenter.heroku.com/articles/getting-started-with-rails5#procfile



来源:https://stackoverflow.com/questions/29595129/foreman-puma-isnt-using-the-specified-port-in-dev-env

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!