Commit eb3d2e2cb368b8953b0f85e20d727701aa536229
1 parent
8dfded9f
Exists in
staging
and in
25 other branches
core: switch to unicorn
Showing
8 changed files
with
71 additions
and
32 deletions
Show diff stats
Gemfile
| @@ -10,7 +10,7 @@ gem 'RedCloth', '~> 4.2.9' | @@ -10,7 +10,7 @@ gem 'RedCloth', '~> 4.2.9' | ||
| 10 | gem 'will_paginate', '~> 3.0.3' | 10 | gem 'will_paginate', '~> 3.0.3' |
| 11 | gem 'ruby-feedparser', '~> 0.7' | 11 | gem 'ruby-feedparser', '~> 0.7' |
| 12 | gem 'daemons', '~> 1.1.5' | 12 | gem 'daemons', '~> 1.1.5' |
| 13 | -gem 'thin', '~> 1.3.1' | 13 | +gem 'unicorn', '~> 4.8' |
| 14 | gem 'nokogiri', '~> 1.5.5' | 14 | gem 'nokogiri', '~> 1.5.5' |
| 15 | gem 'rake', :require => false | 15 | gem 'rake', :require => false |
| 16 | gem 'rest-client', '~> 1.6.7' | 16 | gem 'rest-client', '~> 1.6.7' |
config.ru
| 1 | # This file is used by Rack-based servers to start the application. | 1 | # This file is used by Rack-based servers to start the application. |
| 2 | 2 | ||
| 3 | require ::File.expand_path('../config/environment', __FILE__) | 3 | require ::File.expand_path('../config/environment', __FILE__) |
| 4 | -run Noosfero::Application | 4 | +if ENV['RAILS_RELATIVE_URL_ROOT'] |
| 5 | + map ENV['RAILS_RELATIVE_URL_ROOT'] do | ||
| 6 | + run Noosfero::Application | ||
| 7 | + end | ||
| 8 | +else | ||
| 9 | + run Noosfero::Application | ||
| 10 | +end |
config/thin.yml.dist
gitignore.example
| @@ -10,7 +10,7 @@ config/session.secret | @@ -10,7 +10,7 @@ config/session.secret | ||
| 10 | config/noosfero.yml | 10 | config/noosfero.yml |
| 11 | config/mongrel_cluster.yml | 11 | config/mongrel_cluster.yml |
| 12 | config/plugins | 12 | config/plugins |
| 13 | -config/thin.yml | 13 | +config/unicorn.rb |
| 14 | config/local.rb | 14 | config/local.rb |
| 15 | index | 15 | index |
| 16 | locale | 16 | locale |
| @@ -0,0 +1,20 @@ | @@ -0,0 +1,20 @@ | ||
| 1 | +# our defaults | ||
| 2 | +listen "0.0.0.0:3000" | ||
| 3 | +pid 'tmp/pids/unicorn.pid' | ||
| 4 | + | ||
| 5 | +preload_app true | ||
| 6 | +GC.respond_to?(:copy_on_write_friendly=) and | ||
| 7 | + GC.copy_on_write_friendly = true | ||
| 8 | + | ||
| 9 | +before_fork do |server, worker| | ||
| 10 | + ActiveRecord::Base.connection.disconnect! if defined?(ActiveRecord::Base) | ||
| 11 | +end | ||
| 12 | + | ||
| 13 | +after_fork do |server, worker| | ||
| 14 | + ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base) | ||
| 15 | +end | ||
| 16 | + | ||
| 17 | +# load local configuration file, if it exists | ||
| 18 | +config = File.join(File.dirname(__FILE__), '../../config/unicorn.rb') | ||
| 19 | +instance_eval(File.read(config), config) if File.exists?(config) | ||
| 20 | + |
script/development
| @@ -9,9 +9,9 @@ stop() { | @@ -9,9 +9,9 @@ stop() { | ||
| 9 | ./script/delayed_job stop | 9 | ./script/delayed_job stop |
| 10 | ./script/feed-updater stop | 10 | ./script/feed-updater stop |
| 11 | whenever --clear-crontab | 11 | whenever --clear-crontab |
| 12 | - if [ -f tmp/pids/thin.pid ]; then | ||
| 13 | - kill -9 $(cat tmp/pids/thin.pid) | ||
| 14 | - rm -f tmp/pids/thin.pid | 12 | + if [ -f tmp/pids/unicorn.pid ]; then |
| 13 | + kill -9 $(cat tmp/pids/unicorn.pid) | ||
| 14 | + rm -f tmp/pids/unicorn.pid | ||
| 15 | fi | 15 | fi |
| 16 | exit | 16 | exit |
| 17 | } | 17 | } |
| @@ -20,18 +20,17 @@ start() { | @@ -20,18 +20,17 @@ start() { | ||
| 20 | rake db:abort_if_pending_migrations | 20 | rake db:abort_if_pending_migrations |
| 21 | ./script/feed-updater start | 21 | ./script/feed-updater start |
| 22 | ./script/delayed_job start | 22 | ./script/delayed_job start |
| 23 | - trap stop INT TERM | 23 | + trap stop INT TERM EXIT |
| 24 | whenever --write-crontab --set 'environment=development' | 24 | whenever --write-crontab --set 'environment=development' |
| 25 | if [ -z "$RAILS_RELATIVE_URL_ROOT" ]; then | 25 | if [ -z "$RAILS_RELATIVE_URL_ROOT" ]; then |
| 26 | - rails s $@ | 26 | + unicorn_rails --config-file lib/noosfero/unicorn.rb $@ |
| 27 | else | 27 | else |
| 28 | mkdir -p log | 28 | mkdir -p log |
| 29 | touch log/development.log | 29 | touch log/development.log |
| 30 | - thin \ | ||
| 31 | - --prefix "$RAILS_RELATIVE_URL_ROOT" \ | ||
| 32 | - --pid tmp/pids/thin.pid \ | ||
| 33 | - --daemonize \ | ||
| 34 | - start | 30 | + unicorn_rails \ |
| 31 | + --path "$RAILS_RELATIVE_URL_ROOT" \ | ||
| 32 | + --config-file lib/noosfero/unicorn.rb \ | ||
| 33 | + --daemonize | ||
| 35 | tail -n 0 -f log/development.log || true | 34 | tail -n 0 -f log/development.log || true |
| 36 | fi | 35 | fi |
| 37 | } | 36 | } |
script/production
| @@ -25,12 +25,31 @@ clear_cache() { | @@ -25,12 +25,31 @@ clear_cache() { | ||
| 25 | fi | 25 | fi |
| 26 | } | 26 | } |
| 27 | 27 | ||
| 28 | +app_server_start() { | ||
| 29 | + ruby -S bundle exec unicorn_rails \ | ||
| 30 | + --config-file lib/noosfero/unicorn.rb \ | ||
| 31 | + --env "$RAILS_ENV" \ | ||
| 32 | + --daemonize | ||
| 33 | +} | ||
| 34 | + | ||
| 35 | +app_server_stop() { | ||
| 36 | + # see unicorn_rails(1) | ||
| 37 | + kill -s QUIT $(cat tmp/pids/unicorn.pid) | ||
| 38 | +} | ||
| 39 | + | ||
| 40 | +app_server_restart() { | ||
| 41 | + # see unicorn_rails(1) and "Signal handling" in unicorn documentation | ||
| 42 | + kill -s USR2 $(cat tmp/pids/unicorn.pid) | ||
| 43 | + sleep 5 | ||
| 44 | + kill -s QUIT $(cat tmp/pids/unicorn.pid.oldbin) | ||
| 45 | +} | ||
| 46 | + | ||
| 28 | do_start() { | 47 | do_start() { |
| 29 | bundle exec rake db:migrate SCHEMA=/dev/null | 48 | bundle exec rake db:migrate SCHEMA=/dev/null |
| 30 | clear_cache | 49 | clear_cache |
| 31 | environments_loop start | 50 | environments_loop start |
| 32 | bundle exec whenever --write-crontab --set 'environment=production' | 51 | bundle exec whenever --write-crontab --set 'environment=production' |
| 33 | - ruby -S bundle exec thin -C config/thin.yml start | 52 | + app_server_start |
| 34 | } | 53 | } |
| 35 | 54 | ||
| 36 | do_stop() { | 55 | do_stop() { |
| @@ -39,8 +58,8 @@ do_stop() { | @@ -39,8 +58,8 @@ do_stop() { | ||
| 39 | # back to stopping the daemons by manually reading their PID files, killing | 58 | # back to stopping the daemons by manually reading their PID files, killing |
| 40 | # them and wiping the PID files. | 59 | # them and wiping the PID files. |
| 41 | 60 | ||
| 42 | - ruby -S bundle exec thin -C config/thin.yml stop || | ||
| 43 | - stop_via_pid_file tmp/pids/thin.*.pid | 61 | + app_server_stop || |
| 62 | + stop_via_pid_file tmp/pids/unicorn.pid | ||
| 44 | 63 | ||
| 45 | environments_loop stop || | 64 | environments_loop stop || |
| 46 | stop_via_pid_file tmp/pids/delayed_job.pid tmp/pids/delayed_job.*.pid tmp/pids/feed-updater.*.pid | 65 | stop_via_pid_file tmp/pids/delayed_job.pid tmp/pids/delayed_job.*.pid tmp/pids/feed-updater.*.pid |
| @@ -52,10 +71,11 @@ do_restart() { | @@ -52,10 +71,11 @@ do_restart() { | ||
| 52 | bundle exec rake db:migrate SCHEMA=/dev/null | 71 | bundle exec rake db:migrate SCHEMA=/dev/null |
| 53 | environments_loop stop || | 72 | environments_loop stop || |
| 54 | stop_via_pid_file tmp/pids/delayed_job.pid tmp/pids/delayed_job.*.pid tmp/pids/feed-updater.*.pid | 73 | stop_via_pid_file tmp/pids/delayed_job.pid tmp/pids/delayed_job.*.pid tmp/pids/feed-updater.*.pid |
| 55 | - environments_loop start | ||
| 56 | 74 | ||
| 57 | clear_cache | 75 | clear_cache |
| 58 | - ruby -S bundle exec thin -C config/thin.yml restart --onebyone | 76 | + app_server_restart |
| 77 | + | ||
| 78 | + environments_loop start | ||
| 59 | } | 79 | } |
| 60 | 80 | ||
| 61 | stop_via_pid_file() { | 81 | stop_via_pid_file() { |
| @@ -85,7 +105,7 @@ environments_loop() { | @@ -85,7 +105,7 @@ environments_loop() { | ||
| 85 | } | 105 | } |
| 86 | 106 | ||
| 87 | do_running() { | 107 | do_running() { |
| 88 | - pids=$(sed "s/.*/& /" tmp/pids/thin.*.pid 2>/dev/null | tr -d '\n') | 108 | + pids=$(sed "s/.*/& /" tmp/pids/unicorn.pid 2>/dev/null | tr -d '\n') |
| 89 | # passes if any of $pids exist, fails otherwise | 109 | # passes if any of $pids exist, fails otherwise |
| 90 | kill -0 $pids > /dev/null 2>&1 | 110 | kill -0 $pids > /dev/null 2>&1 |
| 91 | } | 111 | } |
| @@ -111,6 +131,8 @@ case "$ACTION" in | @@ -111,6 +131,8 @@ case "$ACTION" in | ||
| 111 | ;; | 131 | ;; |
| 112 | 132 | ||
| 113 | *) | 133 | *) |
| 134 | + # `running` is not listed on purpose since it's not supposed to be a public | ||
| 135 | + # API. | ||
| 114 | echo "usage: $0 start|stop|restart|run" | 136 | echo "usage: $0 start|stop|restart|run" |
| 115 | exit 1 | 137 | exit 1 |
| 116 | ;; | 138 | ;; |