繁体   English   中英

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

[英]Capistrano/nginx/Unicorn: Website is randomly up and down

我正在使用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

我正在尝试查看独角兽日志文件,但是与此问题没有任何关系。 production.log相同,无关紧要。 当我寻找一个nginx日志文件时,我在这里找到: /var/log/nginx/error.log ,但是文件为空(大小为0 )。

有什么建议可以解决这个问题/错在哪里或什至从哪里开始?

编辑:/home/deployer/apps/rentalhistory/shared/pids目录中,有以下两个文件: unicorn.pid.oldbinunicorn.pid不应该是已删除的文件之一吗?

谢谢!

我的问题是什么-当重新加载Unicorn时,生成了新的unicorn PID( unicorn.pid )- unicorn.pid ,但是旧的PID( unicorn.pid.oldbin )仍然保留在系统中。 显然这导致了整个网站的随机关闭。

FIX:我检查了我的独角兽配置文件​​( config/production.rb ),此行是错误的-路径:

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

shared目录与current处于同一级别,而不位于其内部。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM