2013-07-06 23:30:52 -05:00
|
|
|
# See http://unicorn.bogomips.org/Unicorn/Configurator.html
|
|
|
|
|
2013-11-14 19:15:37 -06:00
|
|
|
# enable out of band gc out of the box, it is low risk and improves perf a lot
|
2014-02-15 23:44:51 -06:00
|
|
|
ENV['UNICORN_ENABLE_OOBGC'] ||= "1"
|
2013-11-12 22:28:39 -06:00
|
|
|
|
2013-07-06 23:30:52 -05:00
|
|
|
discourse_path = File.expand_path(File.expand_path(File.dirname(__FILE__)) + "/../")
|
|
|
|
|
|
|
|
# tune down if not enough ram
|
2013-10-30 00:33:08 -05:00
|
|
|
worker_processes (ENV["UNICORN_WORKERS"] || 3).to_i
|
2013-07-06 23:30:52 -05:00
|
|
|
|
|
|
|
working_directory discourse_path
|
|
|
|
|
2013-10-09 21:33:52 -05:00
|
|
|
# listen "#{discourse_path}/tmp/sockets/unicorn.sock"
|
2014-02-15 23:44:51 -06:00
|
|
|
listen (ENV["UNICORN_PORT"] || 3000).to_i
|
2013-07-06 23:30:52 -05:00
|
|
|
|
|
|
|
# nuke workers after 30 seconds instead of 60 seconds (the default)
|
|
|
|
timeout 30
|
|
|
|
|
|
|
|
# feel free to point this anywhere accessible on the filesystem
|
|
|
|
pid "#{discourse_path}/tmp/pids/unicorn.pid"
|
|
|
|
|
|
|
|
# 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
|
|
|
|
|
|
|
|
initialized = false
|
|
|
|
before_fork do |server, worker|
|
2013-10-09 21:33:52 -05:00
|
|
|
|
2013-07-06 23:30:52 -05:00
|
|
|
unless initialized
|
|
|
|
# load up the yaml for the localization bits, in master process
|
|
|
|
I18n.t(:posts)
|
2013-11-12 22:28:39 -06:00
|
|
|
|
|
|
|
# load up all models and schema
|
|
|
|
(ActiveRecord::Base.connection.tables - %w[schema_migrations]).each do |table|
|
|
|
|
table.classify.constantize.first rescue nil
|
|
|
|
end
|
|
|
|
|
2013-11-19 19:35:33 -06:00
|
|
|
# router warm up
|
|
|
|
Rails.application.routes.recognize_path('abc') rescue nil
|
|
|
|
|
2013-07-06 23:30:52 -05:00
|
|
|
# get rid of rubbish so we don't share it
|
|
|
|
GC.start
|
2013-11-12 22:28:39 -06: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 20:03:36 -05:00
|
|
|
sidekiqs = ENV['UNICORN_SIDEKIQS'].to_i
|
|
|
|
if sidekiqs > 0
|
|
|
|
puts "Starting up #{sidekiqs} supervised sidekiqs"
|
2014-04-22 22:13:18 -05:00
|
|
|
|
2014-04-22 20:03:36 -05:00
|
|
|
require 'demon/sidekiq'
|
|
|
|
|
|
|
|
Demon::Sidekiq.start(sidekiqs)
|
|
|
|
|
|
|
|
class ::Unicorn::HttpServer
|
|
|
|
alias :master_sleep_orig :master_sleep
|
|
|
|
|
2015-03-26 22:27:01 -05:00
|
|
|
def max_rss
|
|
|
|
`ps -eo rss,args | grep sidekiq | grep -v grep | awk '{print $1}'`
|
|
|
|
.split("\n")
|
|
|
|
.map(&:to_i)
|
|
|
|
.max * 1024
|
|
|
|
end
|
|
|
|
|
|
|
|
def out_of_memory?
|
|
|
|
max_allowed_size = [ENV['UNICORN_SIDEKIQ_MAX_RSS'].to_i, 500].min;
|
|
|
|
max_rss > max_allowed_size
|
|
|
|
end
|
|
|
|
|
2014-04-22 22:13:18 -05: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 22:27:01 -05:00
|
|
|
|
2014-04-22 22:13:18 -05:00
|
|
|
last_heartbeat = Jobs::RunHeartbeat.last_heartbeat
|
2015-03-26 22:51:22 -05:00
|
|
|
if false && out_of_memory?
|
2015-03-26 22:27:01 -05:00
|
|
|
Rails.logger.warn("Sidekiq is consuming too much memory (using: %0.2fM), restarting" % (max_rss.to_f / 1.megabyte))
|
|
|
|
Demon::Sidekiq.restart
|
|
|
|
end
|
|
|
|
|
2014-04-22 22:13:18 -05:00
|
|
|
if last_heartbeat < Time.new.to_i - @sidekiq_heartbeat_interval
|
|
|
|
STDERR.puts "Sidekiq heartbeat test failed, restarting"
|
2015-03-26 22:27:01 -05:00
|
|
|
Rails.logger.warn "Sidekiq heartbeat test failed, restarting"
|
2014-04-22 22:13:18 -05:00
|
|
|
|
|
|
|
Demon::Sidekiq.restart
|
|
|
|
end
|
|
|
|
@sidekiq_next_heartbeat_check = Time.new.to_i + @sidekiq_heartbeat_interval
|
2015-03-26 22:27:01 -05:00
|
|
|
|
|
|
|
$redis.client.disconnect
|
2014-04-22 22:13:18 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-04-22 20:03:36 -05:00
|
|
|
def master_sleep(sec)
|
|
|
|
Demon::Sidekiq.ensure_running
|
2014-04-22 22:13:18 -05:00
|
|
|
check_sidekiq_heartbeat
|
|
|
|
|
2014-04-22 20:03:36 -05:00
|
|
|
master_sleep_orig(sec)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-07-06 23:30:52 -05:00
|
|
|
end
|
2013-10-09 21:33:52 -05:00
|
|
|
|
2013-07-06 23:30:52 -05: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 21:33:52 -05:00
|
|
|
sleep 1
|
2013-07-06 23:30:52 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
after_fork do |server, worker|
|
2014-03-27 21:48:14 -05:00
|
|
|
Discourse.after_fork
|
2013-07-06 23:30:52 -05:00
|
|
|
end
|