From 27f60060d55adfb3160cfcc2d1bc238214eec5cb Mon Sep 17 00:00:00 2001 From: r888888888 Date: Mon, 15 Apr 2013 21:58:00 -0700 Subject: [PATCH] add unicorn support --- Gemfile | 5 +++++ Gemfile.lock | 14 ++++++++++++++ config/deploy.rb | 10 ++++------ config/unicorn.rb | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 config/unicorn.rb diff --git a/Gemfile b/Gemfile index b93d6a5d1..09129d484 100644 --- a/Gemfile +++ b/Gemfile @@ -35,7 +35,12 @@ gem 'bcrypt-ruby', :require => "bcrypt" gem 'aws-s3', :require => "aws/s3" gem 'awesome_print' +group :production do + gem 'capistrano-unicorn', :require => false +end + group :development do gem 'ruby-prof' gem 'pry' end + diff --git a/Gemfile.lock b/Gemfile.lock index bc5565605..846f7d38f 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -63,6 +63,14 @@ GEM bourne (1.4.0) mocha (~> 0.13.2) builder (3.0.4) + capistrano (2.14.2) + highline + net-scp (>= 1.0.0) + net-sftp (>= 2.0.0) + net-ssh (>= 2.0.14) + net-ssh-gateway (>= 1.1.0) + capistrano-unicorn (0.1.7) + capistrano chronic (0.9.1) coderay (1.0.9) daemons (1.1.9) @@ -79,6 +87,7 @@ GEM multi_json (~> 1.0) factory_girl (4.2.0) activesupport (>= 3.0.0) + highline (1.6.18) hike (1.2.1) i18n (0.6.4) journey (1.0.4) @@ -97,9 +106,13 @@ GEM multi_json (1.7.2) net-http-digest_auth (1.3) net-http-persistent (2.8) + net-scp (1.1.0) + net-ssh (>= 2.6.5) net-sftp (2.1.1) net-ssh (>= 2.6.5) net-ssh (2.6.6) + net-ssh-gateway (1.2.0) + net-ssh (>= 2.6.5) newrelic_rpm (3.6.0.78) nokogiri (1.5.9) ntlm-http (0.1.1) @@ -191,6 +204,7 @@ DEPENDENCIES awesome_print aws-s3 bcrypt-ruby + capistrano-unicorn daemons delayed_job delayed_job_active_record diff --git a/config/deploy.rb b/config/deploy.rb index f6c08a519..956ad99f2 100644 --- a/config/deploy.rb +++ b/config/deploy.rb @@ -19,6 +19,8 @@ set :scm, :git set :user, "albert" set :deploy_to, "/var/www/danbooru2" +require 'capistrano-unicorn' + default_run_options[:pty] = true namespace :local_config do @@ -101,11 +103,6 @@ namespace :deploy do task :precompile_assets do run "cd #{current_path}; bundle exec rake assets:precompile" end - - desc "Restart the application" - task :restart do - run "touch #{current_path}/tmp/restart.txt" - end end namespace :delayed_job do @@ -145,7 +142,8 @@ after "deploy:stop", "delayed_job:stop" after "deploy:restart", "delayed_job:restart" before "deploy:update", "deploy:web:disable" after "deploy:update", "deploy:migrate" -after "deploy:update", "deploy:restart" +after "deploy:restart", "unicorn:reload" +after "deploy:restart", "unicorn:restart" after "deploy:restart", "deploy:precompile_assets" after "deploy:restart", "deploy:web:enable" after "delayed_job:stop", "delayed_job:kill" diff --git a/config/unicorn.rb b/config/unicorn.rb new file mode 100644 index 000000000..f009ee0ac --- /dev/null +++ b/config/unicorn.rb @@ -0,0 +1,46 @@ +# Set your full path to application. +app_path = "/var/www/danbooru2/current" + +# Set unicorn options +if Socket.gethostname =~ /sonohara|hijiribe/ + worker_processes 12 +else + worker_processes 1 +end + +preload_app false +timeout 180 +listen "127.0.0.1:9000" + +# Spawn unicorn master worker for user apps (group: apps) +user 'albert', 'albert' + +# Fill path to your app +working_directory app_path + +# Should be 'production' by default, otherwise use other env +rails_env = ENV['RAILS_ENV'] || 'production' + +# Log everything to one file +stderr_path "log/unicorn.log" +stdout_path "log/unicorn.log" + +# Set master PID location +pid "#{app_path}/tmp/pids/unicorn.pid" + +before_fork do |server, worker| + ActiveRecord::Base.connection.disconnect! + + old_pid = "#{server.config[:pid]}.oldbin" + if File.exists?(old_pid) && server.pid != old_pid + begin + Process.kill("QUIT", File.read(old_pid).to_i) + rescue Errno::ENOENT, Errno::ESRCH + # someone else did our job for us + end + end +end + +after_fork do |server, worker| + ActiveRecord::Base.establish_connection +end \ No newline at end of file