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}
This commit is contained in:
Sam
2017-04-21 11:36:51 -04:00
parent b0151ab66a
commit 0b3aec9c94
3 changed files with 50 additions and 4 deletions

View File

@@ -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