ruby-on-rails – 如果空闲时Rails停止响应

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 如果空闲时Rails停止响应前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经遇到这个问题了一段时间,搜索了很多关于这一点,但似乎没有什么可以解决我的问题.

我有一个应用程序运行在Ruby on Rails与Nginx和UNICORN一切与Docker.

如果我运行应用程序(docker-compose up),网站完美运行了几分钟.如果网站保持空闲约5到10分钟,并发送另一个请求,rails应用程序停止响应,从Unicorn提供时间.

奇怪的是,我开发的每个应用程序都有同样的问题,而这些应用程序的唯一共同之处就是Gemfile.

到目前为止,我发现,当发送请求时,Nginx收到该请求,将其传递给Unicorn,Unicorn接收它并传递给没有回答的ruby …然后Unicorn给我502个不良请求.

我真的迷失在这一个.

这是我的Nginx配置文件

upstream applicationName {
    # Path to Puma SOCK file,as defined prevIoUsly
    server unix:/tmp/applicationName.sock fail_timeout=0;
}

server {
  listen 80;
  server_name dev.applicationName.com.br;

  location / {
    autoindex on;
    proxy_set_header X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-For $remote_addr;
    proxy_set_header Host $host;
    # time out settings
    proxy_connect_timeout 159s;
    proxy_send_timeout   600;
    proxy_read_timeout   600;
    proxy_buffer_size    64k;
    proxy_buffers     16 32k;
    proxy_busy_buffers_size 64k;
    proxy_temp_file_write_size 64k;
    proxy_pass_header Set-Cookie;
    proxy_redirect     off;
    proxy_hide_header  Vary;
    proxy_set_header   Accept-Encoding '';
    proxy_ignore_headers Cache-Control Expires;
    proxy_set_header   Referer $http_referer;
    proxy_set_header   Host   $host;
    proxy_set_header   Cookie $http_cookie;
    proxy_set_header   X-Real-IP  $remote_addr;
    proxy_set_header X-Forwarded-Host $host;
    proxy_set_header X-Forwarded-Server $host;
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_pass http://applicationName;
  }
}

这是我的独角兽配置

@dir = File.expand_path(File.dirname(__FILE__)) + "/.."

worker_processes 2
working_directory @dir

timeout 10

listen File.join('/tmp/applicationName.sock')

preload_app true# if ENV['RAILS_ENV'] != 'development'

GC.respond_to?(:copy_on_write_friendly=) and
  GC.copy_on_write_friendly = true

check_client_connection false

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

这是Gemfile(没有错)

source 'https://rubygems.org'
gem 'rails','4.2.4'
gem 'unicorn-rails','~> 2.2'
gem 'pg'
gem 'MysqL2','~> 0.3.18'
gem 'sass-rails','~> 5.0'
gem 'uglifier','>= 1.3.0'
gem 'coffee-rails','~> 4.1.0'
gem 'duktape'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder','~> 2.0'
gem 'bootstrap-sass'
gem 'devise'
gem 'simple_form'
gem 'minitest'
gem "paperclip","~> 4.3"
gem 'aws-sdk','< 2.0'
gem 'mail_form','~> 1.5.0.rc'
gem 'sendgrid-ruby'
gem 'zopim_rails'
gem 'Meta-tags'
gem 'ckeditor'
gem 'slick_rails'

group :development do
  gem 'better_errors'
  gem 'binding_of_caller',:platforms=>[:mri_20]
  gem 'quiet_assets'
  gem 'rails_layout'
  gem 'spring-commands-rspec'
  gem 'web-console','~> 2.0'
  gem 'spring'
end
group :production do
  gem 'therubyracer'
end
group :development,:test do
  gem 'factory_girl_rails'
  gem 'faker'
  gem 'pry-rails'
  gem 'pry-rescue'
  gem 'rspec-rails'
  gem 'rubocop'
  gem 'byebug'
end

group :test do
  gem 'capybara'
  gem 'database_cleaner'
  gem 'launchy'
  gem 'selenium-webdriver'
end

当服务器上的日志显示出来,当发生错误时:

Nginx_1 | 172.17.0.1 - - [05/Feb/2016:12:52:41 +0000] "GET /products HTTP/1.1" 502 574 "http://dev.nutrimais.com.br/" "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/48.0.2564.82 Safari/537.36"
web_1   | E,[2016-02-05T12:52:41.551728 #1] ERROR -- : reaped #<Process::Status: pid 9 SIGKILL (signal 9)> worker=0

而在开发日志中:

Started GET "/products" for 127.0.0.1 at 2016-02-05 12:52:18 +0000
Cannot render console from 127.0.0.1! Allowed networks: 127.0.0.1,::1,127.0.0.0/127.255.255.255

解决方法

我听说过独角兽这样的问题,如果一个网站没有访问(平均)30分钟,下一个查询将超时,并且所有打开的工作人员将超时.我不知道为什么会发生这种情况.
我希望下面的链接可以帮助你

http://bogomips.org/unicorn-public/CAKM1sPNRsES6H6ByK6bO9Djwa8WvYV6HJ-rEaHopRUYBVFfuhg@mail.gmail.com/t/

猜你在找的Ruby相关文章