unicorn.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. require "net/http"
  2. worker_processes Integer(ENV["WEB_CONCURRENCY"] || 2)
  3. timeout 15
  4. preload_app true
  5. # Note that this will only work correctly when running Heroku with ONE web worker.
  6. # If you want to run more than one, use the standard Huginn Procfile instead, with separate web and job entries.
  7. # You'll need to set the Heroku config variable PROCFILE_PATH to 'Procfile'.
  8. Thread.new do
  9. worker_pid = nil
  10. while true
  11. if worker_pid.nil?
  12. worker_pid = spawn("bundle exec rails runner bin/threaded.rb")
  13. puts "New threaded worker PID: #{worker_pid}"
  14. end
  15. sleep 45
  16. begin
  17. Process.getpgid worker_pid
  18. rescue Errno::ESRCH
  19. # No longer running
  20. worker_pid = nil
  21. end
  22. end
  23. end
  24. before_fork do |server, worker|
  25. Signal.trap 'TERM' do
  26. puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
  27. Process.kill 'QUIT', Process.pid
  28. end
  29. defined?(ActiveRecord::Base) and
  30. ActiveRecord::Base.connection.disconnect!
  31. end
  32. after_fork do |server, worker|
  33. Signal.trap 'TERM' do
  34. puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  35. end
  36. defined?(ActiveRecord::Base) and
  37. ActiveRecord::Base.establish_connection
  38. end