1

我有一个 unicorn.rb 文件,我想根据环境变量设置 worker_process。我尝试了以下方法但没有成功:

environment = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'production'
# Save on RAM while in development
if environment == 'development'
  worker_processes 1
else
  worker_processes 4
end

当我使用时,foreman start我收到以下错误:

21:07:49 web.1    | /home/hg/.rvm/gems/ruby-1.9.3-p194@px/gems/unicorn-4.3.1/lib/unicorn/configurator.rb:74:in `instance_eval': ./unicorn.rb:4: syntax error, unexpected ':', expecting keyword_then or ';' or '\n' (SyntaxError)
21:07:49 web.1    | ./unicorn.rb:6: syntax error, unexpected keyword_else, expecting $end
21:07:49 web.1    |     from /home/hg/.rvm/gems/ruby-1.9.3-p194@px/gems/unicorn-4.3.1/lib/unicorn/configurator.rb:74:in `reload'
21:07:49 web.1    |     from /home/hg/.rvm/gems/ruby-1.9.3-p194@px/gems/unicorn-4.3.1/lib/unicorn/configurator.rb:67:in `initialize'
21:07:49 web.1    |     from /home/hg/.rvm/gems/ruby-1.9.3-p194@px/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:104:in `new'
21:07:49 web.1    |     from /home/hg/.rvm/gems/ruby-1.9.3-p194@px/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:104:in `initialize'
21:07:49 web.1    |     from /home/hg/.rvm/gems/ruby-1.9.3-p194@px/gems/unicorn-4.3.1/bin/unicorn_rails:209:in `new'
21:07:49 web.1    |     from /home/hg/.rvm/gems/ruby-1.9.3-p194@px/gems/unicorn-4.3.1/bin/unicorn_rails:209:in `<top (required)>'
21:07:49 web.1    |     from /home/hg/.rvm/gems/ruby-1.9.3-p194@px/bin/unicorn_rails:19:in `load'
21:07:49 web.1    |     from /home/hg/.rvm/gems/ruby-1.9.3-p194@px/bin/unicorn_rails:19:in `<main>'
21:07:49 web.1    |     from /home/hg/.rvm/gems/ruby-1.9.3-p194@px/bin/ruby_noexec_wrapper:14:in `eval'
21:07:49 web.1    |     from /home/hg/.rvm/gems/ruby-1.9.3-p194@px/bin/ruby_noexec_wrapper:14:in `<main>'
21:07:49 web.1    | exited with code 1
21:07:49 system   | sending SIGTERM to all processes
SIGTERM received

我能否请教一下如何解决这个问题?谢谢。

4

2 回答 2

1

我相信这是我的错误,我怀疑删除:解决了我的问题。但是,对于任何寻求调整 unicorn 的 worker_processes 的人来说,因为他们的开发环境受到 RAM 限制,这里是我的unicorn.rb文件:

environment = ENV['RACK_ENV'] || ENV['RAILS_ENV'] || 'production'

# Save on RAM while in development
if environment == 'development'
  worker_processes 1
else
  worker_processes 4
end

timeout 30
preload_app true

before_fork do |server, worker|
  # Close all open connections
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.connection.disconnect!
  end

  @resque_pid ||= spawn("bundle exec rake resque:work QUEUES=fast")
end

after_fork do |server, worker|
  # Reopen all connections
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
  end
end

https://gist.github.com/4633113

于 2013-01-26T04:18:52.803 回答
1

对于那些对此感兴趣的人,我使用了一种更特定于主机的方法,该方法允许具有硬编码默认值的特定于机器的独角兽进程。

ENV['UNICORN_PROCESSES'] ||= '4'
worker_processes ENV['UNICORN_PROCESSES'].to_i
于 2013-05-11T23:21:19.807 回答