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 dfdcbf6
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 9 deletions.
11 changes: 6 additions & 5 deletions lib/samson/boot_check.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ def check
end
else
# make sure we do not regress into slow startup time by preloading too much
[
ActiveRecord::Base.send(:descendants).map(&:name) - ["Audited::Audit"],
bad = [
ActiveRecord::Base.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" }
(const_defined?(:Mocha) && "mocha"),
((Thread.list.count != 1) && "Extra threads")
].flatten.select { |x| x }
raise "#{bad.join(", ")} should not be loaded" if bad.any?
end
end
end
Expand Down
19 changes: 19 additions & 0 deletions lib/samson/logging.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,22 @@
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
Rails.logger.reopen(dev.path) if dev&.is_a?(File)
end
end
end
20 changes: 17 additions & 3 deletions test/lib/samson/boot_check_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,23 @@

describe Samson::BootCheck do
describe ".check" do
it "warns about loaded models in regular mode" do
e = assert_raises(RuntimeError) { Samson::BootCheck.check }
e.message.must_include "should not be loaded"
describe "in regular mode" do
it "warns about loaded models/threads/mocha in regular mode" do
e = assert_raises(RuntimeError) { Samson::BootCheck.check }
e.message.must_include "User"
e.message.must_include "thread"
e.message.must_include "mocha"
end

it "does not warn when everything is ok" do
Thread.stubs(:list).returns([1])
Samson::BootCheck.expects(:const_defined?)
ActiveRecord::Base.expects(:descendants).returns([])
ActionController::Base.expects(:descendants).returns([])
Samson::BootCheck.check
ensure
Thread.unstub(:list)
end
end

describe "in server mode" do
Expand Down
2 changes: 1 addition & 1 deletion test/lib/samson/logging_test.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true
require_relative '../../test_helper'

SingleCov.covered! uncovered: 15
SingleCov.not_covered!

describe 'Logging' do
end

0 comments on commit dfdcbf6

Please sign in to comment.