What configuration for Rails 3.2.22.2 + Puma + Heroku?

喜夏-厌秋 提交于 2019-12-06 07:25:47

问题


I've read Richard Schneeman's article, and a bunch of other ones. ;-) I'm still struggling with this.

Here's few gems I've added in my Gemfile to benchmark my app:

gem 'airbrake'
gem 'newrelic_rpm'
gem 'stackprof'
gem 'derailed', group: :development
gem 'rack-mini-profiler'
gem 'flamegraph'
gem 'memory_profiler'
gem "skylight"

After a lots of benchmarks in development and in staging env, I know where my app is not fast enough but there's not memory leak (some small mem bloats sometimes maybe).

newapp-staging app is the new version (aka: new frontend, upgraded gems, optimized queries, ...) of oldapp-production app. Please have a look at the screenshots (oldapp-production use webrick, newapp-staging use puma)

So here comes 2 simple questions:

question #1

newapp-staging app is using ruby '2.2.0' & rails '3.2.22.2' and I can't ensure that it is threadsafe because of my code and the associated gems, so... I must use 1 thread at a time. Is puma an advantage here? Metrics are telling me not. OR... my configuration is not good. (missing preload_app! maybe, or other things?) Here's my Procfile:

web: bundle exec puma -t 1:1 -p ${PORT:-3000} -e ${RACK_ENV:-development}
worker: bundle exec rake jobs:work

question #2

Unicorn could be used as a replacement?

Thank you for your time and your advices.

Cheers


回答1:


Using unicorn is the best move here. Here's my configuration if that could help anyone.

Gemfile:

gem 'unicorn'
gem 'unicorn-rails'
group :production, :staging do
  gem 'unicorn-worker-killer'
end

Procfile:

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
worker: bundle exec rake jobs:work

config/unicorn.rb

worker_processes Integer(ENV["WEB_CONCURRENCY"] || 2)
timeout 15
preload_app true
before_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
    Process.kill 'QUIT', Process.pid
  end
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
  Signal.trap 'TERM' do
    puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  end
  defined?(ActiveRecord::Base) and
    ActiveRecord::Base.establish_connection
end

config.ru

if ENV['RAILS_ENV'] == 'production' || ENV['RAILS_ENV'] == 'staging'
  require 'unicorn/worker_killer'
  use Unicorn::WorkerKiller::MaxRequests, 768, 1024, true
  use Unicorn::WorkerKiller::Oom, (450*(1024**2)), (490*(1024**2)), 16, true
end
require ::File.expand_path('../config/environment',  __FILE__)
use Rack::Deflater
run MyApp::Application

On Heroku:

2 x `Standard 2X dynos` for web
1 x `Standard 1X dyno` for worker

Heroku config vars:

SENSIBLE_DEFAULTS: enabled (just in case) & WEB_CONCURRENCY: 2

Cheers



来源:https://stackoverflow.com/questions/36954206/what-configuration-for-rails-3-2-22-2-puma-heroku

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