2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
# this is a trivial graceful restart on touch of tmp/restart.
|
2013-02-15 21:54:23 -05:00
|
|
|
#
|
|
|
|
# It simply drains all the requests (waits up to 4 seconds) and issues a HUP
|
|
|
|
# if you need a more sophisticated cycling restart for multiple thins it will need to be written
|
|
|
|
#
|
2021-05-20 21:43:47 -04:00
|
|
|
# This works fine for Discourse.org cause we host our app across multiple machines, if you hosting
|
2013-02-15 21:54:23 -05:00
|
|
|
# on a single machine you have a trickier problem at hand as you need to cycle the processes in order
|
2017-05-18 11:36:12 -04:00
|
|
|
#
|
|
|
|
|
|
|
|
# VIM users rejoice, if you add this to your .vimrc CTRL-a will restart puma:
|
|
|
|
# nmap <C-a> <Esc>:!touch tmp/restart<CR><CR>
|
2013-02-15 21:54:23 -05:00
|
|
|
|
2013-02-25 11:42:20 -05:00
|
|
|
Thread.new do
|
2013-02-05 14:16:51 -05:00
|
|
|
file = "#{Rails.root}/tmp/restart"
|
2013-02-15 21:54:23 -05:00
|
|
|
old_time = File.ctime(file).to_i if File.exists? file
|
|
|
|
wait_seconds = 4
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-05-18 11:36:12 -04:00
|
|
|
if Rails.env.development? && $PROGRAM_NAME =~ /puma/
|
|
|
|
require 'listen'
|
2013-02-25 11:42:20 -05:00
|
|
|
|
2017-05-18 11:36:12 -04:00
|
|
|
time = nil
|
2013-02-05 14:16:51 -05:00
|
|
|
|
2017-05-18 11:36:12 -04:00
|
|
|
begin
|
|
|
|
listener = Listen.to("#{Rails.root}/tmp", only: /restart/) do
|
|
|
|
|
|
|
|
time = File.ctime(file).to_i if File.exists? file
|
|
|
|
|
|
|
|
if old_time != time
|
|
|
|
Rails.logger.info "attempting to reload #{$$} #{$PROGRAM_NAME} in #{wait_seconds} seconds"
|
2019-12-03 04:05:53 -05:00
|
|
|
$shutdown = true # rubocop:disable Style/GlobalVars
|
2017-05-18 11:36:12 -04:00
|
|
|
sleep wait_seconds
|
|
|
|
Rails.logger.info "restarting #{$$}"
|
|
|
|
Process.kill("USR2", $$)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
listener.start
|
|
|
|
sleep
|
|
|
|
rescue => e
|
|
|
|
puts "Failed to watch for restart, this probably means the old postgres directory is in tmp, remove it #{e}"
|
2013-02-05 14:16:51 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|