Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Show comment
Hide comment

mjason commented Jan 17, 2013

is good

@fivetwentysix

This comment has been minimized.

Show comment
Hide comment
@fivetwentysix

fivetwentysix Mar 5, 2013

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.

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

This comment has been minimized.

Show comment
Hide comment
@jumph4x

jumph4x Sep 10, 2014

@PatrickMA any solution to that?

jumph4x commented Sep 10, 2014

@PatrickMA any solution to that?

@jerefrer

This comment has been minimized.

Show comment
Hide comment

@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

This comment has been minimized.

Show comment
Hide comment
@johnbayne

johnbayne Oct 21, 2014

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

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