2013-01-25 43 views
1

我有一个unicorn.rb文件,我想根据环境变量设置worker_process。我试过没有成功以下方法:根据环境变化Unicorn 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/[email protected]/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/[email protected]/gems/unicorn-4.3.1/lib/unicorn/configurator.rb:74:in `reload' 
21:07:49 web.1 |  from /home/hg/.rvm/gems/[email protected]/gems/unicorn-4.3.1/lib/unicorn/configurator.rb:67:in `initialize' 
21:07:49 web.1 |  from /home/hg/.rvm/gems/[email protected]/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:104:in `new' 
21:07:49 web.1 |  from /home/hg/.rvm/gems/[email protected]/gems/unicorn-4.3.1/lib/unicorn/http_server.rb:104:in `initialize' 
21:07:49 web.1 |  from /home/hg/.rvm/gems/[email protected]/gems/unicorn-4.3.1/bin/unicorn_rails:209:in `new' 
21:07:49 web.1 |  from /home/hg/.rvm/gems/[email protected]/gems/unicorn-4.3.1/bin/unicorn_rails:209:in `<top (required)>' 
21:07:49 web.1 |  from /home/hg/.rvm/gems/[email protected]/bin/unicorn_rails:19:in `load' 
21:07:49 web.1 |  from /home/hg/.rvm/gems/[email protected]/bin/unicorn_rails:19:in `<main>' 
21:07:49 web.1 |  from /home/hg/.rvm/gems/[email protected]/bin/ruby_noexec_wrapper:14:in `eval' 
21:07:49 web.1 |  from /home/hg/.rvm/gems/[email protected]/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

这是整个unicorn.rb文件吗?你可以将它完全粘贴,还是要求它或确认它确实满了?我看到它在抱怨:在我无法辨认的第4行。第一个建议是避免将环境作为一个变量,因为它可能在独角兽内部使用? –

+0

这是我完整的[gist文件](https://gist.github.com/4633113)。奇怪的是,现在我再试一次,它正在工作。 – Hengjie

+0

我怀疑你可能遗漏了一个:在那里是错误的。 :)使用vi/vim? –

回答

1

我相信这是我的错误,我怀疑删除:为我解决了它。然而,为寻找调整独角兽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

0

对于那些有兴趣在这样做,我使用更主机具体的方法允许具有硬编码默认值的机器特定的独角兽进程。

ENV['UNICORN_PROCESSES'] ||= '4' 
worker_processes ENV['UNICORN_PROCESSES'].to_i