[Git][noosfero/noosfero][master] 3 commits: core: switch to unicorn

Antonio Terceiro gitlab at gitlab.com
Fri Sep 11 14:01:59 BRT 2015


Antonio Terceiro pushed to branch master at Noosfero / noosfero


Commits:
eb3d2e2c by Antonio Terceiro at 2015-09-11T14:01:10Z
core: switch to unicorn

- - - - -
7f62c5bf by Antonio Terceiro at 2015-09-11T14:01:10Z
debian: switch to unicorn

- - - - -
89708241 by Antonio Terceiro at 2015-09-11T14:01:23Z
Merge branch 'unicorn'

- - - - -


21 changed files:

- Gemfile
- config.ru
- − config/thin.yml.dist
- + config/unicorn.rb.dist
- − debian/apache2/conf.d/noosfero-cluster.conf
- debian/apache2/virtualhost.conf
- debian/control
- debian/noosfero-apache.install
- debian/noosfero.install
- debian/noosfero.links
- debian/noosfero.postinst
- − debian/thin.yml
- + debian/unicorn.rb
- debian/update-noosfero-apache
- gitignore.example
- + lib/noosfero/unicorn.rb
- script/apacheconf
- script/development
- script/install-dependencies/debian-wheezy.sh
- script/production
- util/debian-install/test


Changes:

=====================================
Gemfile
=====================================
--- 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'


=====================================
config.ru
=====================================
--- 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


=====================================
config/thin.yml.dist deleted
=====================================
--- 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


=====================================
config/unicorn.rb.dist
=====================================
--- /dev/null
+++ b/config/unicorn.rb.dist
@@ -0,0 +1,5 @@
+listen "127.0.0.1:3000"
+
+worker_processes 1
+
+# vim: ft=ruby


=====================================
debian/apache2/conf.d/noosfero-cluster.conf deleted
=====================================
--- a/debian/apache2/conf.d/noosfero-cluster.conf
+++ /dev/null
@@ -1,8 +0,0 @@
-<Proxy balancer://noosfero>
-  Include /etc/noosfero/apache/cluster.conf
-  Order Allow,Deny
-  Allow from All
-</Proxy>
-
-# vim: ft=apache
-


=====================================
debian/apache2/virtualhost.conf
=====================================
--- a/debian/apache2/virtualhost.conf
+++ b/debian/apache2/virtualhost.conf
@@ -15,7 +15,11 @@ RewriteRule ^/$ /index.html [QSA]
 RewriteRule ^([^.]+)$ $1.html [QSA]
 
 RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_FILENAME} !-f
-RewriteRule ^.*$ balancer://noosfero%{REQUEST_URI} [P,QSA,L]
+RewriteRule ^.*$ http://127.0.0.1:50000%{REQUEST_URI} [P,QSA,L]
+<Proxy http://127.0.0.1:50000>
+  Order Allow,Deny
+  Allow from All
+</Proxy>
 
 ErrorDocument 503 /503.html
 


=====================================
debian/control
=====================================
--- a/debian/control
+++ b/debian/control
@@ -65,7 +65,7 @@ Depends: adduser,
          ruby-whenever,
          ruby-will-paginate (>= 2.3.12-1~),
          tango-icon-theme,
-         thin,
+         unicorn (>= 4.8),
          ${misc:Depends}
 Recommends: postgresql, postgresql-client
 Description: free web-based platform for social networks


=====================================
debian/noosfero-apache.install
=====================================
--- a/debian/noosfero-apache.install
+++ b/debian/noosfero-apache.install
@@ -1,3 +1,2 @@
-debian/apache2/conf.d/noosfero-cluster.conf     etc/apache2/conf.d
 debian/apache2/virtualhost.conf                 etc/noosfero/apache
 debian/update-noosfero-apache                   usr/sbin


=====================================
debian/noosfero.install
=====================================
--- a/debian/noosfero.install
+++ b/debian/noosfero.install
@@ -19,7 +19,7 @@ debian/noosfero-check-dbconfig    usr/sbin
 debian/noosfero-console           usr/sbin
 debian/noosfero-runner            usr/sbin
 debian/noosfero.yml               etc/noosfero
-debian/thin.yml                   etc/noosfero
+debian/unicorn.rb                 etc/noosfero
 doc                               usr/share/noosfero
 doc/noosfero                      usr/share/noosfero/doc
 etc/init.d/noosfero               etc/init.d


=====================================
debian/noosfero.links
=====================================
--- a/debian/noosfero.links
+++ b/debian/noosfero.links
@@ -1,7 +1,7 @@
 var/tmp/noosfero                                    usr/share/noosfero/tmp
 var/log/noosfero                                    usr/share/noosfero/log
 etc/noosfero/database.yml                           usr/share/noosfero/config/database.yml
-etc/noosfero/thin.yml                               usr/share/noosfero/config/thin.yml
+etc/noosfero/unicorn.rb                             usr/share/noosfero/config/unicorn.rb
 etc/noosfero/plugins                                usr/share/noosfero/config/plugins
 etc/noosfero/noosfero.yml                           usr/share/noosfero/config/noosfero.yml
 etc/noosfero/local.rb                               usr/share/noosfero/config/local.rb


=====================================
debian/noosfero.postinst
=====================================
--- a/debian/noosfero.postinst
+++ b/debian/noosfero.postinst
@@ -10,23 +10,23 @@ makedir() {
 }
 
 
-# migrate mongrel configuration to thin
-mongrel_config=/etc/noosfero/mongrel_cluster.yml
+# migrate thin configuration to unicorn
+unicorn_config=/etc/noosfero/unicorn.rb
 thin_config=/etc/noosfero/thin.yml
-mongrel_orig_sha1=602c642c03f79349969c08330112a6f29d4c32aa
-if [ -r $mongrel_config ]; then
-  mongrel_sha1=$(sha1sum $mongrel_config | awk '{print $1}')
-  if [ "$mongrel_sha1" != "$mongrel_orig_sha1" ]; then
-    port=$(awk '{ if ($1 == "port:") { print($2) } }' $mongrel_config)
-    servers=$(awk '{ if ($1 == "servers:") { print($2) } }' $mongrel_config)
+thin_orig_sha1=47cee6728a7896a13f4d66544086ab88b02e89a7
+if [ -r $thin_config ]; then
+  thin_sha1=$(sha1sum $thin_config | awk '{print $1}')
+  if [ "$thin_sha1" != "$thin_orig_sha1" ]; then
+    port=$(awk '{ if ($1 == "port:") { print($2) } }' $thin_config)
+    servers=$(awk '{ if ($1 == "servers:") { print($2) } }' $thin_config)
     if test -n "$port"  && test "$port" -ne 50000  || test -n "$servers" && test "$servers" -ne 1 ; then
-      # mongrel configuration was changed; update thin configuration
+      # thin configuration was changed; update unicorn configuration
       # accordingly
-      sed -i -e "s/port: .*/port: $port/" $thin_config
-      sed -i -e "s/servers: .*/servers: $servers/" $thin_config
+      sed -i -e "s/listen.*/listen '127.0.0.1:$port'/" $unicorn_config
+      sed -i -e "s/worker_processes.*/worker_processes $servers/" $unicorn_config
     fi
   fi
-  mv $mongrel_config $mongrel_config.bak
+  mv $thin_config $thin_config.bak
 fi
 
 # create user noosfero in a portable way, while creating the log directory.


=====================================
debian/thin.yml deleted
=====================================
--- a/debian/thin.yml
+++ /dev/null
@@ -1,13 +0,0 @@
---- 
-pid: tmp/pids/thin.pid
-address: 127.0.0.1
-user: noosfero
-timeout: 0
-port: 50000
-log: log/thin.log
-max_conns: 1024
-servers: 1
-max_persistent_conns: 512
-environment: production
-daemonize: true
-chdir: /usr/share/noosfero


=====================================
debian/unicorn.rb
=====================================
--- /dev/null
+++ b/debian/unicorn.rb
@@ -0,0 +1,3 @@
+listen "127.0.0.1:50000"
+
+worker_processes `nproc`.to_i


=====================================
debian/update-noosfero-apache
=====================================
--- a/debian/update-noosfero-apache
+++ b/debian/update-noosfero-apache
@@ -2,20 +2,9 @@
 
 set -e
 
-# automatically update configuration, but if package noosfero is also installed
+# automatically update configuration, but only if package noosfero is also
+# installed
 if test -x /usr/share/noosfero/script/apacheconf; then
-  datadir="/etc/noosfero/apache"
-  mongrel_file="$datadir/mongrel.conf"
-  cluster_file="$datadir/cluster.conf"
-  if test -e "$cluster_file"; then
-    echo "Overwriting $cluster_file ..."
-  fi
-  /usr/share/noosfero/script/apacheconf thin > "$cluster_file"
-  if test -e "$mongrel_file"; then
-    echo "Moving existing $mongrel_file away ..."
-    mv "$mongrel_file" "$mongrel_file".bak
-    (cd $datadir && ln -sf cluster.conf mongrel.conf)
-  fi
 
   apache_site='/etc/apache2/sites-available/noosfero'
   if ! test -e "$apache_site"; then


=====================================
gitignore.example
=====================================
--- 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


=====================================
lib/noosfero/unicorn.rb
=====================================
--- /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)
+


=====================================
script/apacheconf
=====================================
--- a/script/apacheconf
+++ b/script/apacheconf
@@ -1,21 +1,6 @@
 #!/usr/bin/env ruby
 
-def mongrel_thin(config_file)
-  require 'yaml'
-  config = YAML.load_file(config_file)
-  ip = config['address'] || '127.0.0.1'
-  servers = config['servers'] || 1
-  ports = port_range = config['port']..(config['port'] + servers - 1)
-  ports.each do |port|
-    puts "BalancerMember http://#{ip}:#{port}"
-  end
-end
-
 case ARGV.first
-when 'mongrel'
-  mongrel_thin(File.dirname(__FILE__) + "/../config/mongrel_cluster.yml")
-when 'thin'
-  mongrel_thin(File.dirname(__FILE__) + "/../config/thin.yml")
 when 'virtualhosts'
   require File.dirname(__FILE__) + '/../config/environment'
   Environment.all.each do |environment|
@@ -38,6 +23,6 @@ when 'virtualhosts'
   end
   puts "# vim: ft=apache"
 else
-  puts "usage: %s mongrel|thin|virtualhosts" % $PROGRAM_NAME
+  puts "usage: %s virtualhosts" % $PROGRAM_NAME
   exit 1
 end


=====================================
script/development
=====================================
--- 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
 }


=====================================
script/install-dependencies/debian-wheezy.sh
=====================================
--- a/script/install-dependencies/debian-wheezy.sh
+++ b/script/install-dependencies/debian-wheezy.sh
@@ -72,8 +72,8 @@ run sudo apt-get -qy dist-upgrade
 
 run sudo apt-get -y install dctrl-tools
 
-# *sigh* need ruby-rspec from backports
-run sudo apt-get -y install -t wheezy-backports ruby-rspec
+# need these from backports
+run sudo apt-get -y install -t wheezy-backports ruby-rspec unicorn
 
 # needed to run noosfero
 packages=$(grep-dctrl -n -s Build-Depends,Depends,Recommends -S -X noosfero debian/control | sed -e '/^\s*#/d; s/([^)]*)//g; s/,\s*/\n/g' | grep -v 'memcached\|debconf\|dbconfig-common\|misc:Depends\|adduser\|mail-transport-agent')


=====================================
script/production
=====================================
--- 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
     ;;


=====================================
util/debian-install/test
=====================================
--- a/util/debian-install/test
+++ b/util/debian-install/test
@@ -1,7 +1,7 @@
 #!/bin/sh
 
 test_web_backend_running() {
-  assertTrue 'Noosfero running' 'pgrep -u noosfero -f thin'
+  assertTrue 'Noosfero running' 'pgrep -u noosfero -f unicorn'
 }
 
 test_delayed_job_running() {



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/8dfded9fc2839934fb1a43de88c99e52e7594448...89708241bc4952d09f03c1483db9c1ba693b742e
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150911/af38a78f/attachment-0001.html>


More information about the Noosfero-dev mailing list