unicorn.rb 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. require "yaml"
  2. # Sample verbose configuration file for Unicorn (not Rack)
  3. #
  4. # This configuration file documents many features of Unicorn
  5. # that may not be needed for some applications. See
  6. # http://unicorn.bogomips.org/examples/unicorn.conf.minimal.rb
  7. # for a much simpler configuration file.
  8. #
  9. # See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
  10. # documentation.
  11. # Use at least one worker per core if you're on a dedicated server,
  12. # more will usually help for _short_ waits on databases/caches.
  13. rails_env = ENV['RAILS_ENV'] || 'production'
  14. CONFIG_FILE = YAML.load_file("#{File.dirname(__FILE__)}/config.yml")[rails_env]
  15. worker_processes (rails_env == 'production' ? 5 : 1)
  16. root = CONFIG_FILE['root_path']
  17. # Since Unicorn is never exposed to outside clients, it does not need to
  18. # run on the standard HTTP port (80), there is no reason to start Unicorn
  19. # as root unless it's from system init scripts.
  20. # If running the master process as root and the workers as an unprivileged
  21. # user, do this to switch euid/egid in the workers (also chowns logs):
  22. # user "unprivileged_user", "unprivileged_group"
  23. # Help ensure your application will always spawn in the symlinked
  24. # "current" directory that Capistrano sets up.
  25. working_directory root # available in 0.94.0+
  26. # listen on both a Unix domain socket and a TCP port,
  27. # we use a shorter backlog for quicker failover when busy
  28. listen "#{root}/tmp/sockets/unicorn.sock", :backlog => 64
  29. # nuke workers after 30 seconds instead of 60 seconds (the default)
  30. timeout 30
  31. # feel free to point this anywhere accessible on the filesystem
  32. pid "#{root}/tmp/pids/unicorn.pid"
  33. # By default, the Unicorn logger will write to stderr.
  34. # Additionally, ome applications/frameworks log to stderr or stdout,
  35. # so prevent them from going to /dev/null when daemonized here:
  36. stderr_path "#{root}/log/unicorn.stderr.log"
  37. stdout_path "#{root}/log/unicorn.stdout.log"
  38. # combine REE with "preload_app true" for memory savings
  39. # http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
  40. preload_app true
  41. GC.respond_to?(:copy_on_write_friendly=) and
  42. GC.copy_on_write_friendly = true
  43. before_fork do |server, worker|
  44. # the following is highly recomended for Rails + "preload_app true"
  45. # as there's no need for the master process to hold a connection
  46. defined?(ActiveRecord::Base) and
  47. ActiveRecord::Base.connection.disconnect!
  48. # The following is only recommended for memory/DB-constrained
  49. # installations. It is not needed if your system can house
  50. # twice as many worker_processes as you have configured.
  51. #
  52. # # This allows a new master process to incrementally
  53. # # phase out the old master process with SIGTTOU to avoid a
  54. # # thundering herd (especially in the "preload_app false" case)
  55. # # when doing a transparent upgrade. The last worker spawned
  56. # # will then kill off the old master process with a SIGQUIT.
  57. old_pid = "#{server.config[:pid]}.oldbin"
  58. if old_pid != server.pid
  59. begin
  60. sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
  61. Process.kill(sig, File.read(old_pid).to_i)
  62. rescue Errno::ENOENT, Errno::ESRCH
  63. end
  64. end
  65. #
  66. # Throttle the master from forking too quickly by sleeping. Due
  67. # to the implementation of standard Unix signal handlers, this
  68. # helps (but does not completely) prevent identical, repeated signals
  69. # from being lost when the receiving process is busy.
  70. # sleep 1
  71. end
  72. after_fork do |server, worker|
  73. # per-process listener ports for debugging/admin/migrations
  74. # addr = "127.0.0.1:#{9293 + worker.nr}"
  75. # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
  76. # the following is *required* for Rails + "preload_app true",
  77. defined?(ActiveRecord::Base) and
  78. ActiveRecord::Base.establish_connection
  79. # if preload_app is true, then you may also want to check and
  80. # restart any other shared sockets/descriptors such as Memcached,
  81. # and Redis. TokyoCabinet file handles are safe to reuse
  82. # between any number of forked children (assuming your kernel
  83. # correctly implements pread()/pwrite() system calls)
  84. end