ruby on rails mina unicorn的启动(无缝重启)
一、配置unicorn
gem install ‘unicorn’ 或在rails项目中Gemfile添加gem ‘unicorn’,并在rails项目中创建文件config/unicorn.rb,并在里面添加:
pid_path = '/path/shared/tmp/pids/'
before_fork do |server, worker|
old_pid = "#{pid_path}unicorn.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
puts "Send 'QUIT' signal to unicorn error!"
end
end
end
二、mina配置
require 'mina/bundler'
require 'mina/rails'
require 'mina/git'
# require 'mina/rbenv' # for rbenv support. (http://rbenv.org)
# require 'mina/rvm' # for rvm support. (http://rvm.io)
require 'mina/rvm' # for rvm support. (http://rvm.io)
# Basic settings:
set :domain, '210.10.10.10'
set :user, 'username'
set :deploy_to, 'path'
set :repository, 'xxxxx.git'
set :branch, 'master'
# For system-wide RVM install.
# set :rvm_path, '/usr/local/rvm/bin/rvm'
# Manually create these paths in shared/ (eg: shared/config/database.yml) in your server.
# They will be linked in the 'deploy:link_shared_paths' step.
set :shared_paths, ['config/database.yml', 'config/secrets.yml', 'log']
task :environment do
invoke :'rvm:use[ruby-2.2.4@default]'
end
task :setup => :environment do
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/log"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/log"]
queue! %[mkdir -p "#{deploy_to}/#{shared_path}/config"]
queue! %[chmod g+rx,u+rwx "#{deploy_to}/#{shared_path}/config"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/database.yml"]
queue! %[touch "#{deploy_to}/#{shared_path}/config/secrets.yml"]
queue %[echo "-----> Be sure to edit '#{deploy_to}/#{shared_path}/config/database.yml' and 'secrets.yml'."]
if repository
repo_host = repository.split(%r{@|://}).last.split(%r{:|\/}).first
repo_port = /:([0-9]+)/.match(repository) && /:([0-9]+)/.match(repository)[1] || '22'
queue %[
if ! ssh-keygen -H -F #{repo_host} &>/dev/null; then
ssh-keyscan -t rsa -p #{repo_port} -H #{repo_host} >> ~/.ssh/known_hosts
fi
]
end
end
desc "Deploys the current version to the server."
task :deploy => :environment do
to :before_hook do
# Put things to run locally before ssh
end
deploy do
# Put things that will set up an empty directory into a fully set-up
# instance of your project.
invoke :'git:clone'
invoke :'deploy:link_shared_paths'
invoke :'bundle:install'
invoke :'rails:db_migrate'
invoke :'rails:assets_precompile'
invoke :'deploy:cleanup'
to :launch do
invoke :'unicorn:restart'
end
end
end
namespace :unicorn do
set :unicorn_pid, "#{deploy_to}/shared/tmp/pids/unicorn.pid"
set :start_unicorn, %{
cd #{deploy_to}/#{current_path} && bundle exec unicorn -D -E production -c config/unicorn.rb
}
# 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}"` && rm -rf "#{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
这样在使用mina deploy部署的时候就可以实现unicorn的无缝重启。
浙公网安备 33010602011771号