unicorn.rb 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Where our application lives. $RAILS_ROOT is defined in our Dockerfile.
  2. app_path = ENV['RAILS_ROOT']
  3. crm_path = ENV['CRM_ROOT']
  4. # Set the server's working directory
  5. working_directory app_path
  6. # Define where Unicorn should write its PID file
  7. pid "#{app_path}/tmp/pids/unicorn.pid"
  8. # Bind Unicorn to the container's default route, at port 3000
  9. listen "0.0.0.0:3000"
  10. # Define where Unicorn should write its log files
  11. stderr_path "#{app_path}/log/unicorn.stderr.log"
  12. stdout_path "#{app_path}/log/unicorn.stdout.log"
  13. # Define the number of workers Unicorn should spin up.
  14. # A new Rails app just needs one. You would scale this
  15. # higher in the future once your app starts getting traffic.
  16. # See https://unicorn.bogomips.org/TUNING.html
  17. worker_processes 1
  18. # Make sure we use the correct Gemfile on restarts
  19. before_exec do |server|
  20. ENV['BUNDLE_GEMFILE'] = "#{app_path}/Gemfile"
  21. end
  22. # Speeds up your workers.
  23. # See https://unicorn.bogomips.org/TUNING.html
  24. preload_app true
  25. #
  26. # Below we define how our workers should be spun up.
  27. # See https://unicorn.bogomips.org/Unicorn/Configurator.html
  28. #
  29. before_fork do |server, worker|
  30. # the following is highly recomended for Rails + "preload_app true"
  31. # as there's no need for the master process to hold a connection
  32. if defined?(ActiveRecord::Base)
  33. ActiveRecord::Base.connection.disconnect!
  34. end
  35. # Before forking, kill the master process that belongs to the .oldbin PID.
  36. # This enables 0 downtime deploys.
  37. old_pid = "#{server.config[:pid]}.oldbin"
  38. if File.exists?(old_pid) && server.pid != old_pid
  39. begin
  40. Process.kill("QUIT", File.read(old_pid).to_i)
  41. rescue Errno::ENOENT, Errno::ESRCH
  42. # someone else did our job for us
  43. end
  44. end
  45. end
  46. after_fork do |server, worker|
  47. if defined?(ActiveRecord::Base)
  48. ActiveRecord::Base.establish_connection
  49. end
  50. end