unicorn.rb 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. # Sample configuration file for Unicorn (not Rack)
  2. #
  3. # See http://unicorn.bogomips.org/Unicorn/Configurator.html for complete
  4. # documentation.
  5. SINATRA_ROOT = `pwd`.strip
  6. # Use at least one worker per core if you're on a dedicated server,
  7. # more will usually help for _short_ waits on databases/caches.
  8. worker_processes 3
  9. # Help ensure your application will always spawn in the symlinked
  10. # "current" directory that Capistrano sets up.
  11. working_directory SINATRA_ROOT # available in 0.94.0+
  12. # listen on both a Unix domain socket and a TCP port,
  13. # we use a shorter backlog for quicker failover when busy
  14. # listen "/tmp/.sock", :backlog => 64
  15. listen 18889, :tcp_nopush => true
  16. # nuke workers after 30 seconds instead of 60 seconds (the default)
  17. timeout 30
  18. # feel free to point this anywhere accessible on the filesystem
  19. pid "#{SINATRA_ROOT}/tmp/pids/unicorn.pid"
  20. # relative_path "/test_platform"
  21. # some applications/frameworks log to stderr or stdout, so prevent
  22. # them from going to /dev/null when daemonized here:
  23. stderr_path "#{SINATRA_ROOT}/log/unicorn.stderr.log"
  24. stdout_path "#{SINATRA_ROOT}/log/unicorn.stdout.log"
  25. # combine REE with "preload_app true" for memory savings
  26. # http://rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
  27. preload_app false
  28. GC.respond_to?(:copy_on_write_friendly=) and
  29. GC.copy_on_write_friendly = true
  30. before_fork do |server, worker|
  31. # the following is highly recomended for Rails + "preload_app true"
  32. # as there's no need for the master process to hold a connection
  33. # defined?(ActiveRecord::Base) and
  34. # ActiveRecord::Base.connection.disconnect!
  35. # The following is only recommended for memory/DB-constrained
  36. # installations. It is not needed if your system can house
  37. # twice as many worker_processes as you have configured.
  38. #
  39. # # This allows a new master process to incrementally
  40. # # phase out the old master process with SIGTTOU to avoid a
  41. # # thundering herd (especially in the "preload_app false" case)
  42. # # when doing a transparent upgrade. The last worker spawned
  43. # # will then kill off the old master process with a SIGQUIT.
  44. old_pid = "#{server.config[:pid]}.oldbin"
  45. puts 'pid:'
  46. puts '-------------------'
  47. puts server.pid
  48. puts old_pid
  49. puts '---------------------'
  50. if old_pid != server.pid
  51. begin
  52. sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
  53. Process.kill(sig, File.read(old_pid).to_i)
  54. rescue Errno::ENOENT, Errno::ESRCH
  55. end
  56. end
  57. #
  58. # # *optionally* throttle the master from forking too quickly by sleeping
  59. sleep 1
  60. end
  61. after_fork do |server, worker|
  62. # per-process listener ports for debugging/admin/migrations
  63. # addr = "127.0.0.1:#{9293 + worker.nr}"
  64. # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
  65. # the following is *required* for Rails + "preload_app true",
  66. # defined?(ActiveRecord::Base) and
  67. # ActiveRecord::Base.establish_connection
  68. # if preload_app is true, then you may also want to check and
  69. # restart any other shared sockets/descriptors such as Memcached,
  70. # and Redis. TokyoCabinet file handles are safe to reuse
  71. # between any number of forked children (assuming your kernel
  72. # correctly implements pread()/pwrite() system calls)
  73. end