Commit eb3d2e2cb368b8953b0f85e20d727701aa536229

Authored by Antonio Terceiro
1 parent 8dfded9f

core: switch to unicorn

Gemfile
... ... @@ -10,7 +10,7 @@ gem 'RedCloth', '~> 4.2.9'
10 10 gem 'will_paginate', '~> 3.0.3'
11 11 gem 'ruby-feedparser', '~> 0.7'
12 12 gem 'daemons', '~> 1.1.5'
13   -gem 'thin', '~> 1.3.1'
  13 +gem 'unicorn', '~> 4.8'
14 14 gem 'nokogiri', '~> 1.5.5'
15 15 gem 'rake', :require => false
16 16 gem 'rest-client', '~> 1.6.7'
... ...
config.ru
1 1 # This file is used by Rack-based servers to start the application.
2 2  
3 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
... ... @@ -1,13 +0,0 @@
1   ----
2   -pid: tmp/pids/thin.pid
3   -address: 127.0.0.1
4   -user: noosfero
5   -timeout: 30
6   -port: 50000
7   -log: log/thin.log
8   -max_conns: 1024
9   -servers: 1
10   -max_persistent_conns: 512
11   -environment: production
12   -daemonize: true
13   -chdir: /usr/share/noosfero
config/unicorn.rb.dist 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +listen "127.0.0.1:3000"
  2 +
  3 +worker_processes 1
  4 +
  5 +# vim: ft=ruby
... ...
gitignore.example
... ... @@ -10,7 +10,7 @@ config/session.secret
10 10 config/noosfero.yml
11 11 config/mongrel_cluster.yml
12 12 config/plugins
13   -config/thin.yml
  13 +config/unicorn.rb
14 14 config/local.rb
15 15 index
16 16 locale
... ...
lib/noosfero/unicorn.rb 0 → 100644
... ... @@ -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 ./script/delayed_job stop
10 10 ./script/feed-updater stop
11 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 15 fi
16 16 exit
17 17 }
... ... @@ -20,18 +20,17 @@ start() {
20 20 rake db:abort_if_pending_migrations
21 21 ./script/feed-updater start
22 22 ./script/delayed_job start
23   - trap stop INT TERM
  23 + trap stop INT TERM EXIT
24 24 whenever --write-crontab --set 'environment=development'
25 25 if [ -z "$RAILS_RELATIVE_URL_ROOT" ]; then
26   - rails s $@
  26 + unicorn_rails --config-file lib/noosfero/unicorn.rb $@
27 27 else
28 28 mkdir -p log
29 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 34 tail -n 0 -f log/development.log || true
36 35 fi
37 36 }
... ...
script/production
... ... @@ -25,12 +25,31 @@ clear_cache() {
25 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 47 do_start() {
29 48 bundle exec rake db:migrate SCHEMA=/dev/null
30 49 clear_cache
31 50 environments_loop start
32 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 55 do_stop() {
... ... @@ -39,8 +58,8 @@ do_stop() {
39 58 # back to stopping the daemons by manually reading their PID files, killing
40 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 64 environments_loop stop ||
46 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 71 bundle exec rake db:migrate SCHEMA=/dev/null
53 72 environments_loop stop ||
54 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 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 81 stop_via_pid_file() {
... ... @@ -85,7 +105,7 @@ environments_loop() {
85 105 }
86 106  
87 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 109 # passes if any of $pids exist, fails otherwise
90 110 kill -0 $pids > /dev/null 2>&1
91 111 }
... ... @@ -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 136 echo "usage: $0 start|stop|restart|run"
115 137 exit 1
116 138 ;;
... ...