Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 3.77 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # Our own variable where we deploy this app to
  2. deploy_to = "/srv/example.com"
  3.  
  4. current_path = "#{deploy_to}/current"
  5. shared_path = "#{deploy_to}/shared"
  6. shared_bundler_gems_path = "#{shared_path}/bundler_gems"
  7.  
  8. # See http://unicorn.bogomips.org/Sandbox.html
  9. # Helps ensure the correct unicorn_rails is used when upgrading with USR2
  10. Unicorn::HttpServer::START_CTX[0] = "#{shared_bundler_gems_path}/bin/unicorn_rails"
  11.  
  12. # Use at least one worker per core if you're on a dedicated server,
  13. # more will usually help for _short_ waits on databases/caches.
  14. worker_processes 2
  15.  
  16. # Help ensure your application will always spawn in the symlinked
  17. # "current" directory that Capistrano sets up.
  18. working_directory current_path
  19.  
  20. # listen on both a Unix domain socket and a TCP port,
  21. # we use a shorter backlog for quicker failover when busy
  22. listen "/tmp/.staging_unicorn_sock", :backlog => 64
  23. #listen 8080, :tcp_nopush => true
  24.  
  25. # nuke workers after 30 seconds instead of 60 seconds (the default)
  26. timeout 30
  27.  
  28. # feel free to point this anywhere accessible on the filesystem
  29. # pid "/var/run/staging_unicorn.pid"
  30. pid "#{shared_path}/pids/unicorn.pid"
  31.  
  32. # By default, the Unicorn logger will write to stderr.
  33. # Additionally, some applications/frameworks log to stderr or stdout,
  34. # so prevent them from going to /dev/null when daemonized here:
  35. stderr_path "#{shared_path}/log/unicorn.stderr.log"
  36. stdout_path "#{shared_path}/log/unicorn.stdout.log"
  37.  
  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.  
  44. before_fork do |server, worker|
  45.   # the following is highly recomended for Rails + "preload_app true"
  46.   # as there's no need for the master process to hold a connection
  47.   defined?(ActiveRecord::Base) and
  48.     ActiveRecord::Base.connection.disconnect!
  49.  
  50.   # The following is only recommended for memory/DB-constrained
  51.   # installations.  It is not needed if your system can house
  52.   # twice as many worker_processes as you have configured.
  53.   #
  54.   # # This allows a new master process to incrementally
  55.   # # phase out the old master process with SIGTTOU to avoid a
  56.   # # thundering herd (especially in the "preload_app false" case)
  57.   # # when doing a transparent upgrade.  The last worker spawned
  58.   # # will then kill off the old master process with a SIGQUIT.
  59.   old_pid = "#{server.config[:pid]}.oldbin"
  60.   if old_pid != server.pid
  61.     begin
  62.       sig = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
  63.       Process.kill(sig, File.read(old_pid).to_i)
  64.     rescue Errno::ENOENT, Errno::ESRCH
  65.     end
  66.   end
  67.  
  68.   # # *optionally* throttle the master from forking too quickly by sleeping
  69.   # sleep 1
  70.  
  71.   log_env(:before_fork, server)
  72. end
  73.  
  74. after_fork do |server, worker|
  75.   # worker.user('deployer', 'deployer') if Process.euid == 0
  76.  
  77.   # per-process listener ports for debugging/admin/migrations
  78.   # addr = "127.0.0.1:#{9293 + worker.nr}"
  79.   # server.listen(addr, :tries => -1, :delay => 5, :tcp_nopush => true)
  80.  
  81.   # the following is *required* for Rails + "preload_app true",
  82.   defined?(ActiveRecord::Base) and
  83.     ActiveRecord::Base.establish_connection
  84.  
  85.   # if preload_app is true, then you may also want to check and
  86.   # restart any other shared sockets/descriptors such as Memcached,
  87.   # and Redis.  TokyoCabinet file handles are safe to reuse
  88.   # between any number of forked children (assuming your kernel
  89.   # correctly implements pread()/pwrite() system calls)
  90.  
  91.   # Reconnect memcached
  92.   Rails.cache.reset
  93. end
  94.  
  95. before_exec do |server|
  96.   paths = (ENV["PATH"] || "").split(File::PATH_SEPARATOR)
  97.   paths.unshift "#{shared_bundler_gems_path}/bin"
  98.   ENV["PATH"] = paths.uniq.join(File::PATH_SEPARATOR)
  99.  
  100.   ENV['GEM_HOME'] = ENV['GEM_PATH'] = shared_bundler_gems_path
  101.   ENV['BUNDLE_GEMFILE'] = "#{current_path}/Gemfile"
  102. end