unicorn.rb 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. #
  2. # Author:: Noah Kantrowitz <noah@opscode.com>
  3. # Cookbook Name:: application
  4. # Provider:: unicorn
  5. #
  6. # Copyright:: 2011, Opscode, Inc <legal@opscode.com>
  7. #
  8. # Licensed under the Apache License, Version 2.0 (the "License");
  9. # you may not use this file except in compliance with the License.
  10. # You may obtain a copy of the License at
  11. #
  12. # http://www.apache.org/licenses/LICENSE-2.0
  13. #
  14. # Unless required by applicable law or agreed to in writing, software
  15. # distributed under the License is distributed on an "AS IS" BASIS,
  16. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. # See the License for the specific language governing permissions and
  18. # limitations under the License.
  19. #
  20. action :before_compile do
  21. end
  22. action :before_deploy do
  23. end
  24. action :before_migrate do
  25. end
  26. action :before_symlink do
  27. end
  28. action :before_restart do
  29. node.default[:unicorn][:worker_timeout] = 60
  30. node.default[:unicorn][:preload_app] = false
  31. node.default[:unicorn][:worker_processes] = [node[:cpu][:total].to_i * 4, 8].min
  32. node.default[:unicorn][:preload_app] = false
  33. node.default[:unicorn][:before_fork] = 'sleep 1'
  34. node.default[:unicorn][:port] = '8080'
  35. node.set[:unicorn][:options] = { :tcp_nodelay => true, :backlog => 100 }
  36. unicorn_config "/etc/unicorn/#{app['id']}.rb" do
  37. listen({ node[:unicorn][:port] => node[:unicorn][:options] })
  38. working_directory ::File.join(app['deploy_to'], 'current')
  39. worker_timeout node[:unicorn][:worker_timeout]
  40. preload_app node[:unicorn][:preload_app]
  41. worker_processes node[:unicorn][:worker_processes]
  42. before_fork node[:unicorn][:before_fork]
  43. end
  44. runit_service app['id'] do
  45. template_name 'unicorn'
  46. cookbook 'application'
  47. options(
  48. :app => app,
  49. :rails_env => node.run_state[:rails_env] || node.chef_environment,
  50. :smells_like_rack => ::File.exists?(::File.join(app['deploy_to'], "current", "config.ru"))
  51. )
  52. run_restart false
  53. end
  54. if ::File.exists?(::File.join(app['deploy_to'], "current"))
  55. d = resources(:deploy_revision => app['id'])
  56. d.restart_command do
  57. execute "/etc/init.d/#{app['id']} hup"
  58. end
  59. end
  60. end
  61. action :after_restart do
  62. end