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

时间:2017-07-28 06:34:18

标签: ruby-on-rails ruby nginx capistrano unicorn

我正在使用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&amp;&amp; echo重新加载OK&amp;&amp;退出0

这个:

  USP2&amp;&amp; echo重新加载OK&amp;&amp;退出0

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

有关此问题/错误或甚至开始的建议有什么建议吗?

/home/deployer/apps/rentalhistory/shared/pids目录中的

编辑:,有以下两个文件: unicorn.pid.oldbinunicorn.pid - 不应该被移除?

谢谢!

1 个答案:

答案 0 :(得分:0)

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

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

  

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

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

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