Skip to content

Commit

Permalink
stop puma from sending INT on SIGHUP which we use for logrotate
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Mar 9, 2018
1 parent e35cf2f commit 00c1b23
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
4 changes: 2 additions & 2 deletions lib/samson/boot_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def check
ActiveRecord::Base.send(:descendants).map(&:name) - ["Audited::Audit"],
ActionController::Base.descendants.map(&:name),
(defined?(Mocha) && "mocha"),
Thread.list.count == 1 || "Extra threads"
].compact.flatten.each { |c| raise "#{c} should not be loaded" }
((Thread.list.count != 1) && "Extra threads")
].flatten.each { |c| raise "#{c} should not be loaded" if c }
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions lib/samson/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,23 @@
config.logger = Syslog::Logger.new('samson')
config.lograge.formatter = Lograge::Formatters::Logstash.new
end

# puma kills itself with INT when it receives a SIGHUP see https://github.com/puma/puma/blob/master/docs/signals.md
# If we have a file based logger then we need to reopen that file to log to the new logfile after log rotation.
# The rails logger does not know what file it opened, so we have to help it https://github.com/rails/rails/issues/32211
# If we do not have a file-based logger we still do not want to kill the process, but log that we got the signal.
# We need to use self-pipe since we cannot reopen logs in an interrupt.
# This will break stdout_redirect feature of puma, which I hope nobody uses.
if ENV["SERVER_MODE"]
read, write = IO.pipe
Signal.trap(:SIGHUP) { write.puts }
Thread.new do
loop do
read.gets
Rails.logger.info "Received SIGHUP ... reopening logs"
dev = Rails.logger.instance_variable_get(:@logdev)&.dev
path = dev.path if dev&.is_a?(File)
Rails.logger.reopen(path)
end
end
end

0 comments on commit 00c1b23

Please sign in to comment.