Unicorn 是什么?
特性:
安装:
$ gem install unicorn
配置:
# 设定 GEM_HOME
GEM_HOME = "/Users/P.Luo/.rvm/gems/ruby-1.8.7-p340@project_name"
# 获取当前项目路径
require 'pathname'
path = Pathname.new(__FILE__).realpath # 当前文件完整路径
path = path.sub('/config/unicorn.rb', '')
APP_PATH = path.to_s
# 或直接填写
# APP_PATH = "/path_to_project/workspace/project_name"
# worker 数
worker_processes 20
# 项目目录,部署后的项目指向 current,如:/srv/project_name/current
working_directory APP_PATH
# we use a shorter backlog for quicker failover when busy
# 可同时监听 Unix 本地 socket 或 TCP 端口
listen "/tmp/project_name.sock", :backlog => 64
# 开启tcp 端口,可不使用 apache 或 nginx 做代理,直接本地:http://localhost:port
#listen 8080, :tcp_nopush => true
# 如果为 REE,则添加 copy_on_wirte_friendly
# http://www.rubyenterpriseedition.com/faq.html#adapt_apps_for_cow
if GC.respond_to?(:copy_on_write_friendly=)
GC.copy_on_write_friendly = true
end
# request 超时时间,超过此时间则自动将此请求杀掉,单位为秒
timeout 180
# unicorn master pid
# unicorn pid 存放路径
pid APP_PATH + "/tmp/pids/unicorn.pid"
# unicorn 日志
stderr_path APP_PATH + "/log/unicorn.stderr.log"
stdout_path APP_PATH + "/log/unicorn.stdout.log"
preload_app true
before_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.connection.disconnect!
end
after_fork do |server, worker|
defined?(ActiveRecord::Base) and
ActiveRecord::Base.establish_connection
end
使用:
参数说明:
例:
$ unicorn_rails -E production -D -c /srv/project_name/current/config/unicorn.rb
重启: 方法一:
$ ps auwx | grep unicorn
获取:master 的 pid 如
$ kill -9 17357
$ unicorn_rails -E production -D -c /srv/project_name/current/config/unicorn.rb
方法二: 在/etc/init.d/ 目录下添加 init 脚本:
$ sudo /etc/init.d/unicorn.project_name
Usage: /etc/init.d/unicorn.project_name <start|stop|restart|upgrade|force-stop|reopen-logs>
可以运行:sudo /etc/init.d/unicorn.project_name 来进行项目重启、停止、启动...
init 脚本示例(请更改 APP_ROOT - 项目目录 和 APP_USER - 项目所属用户 的值):
#!/bin/sh
set -e
# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/srv/project_name/current
APP_USER=username
PID=$APP_ROOT/tmp/pids/unicorn.pid
ENV=production
CMD="bundle exec unicorn_rails -E $ENV -D -c $APP_ROOT/config/unicorn.rb"
action="$1"
set -u
old_pid="$PID.oldbin"
cd $APP_ROOT || exit 1
sig (){
test -s "$PID" && kill -$1 `cat $PID`
}
oldsig (){
test -s $old_pid && kill -$1 `cat $old_pid`
}
case $action in
start)
sig 0 && echo >&2 "Already running" && exit 0
su $APP_USER -c "$CMD"
;;
stop)
sig QUIT && exit 0
echo >&2 "Not running"
;;
force-stop)
sig TERM && exit 0
echo >&2 "Not running"
;;
restart|reload)
sig HUP && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
su $APP_USER -c "$CMD"
;;
upgrade)
if sig USR2 && sleep2 && sig 0 && oldsig QUIT
then
n=$TIMEOUT
while test -s $old_pid && test $n -ge 0
do
printf '.' && sleep 1 && n=$(($n - 1))
done
echo
if test $n -lt 0 && test -s $old_pid
then
echo >&2 "$old_pid still exists after $TIMEOUT seconds"
exit1
fi
exit0
fi
echo >&2 "Couldn't upgrade, starting '$CMD' instead"
su $APP_USER -c "$CMD"
;;
reopen-logs)
sig USR1
;;*)
echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
exit 1
;;
esac
参考:
说明: