首页 文章

Capistrano / nginx / Unicorn:网站随机上下

提问于
浏览
0

我正在使用Capistrano将我的代码部署到DigitalOcean服务器 . 我在 deploy.rb 文件中添加了每次部署后重新启动Unicorn的块,但从那时起我就注意到当我到浏览器并开始刷新网站时,有时我会刷新网站,有时只刷新一页(白色) . 这是完全随机的 .

deploy.rb

# config valid only for current version of Capistrano
lock "3.8.1"

set :application, "project"
set :repo_url, "git@bitbucket.org:username/project.git"
set :branch, "master"
set :tmp_dir, '/home/deployer/tmp'

set :deploy_to, "/home/deployer/apps/project"
set :keep_releases, 5

set(:executable_config_files, %w(
  unicorn_init.sh
))

# files which need to be symlinked to other parts of the
# filesystem. For example nginx virtualhosts, log rotation
# init scripts etc.
set(:symlinks, [
  {
    source: "nginx.conf",
    link: "/etc/nginx/sites-enabled/default"
  },
  {
    source: "unicorn_init.sh",
    link: "/etc/init.d/unicorn_#{fetch(:application)}"
  },
  {
    source: "log_rotation",
   link: "/etc/logrotate.d/#{fetch(:application)}"
  },
  {
    source: "monit",
    link: "/etc/monit/conf.d/#{fetch(:application)}.conf"
  }
])


namespace :deploy do   
  desc 'Restart application'
  task :restart_unicorn do
    on roles(:app) do
      execute '/home/deployer/apps/project/current/config/unicorn_init.sh restart'
    end
  end
  after :publishing, :restart_unicorn

  desc "Make sure local git is in sync with remote."
  task :check_revision do
    on roles(:web) do
      unless `git rev-parse HEAD` == `git rev-parse origin/master`
        puts "WARNING: HEAD is not the same as origin/master"
        puts "Run `git push` to sync changes."
        exit
      end
    end
  end
  before "deploy", "deploy:check_revision"
end

unicorn_init.sh

set -e

TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/deployer/apps/project/current
#PID=$APP_ROOT/tmp/pids/unicorn.pid
PID=/home/deployer/apps/project/shared/pids/unicorn.pid
#CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn.rb -E production"
CMD="cd $APP_ROOT; bundle exec unicorn -D -c $APP_ROOT/config/unicorn/production.rb -E production"
AS_USER=deployer
set -u

OLD_PIN="$PID.oldbin"

sig () {
  test -s "$PID" && kill -$1 `cat $PID`
}

oldsig () {
  test -s $OLD_PIN && kill -$1 `cat $OLD_PIN`
}

run () {
  if [ "$(id -un)" = "$AS_USER" ]; then
    eval $1
  else
    su -c "$1" - $AS_USER
  fi
}

case "$1" in
start)
  sig 0 && echo >&2 "Already running" && exit 0
  run "$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
  sig USR2 && echo reloaded OK && exit 0
  echo >&2 "Couldn't reload, starting '$CMD' instead"
  run "$CMD"
  ;;
upgrade)
  if sig USR2 && sleep 2 && sig 0 && oldsig QUIT
  then
    n=$TIMEOUT
    while test -s $OLD_PIN && test $n -ge 0
    do
      printf '.' && sleep 1 && n=$(( $n - 1 ))
    done
    echo

    if test $n -lt 0 && test -s $OLD_PIN
    then
      echo >&2 "$OLD_PIN still exists after $TIMEOUT seconds"
      exit 1
    fi
    exit 0
  fi
  echo >&2 "Couldn't upgrade, starting '$CMD' instead"
  run "$CMD"
  ;;
reopen-logs)
  sig USR1
  ;;
*)
  echo >&2 "Usage: $0 <start|stop|restart|upgrade|force-stop|reopen-logs>"
  exit 1
  ;;
esac

我在这个文件中有什么变化 - 这一行:

sig HUP && echo重新加载OK &&退出0

对于这一个:

sig USR2 && echo重新加载OK &&退出0

我试图查看unicorn日志文件,但没有任何相关的问题 . production.log - 同样,没什么关系 . 当我在寻找一个nginx日志文件时,我在这里找到了: /var/log/nginx/error.log ,但是fiel是空的(大小为 0 ) .

有什么建议如何处理这个问题/什么是错的或甚至从哪里开始?

EDIT:/home/deployer/apps/rentalhistory/shared/pids 目录中,有这两个文件: unicorn.pid.oldbinunicorn.pid - 不应该删除其中一个?

谢谢!

1 回答

  • 0

    在我的情况下出现了什么问题 - 当重新加载Unicorn时,生成了新的独角兽PID( unicorn.pid ) - 这是正确的,但旧的( unicorn.pid.oldbin )仍然留在系统中 . 这显然导致了整个网站的随机性下降 .

    FIX:我检查了我的Unicorn配置文件( config/production.rb ),这行错了 - 路径:

    old_pid =“# /shared/pids/unicorn.pid.oldbin”

    shared 目录与 current 处于同一级别,而不在其中 .

    在此更改之后,将创建新的独角兽PID并正确删除旧的独角兽PID .

相关问题