2013-01-25 76 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