2013-07-07 00:30:52 -04:00
|
|
|
# See http://unicorn.bogomips.org/Unicorn/Configurator.html
|
|
|
|
|
2013-11-14 20:15:37 -05:00
|
|
|
# enable out of band gc out of the box, it is low risk and improves perf a lot
|
2014-02-16 00:44:51 -05:00
|
|
|
ENV['UNICORN_ENABLE_OOBGC'] ||= "1"
|
2013-11-12 23:28:39 -05:00
|
|
|
|
2013-07-07 00:30:52 -04:00
|
|
|
discourse_path = File.expand_path(File.expand_path(File.dirname(__FILE__)) + "/../")
|
|
|
|
|
|
|
|
# tune down if not enough ram
|
2013-10-30 01:33:08 -04:00
|
|
|
worker_processes (ENV["UNICORN_WORKERS"] || 3).to_i
|
2013-07-07 00:30:52 -04:00
|
|
|
|
|
|
|
working_directory discourse_path
|
|
|
|
|
2013-10-09 22:33:52 -04:00
|
|
|
# listen "#{discourse_path}/tmp/sockets/unicorn.sock"
|
2014-02-16 00:44:51 -05:00
|
|
|
listen (ENV["UNICORN_PORT"] || 3000).to_i
|
2013-07-07 00:30:52 -04:00
|
|
|
|
|
|
|
# nuke workers after 30 seconds instead of 60 seconds (the default)
|
|
|
|
timeout 30
|
|
|
|
|
|
|
|
# feel free to point this anywhere accessible on the filesystem
|
2017-06-05 22:46:01 -04:00
|
|
|
pid (ENV["UNICORN_PID_PATH"] || "#{discourse_path}/tmp/pids/unicorn.pid")
|
2013-07-07 00:30:52 -04:00
|
|
|
|
|
|
|
# By default, the Unicorn logger will write to stderr.
|
|
|
|
# Additionally, some applications/frameworks log to stderr or stdout,
|
|
|
|
# so prevent them from going to /dev/null when daemonized here:
|
|
|
|
stderr_path "#{discourse_path}/log/unicorn.stderr.log"
|
|
|
|
stdout_path "#{discourse_path}/log/unicorn.stdout.log"
|
|
|
|
|
|
|
|
# important for Ruby 2.0
|
|
|
|
preload_app true
|
|
|
|
|
|
|
|
# Enable this flag to have unicorn test client connections by writing the
|
|
|
|
# beginning of the HTTP headers before calling the application. This
|
|
|
|
# prevents calling the application for connections that have disconnected
|
|
|
|
# while queued. This is only guaranteed to detect clients on the same
|
|
|
|
# host unicorn runs on, and unlikely to detect disconnects even on a
|
|
|
|
# fast LAN.
|
|
|
|
check_client_connection false
|
|
|
|
|
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}
2017-04-21 11:36:51 -04:00
|
|
|
@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
|
2017-04-21 12:37:28 -04:00
|
|
|
rescue => e
|
|
|
|
server.logger.warn "Failed to clean up stats socket #{e}"
|
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}
2017-04-21 11:36:51 -04:00
|
|
|
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
|
2017-04-21 12:37:28 -04:00
|
|
|
rescue => e
|
|
|
|
server.logger.warn "Failed to start stats socket #{e}"
|
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}
2017-04-21 11:36:51 -04:00
|
|
|
end
|
|
|
|
|
2013-07-07 00:30:52 -04:00
|
|
|
initialized = false
|
|
|
|
before_fork do |server, worker|
|
2013-10-09 22:33:52 -04:00
|
|
|
|
2013-07-07 00:30:52 -04:00
|
|
|
unless initialized
|
|
|
|
# load up the yaml for the localization bits, in master process
|
|
|
|
I18n.t(:posts)
|
2013-11-12 23:28:39 -05:00
|
|
|
|
|
|
|
# load up all models and schema
|
2017-10-12 05:14:36 -04:00
|
|
|
(ActiveRecord::Base.connection.tables - %w[schema_migrations versions]).each do |table|
|
2013-11-12 23:28:39 -05:00
|
|
|
table.classify.constantize.first rescue nil
|
|
|
|
end
|
|
|
|
|
2013-11-19 20:35:33 -05:00
|
|
|
# router warm up
|
|
|
|
Rails.application.routes.recognize_path('abc') rescue nil
|
|
|
|
|
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}
2017-04-21 11:36:51 -04:00
|
|
|
if @stats_socket_dir.present?
|
|
|
|
server.logger.info "Initializing stats socket at #{@stats_socket_dir}"
|
|
|
|
begin
|
2017-04-21 12:37:28 -04:00
|
|
|
require 'stats_socket'
|
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}
2017-04-21 11:36:51 -04:00
|
|
|
FileUtils.mkdir_p @stats_socket_dir
|
|
|
|
FileUtils.rm_f Dir.glob("#{@stats_socket_dir}/*.sock")
|
|
|
|
start_stats_socket(server)
|
|
|
|
rescue => e
|
|
|
|
server.logger.info "Failed to initialize stats socket dir #{e}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-10-03 23:22:23 -04:00
|
|
|
# preload discourse version
|
|
|
|
Discourse.git_version
|
|
|
|
Discourse.git_branch
|
|
|
|
Discourse.full_version
|
|
|
|
|
2013-07-07 00:30:52 -04:00
|
|
|
# get rid of rubbish so we don't share it
|
|
|
|
GC.start
|
2013-11-12 23:28:39 -05:00
|
|
|
|
|
|
|
initialized = true
|
|
|
|
|
|
|
|
supervisor = ENV['UNICORN_SUPERVISOR_PID'].to_i
|
|
|
|
if supervisor > 0
|
|
|
|
Thread.new do
|
|
|
|
while true
|
|
|
|
unless File.exists?("/proc/#{supervisor}")
|
|
|
|
puts "Kill self supervisor is gone"
|
|
|
|
Process.kill "TERM", Process.pid
|
|
|
|
end
|
|
|
|
sleep 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-22 21:03:36 -04:00
|
|
|
sidekiqs = ENV['UNICORN_SIDEKIQS'].to_i
|
|
|
|
if sidekiqs > 0
|
|
|
|
puts "Starting up #{sidekiqs} supervised sidekiqs"
|
2014-04-22 23:13:18 -04:00
|
|
|
|
2014-04-22 21:03:36 -04:00
|
|
|
require 'demon/sidekiq'
|
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}
2017-04-21 11:36:51 -04:00
|
|
|
if @stats_socket_dir
|
|
|
|
Demon::Sidekiq.after_fork do
|
|
|
|
start_stats_socket(server)
|
2017-10-23 02:12:25 -04:00
|
|
|
DiscourseEvent.trigger(:sidekiq_fork_started)
|
|
|
|
end
|
|
|
|
else
|
|
|
|
Demon::Sidekiq.after_fork do
|
|
|
|
DiscourseEvent.trigger(:sidekiq_fork_started)
|
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}
2017-04-21 11:36:51 -04:00
|
|
|
end
|
|
|
|
end
|
2014-04-22 21:03:36 -04:00
|
|
|
Demon::Sidekiq.start(sidekiqs)
|
|
|
|
|
2017-03-10 11:35:25 -05:00
|
|
|
Signal.trap("SIGTSTP") do
|
|
|
|
STDERR.puts "#{Time.now}: Issuing stop to sidekiq"
|
|
|
|
Demon::Sidekiq.stop
|
|
|
|
end
|
|
|
|
|
2014-04-22 21:03:36 -04:00
|
|
|
class ::Unicorn::HttpServer
|
|
|
|
alias :master_sleep_orig :master_sleep
|
|
|
|
|
2015-03-26 23:27:01 -04:00
|
|
|
def max_rss
|
2015-03-27 00:44:52 -04:00
|
|
|
rss = `ps -eo rss,args | grep sidekiq | grep -v grep | awk '{print $1}'`
|
2017-07-27 21:20:09 -04:00
|
|
|
.split("\n")
|
|
|
|
.map(&:to_i)
|
|
|
|
.max
|
2015-03-27 00:44:52 -04:00
|
|
|
|
|
|
|
rss ||= 0
|
|
|
|
|
|
|
|
rss * 1024
|
2015-03-26 23:27:01 -04:00
|
|
|
end
|
|
|
|
|
2015-06-18 01:32:04 -04:00
|
|
|
def max_allowed_size
|
|
|
|
[ENV['UNICORN_SIDEKIQ_MAX_RSS'].to_i, 500].max.megabytes
|
|
|
|
end
|
|
|
|
|
2015-03-26 23:27:01 -04:00
|
|
|
def out_of_memory?
|
|
|
|
max_rss > max_allowed_size
|
|
|
|
end
|
|
|
|
|
2015-06-18 01:32:04 -04:00
|
|
|
def force_kill_rogue_sidekiq
|
|
|
|
info = `ps -eo pid,rss,args | grep sidekiq | grep -v grep | awk '{print $1,$2}'`
|
|
|
|
info.split("\n").each do |row|
|
2017-07-27 21:20:09 -04:00
|
|
|
pid, mem = row.split(" ").map(&:to_i)
|
|
|
|
if pid > 0 && (mem * 1024) > max_allowed_size
|
|
|
|
Rails.logger.warn "Detected rogue Sidekiq pid #{pid} mem #{mem * 1024}, killing"
|
2015-06-18 01:32:04 -04:00
|
|
|
Process.kill("KILL", pid) rescue nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-22 23:13:18 -04:00
|
|
|
def check_sidekiq_heartbeat
|
|
|
|
@sidekiq_heartbeat_interval ||= 30.minutes
|
|
|
|
@sidekiq_next_heartbeat_check ||= Time.new.to_i + @sidekiq_heartbeat_interval
|
|
|
|
|
|
|
|
if @sidekiq_next_heartbeat_check < Time.new.to_i
|
2015-03-26 23:27:01 -04:00
|
|
|
|
2014-04-22 23:13:18 -04:00
|
|
|
last_heartbeat = Jobs::RunHeartbeat.last_heartbeat
|
2015-03-27 00:44:52 -04:00
|
|
|
restart = false
|
|
|
|
|
|
|
|
if out_of_memory?
|
2015-05-12 13:43:25 -04:00
|
|
|
Rails.logger.warn("Sidekiq is consuming too much memory (using: %0.2fM) for '%s', restarting" % [(max_rss.to_f / 1.megabyte), ENV["DISCOURSE_HOSTNAME"]])
|
2015-03-27 00:44:52 -04:00
|
|
|
restart = true
|
2015-03-26 23:27:01 -04:00
|
|
|
end
|
|
|
|
|
2014-04-22 23:13:18 -04:00
|
|
|
if last_heartbeat < Time.new.to_i - @sidekiq_heartbeat_interval
|
|
|
|
STDERR.puts "Sidekiq heartbeat test failed, restarting"
|
2015-03-26 23:27:01 -04:00
|
|
|
Rails.logger.warn "Sidekiq heartbeat test failed, restarting"
|
2014-04-22 23:13:18 -04:00
|
|
|
|
2015-03-27 00:44:52 -04:00
|
|
|
restart = true
|
2014-04-22 23:13:18 -04:00
|
|
|
end
|
|
|
|
@sidekiq_next_heartbeat_check = Time.new.to_i + @sidekiq_heartbeat_interval
|
2015-03-26 23:27:01 -04:00
|
|
|
|
2015-06-18 01:32:04 -04:00
|
|
|
if restart
|
|
|
|
Demon::Sidekiq.restart
|
|
|
|
sleep 10
|
|
|
|
force_kill_rogue_sidekiq
|
|
|
|
end
|
2015-03-26 23:27:01 -04:00
|
|
|
$redis.client.disconnect
|
2014-04-22 23:13:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-22 21:03:36 -04:00
|
|
|
def master_sleep(sec)
|
|
|
|
Demon::Sidekiq.ensure_running
|
2014-04-22 23:13:18 -04:00
|
|
|
check_sidekiq_heartbeat
|
|
|
|
|
2014-04-22 21:03:36 -04:00
|
|
|
master_sleep_orig(sec)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-07 00:30:52 -04:00
|
|
|
end
|
2013-10-09 22:33:52 -04:00
|
|
|
|
2013-07-07 00:30:52 -04:00
|
|
|
ActiveRecord::Base.connection.disconnect!
|
|
|
|
$redis.client.disconnect
|
|
|
|
|
|
|
|
# Throttle the master from forking too quickly by sleeping. Due
|
|
|
|
# to the implementation of standard Unix signal handlers, this
|
|
|
|
# helps (but does not completely) prevent identical, repeated signals
|
|
|
|
# from being lost when the receiving process is busy.
|
2013-10-09 22:33:52 -04:00
|
|
|
sleep 1
|
2013-07-07 00:30:52 -04:00
|
|
|
end
|
|
|
|
|
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}
2017-04-21 11:36:51 -04:00
|
|
|
after_worker_exit do |server, worker, status|
|
|
|
|
clean_up_stats_socket(server, status.pid)
|
|
|
|
end
|
|
|
|
|
2013-07-07 00:30:52 -04:00
|
|
|
after_fork do |server, worker|
|
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}
2017-04-21 11:36:51 -04:00
|
|
|
start_stats_socket(server)
|
|
|
|
|
2017-10-23 00:40:31 -04:00
|
|
|
DiscourseEvent.trigger(:web_fork_started)
|
|
|
|
|
2016-07-16 01:11:34 -04:00
|
|
|
# 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
|
2014-03-27 22:48:14 -04:00
|
|
|
Discourse.after_fork
|
2016-09-06 02:02:08 -04:00
|
|
|
begin
|
|
|
|
PrettyText.cook("warm up **pretty text**")
|
|
|
|
rescue => e
|
|
|
|
Rails.logger.error("Failed to warm up pretty text: #{e}")
|
|
|
|
end
|
2013-07-07 00:30:52 -04:00
|
|
|
end
|