Skip to content

Instantly share code, notes, and snippets.

@shmatov
Created November 15, 2012 13:17
Show Gist options
  • Save shmatov/4078612 to your computer and use it in GitHub Desktop.
Save shmatov/4078612 to your computer and use it in GitHub Desktop.
rails + mina + unicorn
# lib/tasks/deploy.rake
namespace :deploy do
desc 'Deploy to staging environment'
task :staging do
exec 'mina deploy -f config/deploy/staging.rb'
end
end
# config/deploy/staging.rb
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
require 'mina/rvm'
# Config
# ==============================================================================
set :term_mode, :system
set :rails_env, 'staging'
set :domain, '50.56.204.208'
set :port, 37894
set :deploy_to, "/home/apps/mfp/#{rails_env}"
set :app_path, "#{deploy_to}/#{current_path}"
set :repository, 'git@goup.unfuddle.com:goup/mfp.git'
set :brach, 'master'
set :user, 'apps'
set :shared_paths, ['public/static', 'tmp']
set :keep_releases, 5
# RVM
# ==============================================================================
set :rvm_path, '/usr/local/rvm/scripts/rvm'
task :environment do
invoke 'rvm:use[1.9.3]'
end
# Setup task
# ==============================================================================
task :setup do
queue! %{
mkdir -p "#{deploy_to}/shared/tmp/pids"
}
end
# Deploy task
# ==============================================================================
desc "deploys the current version to the server."
task :deploy => :environment do
deploy do
invoke 'git:clone'
invoke 'bundle:install'
invoke 'rails:db_migrate'
invoke 'rails:assets_precompile'
invoke 'deploy:link_shared_paths'
to :launch do
invoke :'unicorn:restart'
end
end
end
# Unicorn
# ==============================================================================
namespace :unicorn do
set :unicorn_pid, "#{app_path}/tmp/pids/unicorn.pid"
set :start_unicorn, %{
cd #{app_path}
bundle exec unicorn -c #{app_path}/config/unicorn/#{rails_env}.rb -E #{rails_env} -D
}
# Start task
# ------------------------------------------------------------------------------
desc "Start unicorn"
task :start => :environment do
queue 'echo "-----> Start Unicorn"'
queue! start_unicorn
end
# Stop task
# ------------------------------------------------------------------------------
desc "Stop unicorn"
task :stop do
queue 'echo "-----> Stop Unicorn"'
queue! %{
test -s "#{unicorn_pid}" && kill -QUIT `cat "#{unicorn_pid}"` && echo "Stop Ok" && exit 0
echo >&2 "Not running"
}
end
# Restart task
# ------------------------------------------------------------------------------
desc "Restart unicorn using 'upgrade'"
task :restart => :environment do
invoke 'unicorn:stop'
invoke 'unicorn:start'
end
end
# config/unicorn/staging.rb
app_path = "/home/apps/mfp/staging/current"
worker_processes 1
preload_app true
timeout 180
listen '127.0.0.1:9021'
user 'apps', 'apps'
working_directory app_path
pid "#{app_path}/tmp/pids/unicorn.pid"
stderr_path "log/unicorn.log"
stdout_path "log/unicorn.log"
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
@mjason
Copy link

mjason commented Jan 17, 2013

is good

@fivetwentysix
Copy link

It seems like the server doesn't stop instantly and the server tries to start before the server shuts down...

-----> Launching
-----> Stop Unicorn
Stop Ok
Not running
-----> Start Unicorn
master failed to start, check stderr log for details
! ERROR: Deploy failed.

Also I heard to achieve 100% uptime you should send signals instead of stopping and starting the unicorn instance.

@jumph4x
Copy link

jumph4x commented Sep 10, 2014

@PatrickMA any solution to that?

@jerefrer
Copy link

@jumph4x : I just used https://blog.nicolai86.eu/posts/2012-11-28/zero-downtime-deployments-with-unicorn-and-supervisord/ (Use unicornherder). Maybe things have improved since then ?

@johnbayne
Copy link

Nitpick: line 19, brach should be branch :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment