diff --git a/config/initializers/99-rack-cache.rb b/config/initializers/99-rack-cache.rb index 17719fcc5..ed2439eb1 100644 --- a/config/initializers/99-rack-cache.rb +++ b/config/initializers/99-rack-cache.rb @@ -6,7 +6,7 @@ enabled = if Rails.configuration.respond_to?(:enable_anon_caching) Rails.env.production? end -if enabled +if !ENV['DISCOURSE_DISABLE_ANON_CACHE'] && enabled Rails.configuration.middleware.insert 0, Middleware::AnonymousCache end diff --git a/config/initializers/99-unicorn.rb b/config/initializers/99-unicorn.rb new file mode 100644 index 000000000..e5b6473df --- /dev/null +++ b/config/initializers/99-unicorn.rb @@ -0,0 +1,4 @@ +if (oobgc=ENV['UNICORN_OOBGC_REQS'].to_i) > 0 + require 'unicorn/oob_gc' + Rails.configuration.middleware.insert 0, Unicorn::OobGC, oobgc +end diff --git a/config/unicorn.conf.rb b/config/unicorn.conf.rb index 5fcb7f19e..3c4b7bf21 100644 --- a/config/unicorn.conf.rb +++ b/config/unicorn.conf.rb @@ -1,5 +1,8 @@ # See http://unicorn.bogomips.org/Unicorn/Configurator.html +# RUN out of band GC every 2 requests +# ENV['UNICORN_OOBGC_REQS'] = "2" unless ENV['UNICORN_OOBGC_REQS'] + discourse_path = File.expand_path(File.expand_path(File.dirname(__FILE__)) + "/../") # tune down if not enough ram @@ -39,8 +42,30 @@ before_fork do |server, worker| unless initialized # load up the yaml for the localization bits, in master process I18n.t(:posts) + + # load up all models and schema + (ActiveRecord::Base.connection.tables - %w[schema_migrations]).each do |table| + table.classify.constantize.first rescue nil + end + # get rid of rubbish so we don't share it GC.start + + 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 + end ActiveRecord::Base.connection.disconnect! diff --git a/config/unicorn_launcher b/config/unicorn_launcher new file mode 100755 index 000000000..c214b49af --- /dev/null +++ b/config/unicorn_launcher @@ -0,0 +1,39 @@ +#!/bin/bash + +# This is a helper script you can use to supervise unicorn, it allows you to perform a live restart +# by sending it a USR2 signal + +LOCAL_WEB="http://127.0.0.1:3000/" + +function on_exit() +{ + kill $UNICORN_PID + echo "exiting" +} + +function on_reload() +{ + echo "Reloading unicorn" + kill -s USR2 $UNICORN_PID + sleep 10 + curl $LOCAL_WEB &> /dev/null + NEW_UNICORN_PID=`ps -f --ppid $UNICORN_PID | grep unicorn | grep -v worker | awk '{ print $2 }'` + kill $UNICORN_PID + echo "Old pid is: $UNICORN_PID New pid is: $NEW_UNICORN_PID" + UNICORN_PID=$NEW_UNICORN_PID +} + +export UNICORN_SUPERVISOR_PID=$$ + +trap on_exit EXIT +trap on_reload USR2 + +unicorn -c $1 & +UNICORN_PID=$! + +echo "supervisor pid: $UNICORN_SUPERVISOR_PID unicorn pid: $UNICORN_PID" + +while [ -e /proc/$UNICORN_PID ] +do + sleep 0.1 +done