FIX: Reopen sidekiq log files after rotation (#9429)

FIX: Reopen sidekiq log files after rotation (#9429)

Unicorn uses the USR1 to indicate that log files should be reopened. This commit implements the same functionality for our forked sidekiq workers:

  • USR1 is intercepted in the unicorn master, and re-issued to all child processes
  • USR1 is trapped in the sidekiq processes, and Unicorn::Util.reopen_logs is used to re-open log files
diff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rb
index dd698f3..268edb6 100644
--- a/config/unicorn.conf.rb
+++ b/config/unicorn.conf.rb
@@ -95,6 +95,13 @@ before_fork do |server, worker|
         Demon::Sidekiq.stop
       end
 
+      # Trap USR1, so we can re-issue to sidekiq workers
+      # but chain the default unicorn implementation as well
+      old_handler = Signal.trap("USR1") do
+        Demon::Sidekiq.kill("USR1")
+        old_handler.call
+      end
+
       class ::Unicorn::HttpServer
         alias :master_sleep_orig :master_sleep
 
diff --git a/lib/demon/base.rb b/lib/demon/base.rb
index d162611..fc752a4 100644
--- a/lib/demon/base.rb
+++ b/lib/demon/base.rb
@@ -37,6 +37,13 @@ class Demon::Base
     end
   end
 
+  def self.kill(signal)
+    return unless @demons
+    @demons.values.each do |demon|
+      demon.kill(signal)
+    end
+  end
+
   attr_reader :pid, :parent_pid, :started, :index
   attr_accessor :stop_timeout
 
@@ -63,6 +70,10 @@ class Demon::Base
     end
   end
 
+  def kill(signal)
+    Process.kill(signal, @pid)
+  end
+
   def stop_signal
     "HUP"
   end
diff --git a/lib/demon/sidekiq.rb b/lib/demon/sidekiq.rb
index e5ff82f..a66fed3 100644
--- a/lib/demon/sidekiq.rb
+++ b/lib/demon/sidekiq.rb
@@ -34,6 +34,13 @@ class Demon::Sidekiq < ::Demon::Base
     Sidekiq.logger = nil
     cli = Sidekiq::CLI.instance
 
+    # Unicorn uses USR1 to indicate that log files have been rotated
+    Signal.trap("USR1") do
+      puts "Sidekiq PID #{Process.pid} reopening logs..."
+      Unicorn::Util.reopen_logs
+      puts "Sidekiq PID #{Process.pid} done reopening logs..."
+    end
+
     options = ["-c", GlobalSetting.sidekiq_workers.to_s]
 
     [['critical', 8], ['default', 4], ['low', 2], ['ultra_low', 1]].each do |queue_name, weight|

GitHub sha: ed6b3b82

This commit appears in #9429 which was approved by eviltrout. It was merged by davidtaylorhq.