Commit 3586fa6a authored by Justin Ramos's avatar Justin Ramos Committed by Benjamin Neff

allowing for graceful unicorn restarts

closes #7217
parent 2ca42ea5
...@@ -28,6 +28,7 @@ ...@@ -28,6 +28,7 @@
* Update notifications every 5 minutes and when opening the notification dropdown [#6952](https://github.com/diaspora/diaspora/pull/6952) * Update notifications every 5 minutes and when opening the notification dropdown [#6952](https://github.com/diaspora/diaspora/pull/6952)
* Show browser notifications when receiving new unread notifications [#6952](https://github.com/diaspora/diaspora/pull/6952) * Show browser notifications when receiving new unread notifications [#6952](https://github.com/diaspora/diaspora/pull/6952)
* Only clear comment textarea when comment submission was successful [#7186](https://github.com/diaspora/diaspora/pull/7186) * Only clear comment textarea when comment submission was successful [#7186](https://github.com/diaspora/diaspora/pull/7186)
* Add support for graceful unicorn restarts [#7217](https://github.com/diaspora/diaspora/pull/7217)
# 0.6.1.0 # 0.6.1.0
......
...@@ -42,7 +42,7 @@ defaults: ...@@ -42,7 +42,7 @@ defaults:
server: server:
listen: '0.0.0.0:3000' listen: '0.0.0.0:3000'
rails_environment: 'development' rails_environment: 'development'
pid: pid: "tmp/pids/web.pid"
stderr_log: stderr_log:
stdout_log: stdout_log:
unicorn_worker: 2 unicorn_worker: 2
......
...@@ -177,8 +177,8 @@ configuration: ## Section ...@@ -177,8 +177,8 @@ configuration: ## Section
#listen: 'unix:/run/diaspora/diaspora.sock' #listen: 'unix:/run/diaspora/diaspora.sock'
#listen: '127.0.0.1:3000' #listen: '127.0.0.1:3000'
## Set the path for the PID file of the unicorn master process (default=none) ## Set the path for the PID file of the unicorn master process (default=tmp/pids/web.pid)
#pid: '/run/diaspora/diaspora.pid' #pid: 'tmp/pids/web.pid'
## Rails environment (default='development'). ## Rails environment (default='development').
## The environment in which the server should be started by default. ## The environment in which the server should be started by default.
......
...@@ -12,10 +12,21 @@ Eye.application("diaspora") do ...@@ -12,10 +12,21 @@ Eye.application("diaspora") do
stderr "log/eye_processes_stderr.log" stderr "log/eye_processes_stderr.log"
process :web do process :web do
start_command "bin/bundle exec unicorn -c config/unicorn.rb" unicorn_command = "bin/bundle exec unicorn -c config/unicorn.rb"
daemonize true
pid_file "tmp/pids/web.pid" if rails_env == "production"
start_command "#{unicorn_command} -D"
daemonize false
restart_command "kill -USR2 {PID}"
restart_grace 10.seconds
else
start_command unicorn_command
daemonize true
end
pid_file AppConfig.server.pid.get
stop_signals [:TERM, 10.seconds] stop_signals [:TERM, 10.seconds]
env "PORT" => ENV["PORT"] env "PORT" => ENV["PORT"]
monitor_children do monitor_children do
......
...@@ -4,7 +4,7 @@ port = ENV["PORT"] ...@@ -4,7 +4,7 @@ port = ENV["PORT"]
port = port && !port.empty? ? port.to_i : nil port = port && !port.empty? ? port.to_i : nil
listen port || AppConfig.server.listen.get unless RACKUP[:set_listener] listen port || AppConfig.server.listen.get unless RACKUP[:set_listener]
pid AppConfig.server.pid.get if AppConfig.server.pid? pid AppConfig.server.pid.get
worker_processes AppConfig.server.unicorn_worker.to_i worker_processes AppConfig.server.unicorn_worker.to_i
timeout AppConfig.server.unicorn_timeout.to_i timeout AppConfig.server.unicorn_timeout.to_i
stderr_path AppConfig.server.stderr_log.get if AppConfig.server.stderr_log? stderr_path AppConfig.server.stderr_log.get if AppConfig.server.stderr_log?
...@@ -26,11 +26,25 @@ before_fork do |_server, _worker| ...@@ -26,11 +26,25 @@ before_fork do |_server, _worker|
end end
end end
after_fork do |_server, _worker| after_fork do |server, worker|
Logging.reopen # reopen logfiles to obtain a new file descriptor Logging.reopen # reopen logfiles to obtain a new file descriptor
ActiveRecord::Base.establish_connection # preloading app in master, so reconnect to DB ActiveRecord::Base.establish_connection # preloading app in master, so reconnect to DB
# We don't generate uuids in the frontend, but let's be on the safe side # We don't generate uuids in the frontend, but let's be on the safe side
UUID.generator.next_sequence UUID.generator.next_sequence
# Check for an old master process from a graceful restart
old_pid = "#{AppConfig.server.pid.get}.oldbin"
if File.exist?(old_pid) && server.pid != old_pid
begin
# Remove a worker from the old master when we fork a new one (TTOU)
# Except for the last worker forked by this server, which kills the old master (QUIT)
signal = (worker.nr + 1) >= server.worker_processes ? :QUIT : :TTOU
Process.kill(signal, File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment