From eb3d2e2cb368b8953b0f85e20d727701aa536229 Mon Sep 17 00:00:00 2001 From: Antonio Terceiro Date: Wed, 9 Sep 2015 17:20:58 -0300 Subject: [PATCH] core: switch to unicorn --- Gemfile | 2 +- config.ru | 8 +++++++- config/thin.yml.dist | 13 ------------- config/unicorn.rb.dist | 5 +++++ gitignore.example | 2 +- lib/noosfero/unicorn.rb | 20 ++++++++++++++++++++ script/development | 19 +++++++++---------- script/production | 34 ++++++++++++++++++++++++++++------ 8 files changed, 71 insertions(+), 32 deletions(-) delete mode 100644 config/thin.yml.dist create mode 100644 config/unicorn.rb.dist create mode 100644 lib/noosfero/unicorn.rb diff --git a/Gemfile b/Gemfile index 9df5fe6..eb6ef5b 100644 --- a/Gemfile +++ b/Gemfile @@ -10,7 +10,7 @@ gem 'RedCloth', '~> 4.2.9' gem 'will_paginate', '~> 3.0.3' gem 'ruby-feedparser', '~> 0.7' gem 'daemons', '~> 1.1.5' -gem 'thin', '~> 1.3.1' +gem 'unicorn', '~> 4.8' gem 'nokogiri', '~> 1.5.5' gem 'rake', :require => false gem 'rest-client', '~> 1.6.7' diff --git a/config.ru b/config.ru index 7cfb73b..62ec6b3 100644 --- a/config.ru +++ b/config.ru @@ -1,4 +1,10 @@ # This file is used by Rack-based servers to start the application. require ::File.expand_path('../config/environment', __FILE__) -run Noosfero::Application +if ENV['RAILS_RELATIVE_URL_ROOT'] + map ENV['RAILS_RELATIVE_URL_ROOT'] do + run Noosfero::Application + end +else + run Noosfero::Application +end diff --git a/config/thin.yml.dist b/config/thin.yml.dist deleted file mode 100644 index 6c3a75d..0000000 --- a/config/thin.yml.dist +++ /dev/null @@ -1,13 +0,0 @@ ---- -pid: tmp/pids/thin.pid -address: 127.0.0.1 -user: noosfero -timeout: 30 -port: 50000 -log: log/thin.log -max_conns: 1024 -servers: 1 -max_persistent_conns: 512 -environment: production -daemonize: true -chdir: /usr/share/noosfero diff --git a/config/unicorn.rb.dist b/config/unicorn.rb.dist new file mode 100644 index 0000000..111693b --- /dev/null +++ b/config/unicorn.rb.dist @@ -0,0 +1,5 @@ +listen "127.0.0.1:3000" + +worker_processes 1 + +# vim: ft=ruby diff --git a/gitignore.example b/gitignore.example index 8dfe08e..abbc244 100644 --- a/gitignore.example +++ b/gitignore.example @@ -10,7 +10,7 @@ config/session.secret config/noosfero.yml config/mongrel_cluster.yml config/plugins -config/thin.yml +config/unicorn.rb config/local.rb index locale diff --git a/lib/noosfero/unicorn.rb b/lib/noosfero/unicorn.rb new file mode 100644 index 0000000..045340a --- /dev/null +++ b/lib/noosfero/unicorn.rb @@ -0,0 +1,20 @@ +# our defaults +listen "0.0.0.0:3000" +pid 'tmp/pids/unicorn.pid' + +preload_app true +GC.respond_to?(:copy_on_write_friendly=) and + GC.copy_on_write_friendly = true + +before_fork do |server, worker| + ActiveRecord::Base.connection.disconnect! if defined?(ActiveRecord::Base) +end + +after_fork do |server, worker| + ActiveRecord::Base.establish_connection if defined?(ActiveRecord::Base) +end + +# load local configuration file, if it exists +config = File.join(File.dirname(__FILE__), '../../config/unicorn.rb') +instance_eval(File.read(config), config) if File.exists?(config) + diff --git a/script/development b/script/development index e6125f8..de6f05e 100755 --- a/script/development +++ b/script/development @@ -9,9 +9,9 @@ stop() { ./script/delayed_job stop ./script/feed-updater stop whenever --clear-crontab - if [ -f tmp/pids/thin.pid ]; then - kill -9 $(cat tmp/pids/thin.pid) - rm -f tmp/pids/thin.pid + if [ -f tmp/pids/unicorn.pid ]; then + kill -9 $(cat tmp/pids/unicorn.pid) + rm -f tmp/pids/unicorn.pid fi exit } @@ -20,18 +20,17 @@ start() { rake db:abort_if_pending_migrations ./script/feed-updater start ./script/delayed_job start - trap stop INT TERM + trap stop INT TERM EXIT whenever --write-crontab --set 'environment=development' if [ -z "$RAILS_RELATIVE_URL_ROOT" ]; then - rails s $@ + unicorn_rails --config-file lib/noosfero/unicorn.rb $@ else mkdir -p log touch log/development.log - thin \ - --prefix "$RAILS_RELATIVE_URL_ROOT" \ - --pid tmp/pids/thin.pid \ - --daemonize \ - start + unicorn_rails \ + --path "$RAILS_RELATIVE_URL_ROOT" \ + --config-file lib/noosfero/unicorn.rb \ + --daemonize tail -n 0 -f log/development.log || true fi } diff --git a/script/production b/script/production index 3d706e3..d41e01c 100755 --- a/script/production +++ b/script/production @@ -25,12 +25,31 @@ clear_cache() { fi } +app_server_start() { + ruby -S bundle exec unicorn_rails \ + --config-file lib/noosfero/unicorn.rb \ + --env "$RAILS_ENV" \ + --daemonize +} + +app_server_stop() { + # see unicorn_rails(1) + kill -s QUIT $(cat tmp/pids/unicorn.pid) +} + +app_server_restart() { + # see unicorn_rails(1) and "Signal handling" in unicorn documentation + kill -s USR2 $(cat tmp/pids/unicorn.pid) + sleep 5 + kill -s QUIT $(cat tmp/pids/unicorn.pid.oldbin) +} + do_start() { bundle exec rake db:migrate SCHEMA=/dev/null clear_cache environments_loop start bundle exec whenever --write-crontab --set 'environment=production' - ruby -S bundle exec thin -C config/thin.yml start + app_server_start } do_stop() { @@ -39,8 +58,8 @@ do_stop() { # back to stopping the daemons by manually reading their PID files, killing # them and wiping the PID files. - ruby -S bundle exec thin -C config/thin.yml stop || - stop_via_pid_file tmp/pids/thin.*.pid + app_server_stop || + stop_via_pid_file tmp/pids/unicorn.pid environments_loop stop || 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() { bundle exec rake db:migrate SCHEMA=/dev/null environments_loop stop || stop_via_pid_file tmp/pids/delayed_job.pid tmp/pids/delayed_job.*.pid tmp/pids/feed-updater.*.pid - environments_loop start clear_cache - ruby -S bundle exec thin -C config/thin.yml restart --onebyone + app_server_restart + + environments_loop start } stop_via_pid_file() { @@ -85,7 +105,7 @@ environments_loop() { } do_running() { - pids=$(sed "s/.*/& /" tmp/pids/thin.*.pid 2>/dev/null | tr -d '\n') + pids=$(sed "s/.*/& /" tmp/pids/unicorn.pid 2>/dev/null | tr -d '\n') # passes if any of $pids exist, fails otherwise kill -0 $pids > /dev/null 2>&1 } @@ -111,6 +131,8 @@ case "$ACTION" in ;; *) + # `running` is not listed on purpose since it's not supposed to be a public + # API. echo "usage: $0 start|stop|restart|run" exit 1 ;; -- libgit2 0.21.2