我有一个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
我可以指点一下如何解决这个问题吗?感谢。
答案 0 :(得分: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
答案 1 :(得分:1)
对于那些有兴趣这样做的人,我使用了更多主机特定的方法,允许机器特定的独角兽进程具有硬编码默认值。
ENV['UNICORN_PROCESSES'] ||= '4'
worker_processes ENV['UNICORN_PROCESSES'].to_i