unicorn.rb 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. require_relative 'auto_tune'
  2. # FWBM only...
  3. num_workers, = auto_tune
  4. worker_processes num_workers
  5. listen "/tmp/.sock", :backlog => 512
  6. preload_app true
  7. GC.respond_to?(:copy_on_write_friendly=) and
  8. GC.copy_on_write_friendly = true
  9. before_fork do |server, worker|
  10. # the following is highly recomended for Rails + "preload_app true"
  11. # as there's no need for the master process to hold a connection
  12. defined?(ActiveRecord::Base) and
  13. ActiveRecord::Base.connection.disconnect!
  14. # The following is only recommended for memory/DB-constrained
  15. # installations. It is not needed if your system can house
  16. # twice as many worker_processes as you have configured.
  17. #
  18. # # This allows a new master process to incrementally
  19. # # phase out the old master process with SIGTTOU to avoid a
  20. # # thundering herd (especially in the "preload_app false" case)
  21. # # when doing a transparent upgrade. The last worker spawned
  22. # # will then kill off the old master process with a SIGQUIT.
  23. # old_pid = "#{server.config[:pid]}.oldbin"
  24. # if old_pid != server.pid
  25. # begin
  26. # sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
  27. # Process.kill(sig, File.read(old_pid).to_i)
  28. # rescue Errno::ENOENT, Errno::ESRCH
  29. # end
  30. # end
  31. #
  32. # Throttle the master from forking too quickly by sleeping. Due
  33. # to the implementation of standard Unix signal handlers, this
  34. # helps (but does not completely) prevent identical, repeated signals
  35. # from being lost when the receiving process is busy.
  36. # sleep 1
  37. end
  38. after_fork do |server, worker|
  39. # per-process listener ports for debugging/admin/migrations
  40. # addr = "127.0.0.1:#{9293 + worker.nr}"
  41. # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
  42. # the following is *required* for Rails + "preload_app true",
  43. defined?(ActiveRecord::Base) and
  44. ActiveRecord::Base.establish_connection
  45. # if preload_app is true, then you may also want to check and
  46. # restart any other shared sockets/descriptors such as Memcached,
  47. # and Redis. TokyoCabinet file handles are safe to reuse
  48. # between any number of forked children (assuming your kernel
  49. # correctly implements pread()/pwrite() system calls)
  50. end