unicorn.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  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. if ENV['DOMAIN']
  17. force_ssl = ENV['FORCE_SSL'] == 'true'
  18. Net::HTTP.get_response(URI((force_ssl ? "https://" : "http://") + ENV['DOMAIN']))
  19. end
  20. begin
  21. Process.getpgid worker_pid
  22. rescue Errno::ESRCH
  23. # No longer running
  24. worker_pid = nil
  25. end
  26. end
  27. end
  28. before_fork do |server, worker|
  29. Signal.trap 'TERM' do
  30. puts 'Unicorn master intercepting TERM and sending myself QUIT instead'
  31. Process.kill 'QUIT', Process.pid
  32. end
  33. defined?(ActiveRecord::Base) and
  34. ActiveRecord::Base.connection.disconnect!
  35. end
  36. after_fork do |server, worker|
  37. Signal.trap 'TERM' do
  38. puts 'Unicorn worker intercepting TERM and doing nothing. Wait for master to send QUIT'
  39. end
  40. defined?(ActiveRecord::Base) and
  41. ActiveRecord::Base.establish_connection
  42. end