Unicorn Worker Killer并没有减少内存使用量

时间:2014-12-08 22:42:14

标签: ruby unicorn sidekiq

过去几天,我一直在使用Rails应用程序解决内存问题。

我使用sidekiq gem设置了一个Unicorn服务器来运行后台作业。这些工作一直在生产中失败,所以我安装了NewRelic以查看内存使用情况,结果显示Sidekiq工作人员超出了Heroku的内存限制。

我现在正试图使用​​unicorn-worker-killer gem来减少工作人员的记忆,但我没有取得任何成功。日志显示每次加载页面时都会发生此消息,但它不会减少内存负载。

[2014-12-08T13:52:34.346557 #88418]  WARN -- : #<Unicorn::HttpServer:0x00000101025ee8>: worker    (pid: 88418) exceeds memory limit (123482112 bytes > 105673916 bytes)
W, [2014-12-08T13:52:34.346793 #88418]  WARN -- : Unicorn::WorkerKiller send SIGQUIT (pid: 88418) alive: 4 sec (trial 1)
I, [2014-12-08T13:52:34.492893 #88410]  INFO -- : reaped #<Process::Status: pid 88418 exit 0> worker=0
I, [2014-12-08T13:52:34.501168 #88441]  INFO -- : worker=0 ready

对我而言看起来是正确的,但工人占用的记忆力根本不会下降。 NewRelic仍然显示内存消耗从100MB到超过1GB并保持高水平,直到我重新启动服务器才下降。

非常感谢任何建议!我真的需要我的工作人员在内存限制内运行。

这是我的config.ru文件:

require 'unicorn/oob_gc'
use Unicorn::OobGC, 10 # Only GC once every GC_FREQUENCY requests

# --- Start of unicorn worker killer code ---

require 'unicorn/worker_killer'

max_request_min =  500
max_request_max =  600

# Max requests per worker
use Unicorn::WorkerKiller::MaxRequests, max_request_min, max_request_max

oom_min = (100) * (1024**2)
oom_max = (150) * (1024**2)

# Max memory size (RSS) per worker
use Unicorn::WorkerKiller::Oom, oom_min, oom_max, 1, true

# --- End of unicorn worker killer code ---



# This file is used by Rack-based servers to start the application.

require ::File.expand_path('../config/environment',  __FILE__)
run Rails.application

这是Unicorn.rb文件:

worker_processes Integer(1)
timeout 30
preload_app true
stderr_path "log/unicorn.stderr.log"
stdout_path "log/unicorn.stdout.log"

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

  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
  end

  old_pid = "#{server.config[:pid]}.oldbin"
  if old_pid != server.pid
    begin
      sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
      Process.kill(sig, File.read(old_pid).to_i)
    rescue Errno::ENOENT, Errno::ESRCH
    end
  end
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

  if defined?(ActiveRecord::Base)
    config = ActiveRecord::Base.configurations[Rails.env] ||
            Rails.application.config.database_configuration[Rails.env]
    config['reaping_frequency'] = 10 #seconds
    config['pool']            =   ENV['DB_POOL']
    ActiveRecord::Base.establish_connection(config)
  end
end

1 个答案:

答案 0 :(得分:0)

Unicorn与Sidekiq无关,不会影响其内存使用量。 Unicorn处理Web请求,Sidekiq处理后台作业。它们是两个不同的过程。