From 0b3aec9c945cdcf6d7b11afd99ed9a17608b9890 Mon Sep 17 00:00:00 2001 From: Sam Date: Fri, 21 Apr 2017 11:36:51 -0400 Subject: [PATCH] FEATURE: set UNICORN_STATS_SOCKET_DIR for status socket eg: sam@ubuntu stats_sockets % socat - UNIX-CONNECT:9622.sock gc_stat {"count":46,"heap_allocated_pages":2459,"heap_sorted_length":2460,"heap_allocatable_pages":0,"heap_available_slots":1002267,"heap_live_slots":647293,"heap_free_slots":354974,"heap_final_slots":0,"heap_marked_slots":503494,"heap_swept_slots":498773,"heap_eden_pages":2459,"heap_tomb_pages":0,"total_allocated_pages":2459,"total_freed_pages":0,"total_allocated_objects":4337014,"total_freed_objects":3689721,"malloc_increase_bytes":6448248,"malloc_increase_bytes_limit":29188387,"minor_gc_count":36,"major_gc_count":10,"remembered_wb_unprotected_objects":19958,"remembered_wb_unprotected_objects_limit":39842,"old_objects":462019,"old_objects_limit":895782,"oldmalloc_increase_bytes":6448696,"oldmalloc_increase_bytes_limit":19350882} --- Gemfile.lock | 6 +++--- config/unicorn.conf.rb | 42 +++++++++++++++++++++++++++++++++++++++++- lib/demon/sidekiq.rb | 6 ++++++ 3 files changed, 50 insertions(+), 4 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index 3dda61b6d97..3ccbe4a87dd 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -140,7 +140,7 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) jwt (1.5.6) - kgio (2.10.0) + kgio (2.11.0) libv8 (5.3.332.38.5) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) @@ -272,7 +272,7 @@ GEM activesupport (= 4.2.8) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) - raindrops (0.17.0) + raindrops (0.18.0) rake (11.3.0) rake-compiler (0.9.9) rake @@ -380,7 +380,7 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.7.1) - unicorn (5.2.0) + unicorn (5.3.0) kgio (~> 2.6) raindrops (~> 0.7) uniform_notifier (1.10.0) diff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rb index 5f51df510a5..0fc36af3c08 100644 --- a/config/unicorn.conf.rb +++ b/config/unicorn.conf.rb @@ -36,6 +36,24 @@ preload_app true # fast LAN. check_client_connection false +@stats_socket_dir = ENV["UNICORN_STATS_SOCKET_DIR"] + +def clean_up_stats_socket(server, pid) + if @stats_socket_dir.present? + name = "#{@stats_socket_dir}/#{pid}.sock" + FileUtils.rm_f(name) + server.logger.info "Cleaned up stats socket at #{name}" + end +end + +def start_stats_socket(server) + if @stats_socket_dir.present? + name = "#{@stats_socket_dir}/#{Process.pid}.sock" + StatsSocket.new(name).start + server.logger.info "Started stats socket at #{name}" + end +end + initialized = false before_fork do |server, worker| @@ -51,6 +69,18 @@ before_fork do |server, worker| # router warm up Rails.application.routes.recognize_path('abc') rescue nil + if @stats_socket_dir.present? + server.logger.info "Initializing stats socket at #{@stats_socket_dir}" + begin + FileUtils.mkdir_p @stats_socket_dir + FileUtils.rm_f Dir.glob("#{@stats_socket_dir}/*.sock") + require 'stats_socket' + start_stats_socket(server) + rescue => e + server.logger.info "Failed to initialize stats socket dir #{e}" + end + end + # get rid of rubbish so we don't share it GC.start @@ -75,6 +105,11 @@ before_fork do |server, worker| require 'demon/sidekiq' + if @stats_socket_dir + Demon::Sidekiq.after_fork do + start_stats_socket(server) + end + end Demon::Sidekiq.start(sidekiqs) Signal.trap("SIGTSTP") do @@ -168,12 +203,17 @@ before_fork do |server, worker| sleep 1 end +after_worker_exit do |server, worker, status| + clean_up_stats_socket(server, status.pid) +end + after_fork do |server, worker| + start_stats_socket(server) + # warm up v8 after fork, that way we do not fork a v8 context # it may cause issues if bg threads in a v8 isolate randomly stop # working due to fork Discourse.after_fork - begin PrettyText.cook("warm up **pretty text**") rescue => e diff --git a/lib/demon/sidekiq.rb b/lib/demon/sidekiq.rb index 4e984bc7816..b095cb951a6 100644 --- a/lib/demon/sidekiq.rb +++ b/lib/demon/sidekiq.rb @@ -6,6 +6,10 @@ class Demon::Sidekiq < Demon::Base "sidekiq" end + def self.after_fork(&blk) + blk ? (@blk=blk) : @blk + end + private def suppress_stdout @@ -17,6 +21,8 @@ class Demon::Sidekiq < Demon::Base end def after_fork + Demon::Sidekiq.after_fork&.call + STDERR.puts "Loading Sidekiq in process id #{Process.pid}" require 'sidekiq/cli' # CLI will close the logger, if we have one set we can be in big