独角兽不使用默认的红宝石版本

时间:2018-09-27 13:52:02

标签: ruby-on-rails ruby rubygems unicorn

我有一个使用独角兽aws上的nginx的Rails应用。 我刚刚将ruby版本从2.0更新到了2.2.1。

ruby -v返回

  

红宝石2.2.1p85(2015-02-26修订版49769)[x86_64-linux]

我试图重新启动独角兽服务

sudo service unicorn_game-mercenaries_production stop
sudo service unicorn_game-mercenaries_production start

结果是

  

/home/gamemercs/.rvm/gems/ruby-2.2.1@game-mercenaries/gems/bundler-1.10.6/lib/bundler/definition.rb:412:in   validate_ruby!': Your Ruby version is 2.0.0, but your Gemfile specified 2.2.1 (Bundler::RubyVersionMismatch) from /home/gamemercs/.rvm/gems/ruby-2.2.1@game-mercenaries/gems/bundler-1.10.6/lib/bundler.rb:123:in 设置”来自   /home/gamemercs/.rvm/gems/ruby-2.2.1@game-mercenaries/gems/bundler-1.10.6/lib/bundler/setup.rb:18:in <top (required)>' from /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in require'from   /usr/share/ruby/vendor_ruby/2.0/rubygems/core_ext/kernel_require.rb:55:in   `require'

这是我的unicorn.rb:

working_directory "/home/gamemercs/rails_apps/game-mercenaries/current"
pid "/home/gamemercs/rails_apps/game-mercenaries/shared/tmp/pids/unicorn.pid"
stdout_path "/home/gamemercs/rails_apps/game-mercenaries/shared/log/unicorn.stdout.log"
stderr_path "/home/gamemercs/rails_apps/game-mercenaries/shared/log/unicorn.stderr.log"


listen "/tmp/unicorn.game-mercenaries_production.sock"


worker_processes 2
timeout 30

preload_app true

before_exec do |server|
  ENV["BUNDLE_GEMFILE"] = "/home/gamemercs/rails_apps/game-mercenaries/current/Gemfile"
end

before_fork do |server, worker|
  # Disconnect since the database connection will not carry over
  if defined? ActiveRecord::Base
    ActiveRecord::Base.connection.disconnect!
  end

  # Quit the old unicorn process
  old_pid = "#{server.config[: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
      # someone else did our job for us
    end
  end

  if defined?(Resque)
    Resque.redis.quit
  end
  p 'stop'

  sleep 1
end

after_fork do |server, worker|
  # Start up the database connection again in the worker
  if defined?(ActiveRecord::Base)
    ActiveRecord::Base.establish_connection
  end

  p 'start'
  if defined?(Resque)
    Resque.redis = 'localhost:6379'
  end
end

/etc/init.d/unicorn_game-mercenaries_production

#!/bin/bash
### BEGIN INIT INFO
# Provides: unicorn
# Required-Start: $remote_fs $syslog
# Required-Stop: $remote_fs $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Manage unicorn server
# Description: Start, stop, restart unicorn server for a specific application.
### END INIT INFO
set -e

# Feel free to change any of the following variables for your app:
TIMEOUT=${TIMEOUT-60}
APP_ROOT=/home/gamemercs/rails_apps/game-mercenaries/current
PID=/home/gamemercs/rails_apps/game-mercenaries/shared/tmp/pids/unicorn.pid

AS_USER=gamemercs
CMD="export HOME; true "${HOME:=$(getent passwd "$AS_USER" | cut -d: -f6;)}" ; cd $APP_ROOT && ~/.rvm/bin/rvm 2.2.1@game-mercenaries do bundle exec unicorn -D -c /home/gamemercs/rails_apps/game-mercenaries/shared/config/unicorn.rb -E production"

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 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

我将不胜感激。 如何让独角兽使用默认的ruby(v2.2.1)?

1 个答案:

答案 0 :(得分:0)

能否请您在/etc/init.d/unicorn中编辑GEM_HOME和GEM_PATH以反映您的新红宝石版本路径/编号?