From f0ef4a764ecdbb329fbc0228da1229388243fbc1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonne=20Ha=C3=9F?= Date: Sun, 14 Oct 2012 23:49:04 +0200 Subject: [PATCH] refactor script/server and associated stuff --- .gitignore | 1 - Changelog.md | 14 +++- Gemfile | 8 +- Gemfile.lock | 7 -- Procfile | 2 +- config/defaults.yml | 8 +- config/diaspora.yml.example | 21 ++++- config/script_server.yml.example | 15 ---- config/unicorn.rb | 2 +- script/get_config.rb | 48 +++++------ script/server | 140 ++++++++++++++++--------------- 11 files changed, 132 insertions(+), 134 deletions(-) delete mode 100644 config/script_server.yml.example diff --git a/.gitignore b/.gitignore index b5a4be5428f..369e77eea85 100644 --- a/.gitignore +++ b/.gitignore @@ -6,7 +6,6 @@ app/assets/images/custom/* # Configuration files config/diaspora.yml config/heroku.yml -config/script_server.yml config/initializers/secret_token.rb config/redis.conf config/deploy_config.yml diff --git a/Changelog.md b/Changelog.md index 5541af70b73..ca5d295dbdc 100644 --- a/Changelog.md +++ b/Changelog.md @@ -2,9 +2,21 @@ ## Refactor +### script/server + +* Uses foreman now +* Reduce startup time by reducing calls to `script/get_config.rb` +* `config/script_server.yml` is removed and replaced by the `server` section in `config/diaspora.yml` + Have a look at the updated example! +* Thin is dropped in favour of unicorn +* Already set versions of `RAILS_ENV` and `DB` are now prefered over those set in `config/diaspora.yml` +* **Heroku setups:** `ENVIRONMENT_UNICORN_EMBED_RESQUE_WORKER` got renamed to `SERVER_EMBED_RESQUE_WORKER` + +### Other + * MessagesController. [#3657](https://github.com/diaspora/diaspora/pull/3657) -## Add Features +## Features * Add password_confirmation field to registration page. [#3647](https://github.com/diaspora/diaspora/pull/3647) diff --git a/Gemfile b/Gemfile index 7e8ff1b65bf..c37e2d61916 100644 --- a/Gemfile +++ b/Gemfile @@ -6,7 +6,8 @@ gem 'rails', '3.2.8' gem 'foreman', '0.59' -gem 'thin', '1.4.1', :require => false +gem 'unicorn', '4.3.1', :require => false + gem 'rails_autolink', '1.0.9' # cross-origin resource sharing @@ -43,11 +44,6 @@ group :production do # we don't install these on travis to speed up test runs end -# configuration - -group :heroku do - gem 'unicorn', '4.3.1', :require => false -end # database diff --git a/Gemfile.lock b/Gemfile.lock index 62c9a45d344..8b5617508b4 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -108,7 +108,6 @@ GEM capybara (>= 1.1.2) cucumber (>= 1.1.8) nokogiri (>= 1.5.0) - daemons (1.1.9) database_cleaner (0.8.0) debugger (1.2.0) columnize (>= 0.3.1) @@ -124,7 +123,6 @@ GEM warden (~> 1.2.1) diff-lcs (1.1.3) erubis (2.7.0) - eventmachine (0.12.10) excon (0.16.2) execjs (1.4.0) multi_json (~> 1.0) @@ -374,10 +372,6 @@ GEM rack (~> 1.0) tilt (~> 1.1, != 1.3.0) subexec (0.2.2) - thin (1.4.1) - daemons (>= 1.0.9) - eventmachine (>= 0.12.6) - rack (>= 1.0.0) thor (0.16.0) tilt (1.3.3) timecop (0.5.1) @@ -481,7 +475,6 @@ DEPENDENCIES sass-rails (= 3.2.5) selenium-webdriver (= 2.25.0) spork (= 1.0.0rc3) - thin (= 1.4.1) timecop (= 0.5.1) twitter (= 2.0.2) typhoeus (= 0.3.3) diff --git a/Procfile b/Procfile index 9fb9e3fbaef..a1ddfe5e0fb 100644 --- a/Procfile +++ b/Procfile @@ -1,2 +1,2 @@ web: bundle exec unicorn -c config/unicorn.rb -p $PORT -worker: env QUEUE=* bundle exec rake resque:work \ No newline at end of file +worker: env QUEUE=* bundle exec rake resque:work diff --git a/config/defaults.yml b/config/defaults.yml index 11bfeed620f..fffeeb1a9ea 100644 --- a/config/defaults.yml +++ b/config/defaults.yml @@ -25,8 +25,12 @@ defaults: upload: false host: pubsub_server: 'https://pubsubhubbub.appspot.com/' - unicorn: - embed_resque_worker: false + server: + port: 3000 + rails_environment: 'development' + db: 'mysql' + embed_resque_worker: false + resque_workers: 1 privacy: jquery_cdn: true google_analytics_key: diff --git a/config/diaspora.yml.example b/config/diaspora.yml.example index c34ffcb023b..0f71bfffa0a 100644 --- a/config/diaspora.yml.example +++ b/config/diaspora.yml.example @@ -100,10 +100,23 @@ configuration: ## Section ## You likely don't want to change this. #pubsub_server: 'https://pubsubhubbub.appspot.com/' - unicorn: ## Section - ## Embed a resque worker inside the unicorn process, useful for - ## minimal Heroku setups - #embed_resque_worker: true + server: ## Section + ## The port on which the appserver should listen + #port: 3000 + + ## The environment in which the server should be started by default. + #rails_environment: 'production' + + ## The database type the server should use by default. + ## Valid choices are 'mysql' and 'postgres' + #database: 'mysql' + + ## Embed a resque worker inside the unicorn process, useful for + ## minimal Heroku setups + #embed_resque_worker: true + + ## Number of resque workers to start + #resque_workers: 1 ## Settings probably affecting the privacy of your users privacy: ## Section diff --git a/config/script_server.yml.example b/config/script_server.yml.example deleted file mode 100644 index 07b3f1d5323..00000000000 --- a/config/script_server.yml.example +++ /dev/null @@ -1,15 +0,0 @@ -script_server: - # Choose database. Currently supported: mysql, postgres - db: "mysql" - - # Enable thin as application server - enable_thin: true - - # Port on which thin should listen - thin_port: 3000 - - # Customize thin's startup - default_thin_args: "-p $THIN_PORT -e $RAILS_ENV" - - # Possibilities are development, production - rails_env: "development" diff --git a/config/unicorn.rb b/config/unicorn.rb index 0f998c0287e..1f305dcf9e1 100644 --- a/config/unicorn.rb +++ b/config/unicorn.rb @@ -32,7 +32,7 @@ Resque.redis.client.disconnect end - if AppConfig.environment.unicorn.embed_resque_worker? + if AppConfig.server.embed_resque_worker? # Clean up Resque workers killed by previous deploys/restarts Resque.workers.each { |w| w.unregister_worker } @resque_pid ||= spawn('bundle exec rake resque:work QUEUES=*') diff --git a/script/get_config.rb b/script/get_config.rb index 51d5021e305..b9b6d6cdb35 100755 --- a/script/get_config.rb +++ b/script/get_config.rb @@ -3,9 +3,19 @@ # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. +unless ARGV.length >= 1 + $stderr.puts "Usage: ./script/get_config.rb var=option | option [...]" + Process.exit(1) +end + require 'rubygems' require 'pathname' +require 'active_support/core_ext/class/attribute_accessors' +require 'active_support/core_ext/object/blank' +require 'active_support/core_ext/module/delegation' +require 'active_support/core_ext/module/method_names' + class Rails def self.root @@root ||= Pathname.new(File.expand_path(File.join(File.dirname(__FILE__), ".."))) @@ -14,38 +24,20 @@ def self.root def self.env env = 'development' env = ENV['RAILS_ENV'] if ENV.has_key?('RAILS_ENV') - env = ARGV[1] if ARGV.length == 2 env.downcase end end +require Rails.root.join("config/load_config") -if ARGV.length >= 1 - setting_name = ARGV[0] - if Rails.env == 'script_server' # load from the special script_server_config.yml file - require 'yaml' - script_server_config_file = Rails.root.join('config', 'script_server.yml') - begin - print YAML.load_file(script_server_config_file)['script_server'][setting_name] - rescue - $stderr.puts "Setting '#{setting_name}' not found in file #{script_server_config_file}." - $stderr.puts "Does that file exist? If not, copy it from #{File.basename(script_server_config_file)}.example in the same directory and run this script again." - Process.exit(1) - end - else # load from the general diaspora settings file - require 'active_support/core_ext/class/attribute_accessors' - require 'active_support/core_ext/object/blank' - require 'active_support/core_ext/module/delegation' - require 'active_support/core_ext/module/method_names' - require Rails.root.join("config/load_config") - - setting = AppConfig.send(setting_name) - setting = setting.get if setting.is_a?(Configuration::Proxy) - print setting +ARGV.each do |arg| + var, setting_name = arg.split("=") + setting_name = var unless setting_name + setting = AppConfig.send(setting_name) + setting = setting.get if setting.is_a?(Configuration::Proxy) + if var != setting_name + puts "#{var}=#{setting}" + else + puts setting end -else - $stderr.puts "Usage: ./script/get_config.rb option [section]" - $stderr.puts "" - $stderr.puts "section defaults to development" - Process.exit(1) end diff --git a/script/server b/script/server index c961d7b699f..70e81dea91d 100755 --- a/script/server +++ b/script/server @@ -2,111 +2,115 @@ # Copyright (c) 2010-2011, Diaspora Inc. This file is # licensed under the Affero General Public License version 3 or later. See # the COPYRIGHT file. -# -# This script is meant for running a small server in production. It starts the Diaspora -# server, redis, and resque. To run a server locally for development, use foreman instead: -# foreman start -# # ensure right directory realpath=$( ruby -e "puts File.expand_path(\"$0\")") cd $(dirname $realpath)/.. -# Check if script_server.yml exists -if [ ! -e 'config/script_server.yml' ]; then - echo 'FATAL: config/script_server.yml is missing! Copy over config/script_server.yml.example to config/script_server.yml and edit it properly!' >&2 - exit 69 +#Warn if legacy config exists +if [ -e 'config/script_server.yml' ]; then + echo "WARNING: config/script_server.yml was merged into config/diaspora.yml. Please read the changelog!" >&2 fi - # Check if database.yml exists if [ ! -e 'config/database.yml' ]; then - echo 'FATAL: config/database.yml is missing! Copy over config/database.yml.example to config/database.yml and edit it properly!' >&2 - exit 68 + echo 'FATAL: config/database.yml is missing! Copy over config/database.yml.example to config/database.yml and edit it properly!' >&2 + exit 68 fi - -# Check if application.yml exists +# Check if diaspora.yml exists if [ ! -e 'config/diaspora.yml' ]; then - echo 'FATAL: config/diaspora.yml is missing! Copy over config/diaspora.yml.example to config/diaspora.yml and edit it properly!' >&2 - exit 70 + echo 'FATAL: config/diaspora.yml is missing! Copy over config/diaspora.yml.example to config/diaspora.yml and edit it properly!' >&2 + exit 70 fi - - # Setup environment -OS=`uname -s` -export RAILS_ENV=$(bundle exec ruby ./script/get_config.rb rails_env script_server) -export DB=$(bundle exec ruby ./script/get_config.rb db script_server) -THIN_PORT=$(bundle exec ruby ./script/get_config.rb thin_port script_server) -eval "DEFAULT_THIN_ARGS=\"$(bundle exec ruby ./script/get_config.rb default_thin_args script_server)\"" - -# Backward compatibillity, overide default settings -[ -e config/server.sh ] && source config/server.sh - +if [ -z "$RAILS_ENV" ]; then + export RAILS_ENV=$(bundle exec ruby ./script/get_config.rb server.rails_environment) +fi -function chk_service -{ - port=${1:?Missing port} - case $OS in - *[Bb][Ss][Dd]*|Darwin) - ## checks ipv[46] - netstat -anL | awk '{print $2}' | grep "\.$1$" - ;; - *) - # Is someone listening on the ports already? (ipv4 only test ?) - netstat -nl | grep '[^:]:'$port'[ \t]' - ;; - esac -} +os=`uname -s` +eval $(bundle exec ruby ./script/get_config.rb \ + port=server.port \ + db=server.db \ + workers=server.resque_workers \ + single_process_mode=environment.single_process_mode? + embed_resque_worker=server.embed_resque_worker +) + +if [ -z "$DB" ]; then + export DB=$db +fi +if [ "$single_process_mode" = "true" -o "$embed_resque_worker" = "true" ]; then + workers=0 +fi -# Scan for -p, find out what port thin is about to use. -args="$DEFAULT_THIN_ARGS $@" +args="$@" prev_arg='' for arg in $( echo $args | awk '{ for (i = 1; i <= NF; i++) print $i}') do - [ "$prev_arg" = '-p' ] && THIN_PORT="$arg" - prev_arg="$arg" + [ "$prev_arg" = '-p' ] && port="$arg" + prev_arg="$arg" done +# Check if already running/port blocked +function chk_service +{ + port=${1:?Missing port} + case $os in + *[Bb][Ss][Dd]*|Darwin) + ## checks ipv[46] + netstat -anL | awk '{print $2}' | grep "\.$1$" + ;; + *) + # Is someone listening on the ports already? (ipv4 only test ?) + netstat -nl | grep '[^:]:'$port'[ \t]' + ;; + esac +} + -# Is someone listening on the ports already? (ipv4 only test ?) -services=$( chk_service $THIN_PORT ) +services=$( chk_service $port ) if [ -n "$services" ]; then - echo "FATAL: Error: thin port $THIN_PORT is already in use. Exiting" >&2 - echo " $services" - exit 64 + echo "FATAL: Error: port $port is already in use. Exiting" >&2 + echo " $services" + exit 64 fi + # Force AGPL if [ -w public -a ! -e public/source.tar.gz ]; then - branch=$( git branch | awk '/^[*]/ {print $2}') - tar czf public/source.tar.gz `git ls-tree -r $branch | awk '{print $4}'` + branch=$( git branch | awk '/^[*]/ {print $2}') + tar czf public/source.tar.gz `git ls-tree -r $branch | awk '{print $4}'` fi + if [ ! -e public/source.tar.gz ]; then - echo "FATAL: Error: Can't find, or even create, public/source.tar.gz. Exiting" >&2 - exit 65 + echo "FATAL: Error: Can't find, or even create, public/source.tar.gz. Exiting" >&2 + exit 65 fi - -if [ ! -e 'public/assets/default.css' ]; then - if [ "$RAILS_ENV" == 'production' ]; then - echo "FATAL: You're running in production mode without having assets precompiled." >&2 - echo "Now and after each git pull before you restart the application, run:" >&2 - echo "bundle exec rake assets:precompile" >&2 - exit 71 - fi +# Check if assets are precompiled +if [ "$RAILS_ENV" == 'production' ]; then + if [ ! -e 'public/assets/default.css' ]; then + echo "FATAL: You're running in production mode without having assets precompiled." >&2 + echo "Now and after each update before you restart the application, run:" >&2 + echo "bundle exec rake assets:precompile" >&2 + exit 71 + fi fi # Start Diaspora - -if [ "$(bundle exec ruby ./script/get_config.rb 'environment.single_process_mode?')" != "true" ]; then - QUEUE=* bundle exec rake resque:work& +echo -n "Starting Diaspora in $RAILS_ENV mode on port $port " +if [ "$embed_resque_worker" = "true" ]; then + echo "with a resque worker embeded into unicorn." +elif [ "$single_process_mode" = "true" ]; then + echo "with job processing inside the request cycle." +else + echo "with $workers resque workers." fi +echo "" -if [ "$(./script/get_config.rb enable_thin script_server)" = "true" ]; then - bundle exec thin start $args -fi +bundle exec foreman start -c "web=1,worker=$workers" -p $port