【问题标题】:Rake tasks in capistrano and cron use wrong ruby versioncapistrano 和 cron 中的 Rake 任务使用错误的 ruby​​ 版本
【发布时间】:2020-01-15 08:46:53
【问题描述】:

我需要在部署过程中执行一个 rake 任务。为此,我在部署命名空间中有一个任务:

desc 'Start Telegram server'
task :start_telegram do
  on roles(:app) do
    puts "Current path: #{release_path}"
    within release_path do
      with rails_env: fetch(:rails_env) do
        execute :nohup, "bundle exec rake bot:start_poller > #{shared_path}/log/telegram.log 2>&1 & echo $! > #{shared_path}/tmp/pids/telegram-server.pid"
      end
    end
  end
end

它是这样执行的:after :stop_telegram, :start_telegram。日志文件的输出为:

/var/lib/gems/2.3.0/gems/bundler-2.0.2/lib/bundler/spec_set.rb:87:in `block in materialize': Could not find nokogiri-1.10.3 in any of the sources (Bundler::GemNotFound)
        from /var/lib/gems/2.3.0/gems/bundler-2.0.2/lib/bundler/spec_set.rb:81:in `map!'
        from /var/lib/gems/2.3.0/gems/bundler-2.0.2/lib/bundler/spec_set.rb:81:in `materialize'
        from /var/lib/gems/2.3.0/gems/bundler-2.0.2/lib/bundler/definition.rb:170:in `specs'
        from /var/lib/gems/2.3.0/gems/bundler-2.0.2/lib/bundler/definition.rb:237:in `specs_for'
        from /var/lib/gems/2.3.0/gems/bundler-2.0.2/lib/bundler/definition.rb:226:in `requested_specs'
        from /var/lib/gems/2.3.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:108:in `block in definition_method'
        from /var/lib/gems/2.3.0/gems/bundler-2.0.2/lib/bundler/runtime.rb:20:in `setup'
        from /var/lib/gems/2.3.0/gems/bundler-2.0.2/lib/bundler.rb:107:in `setup'
        from /var/lib/gems/2.3.0/gems/bundler-2.0.2/lib/bundler/setup.rb:20:in `<top (required)>'
        from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'
        from /usr/lib/ruby/2.3.0/rubygems/core_ext/kernel_require.rb:55:in `require'

rbenv ruby​​ 版本是 2.5.3,ruby -v 给了我ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux],我不明白为什么它会查看/usr/lib/gems/2.3.0。当 cron 执行 rake 任务时也会发生同样的情况。关于什么可能是错误的任何想法?先谢谢了。

编辑: 如果我在服务器上手动执行它:RAILS_ENV=production nohup bundle exec rake bot:start_poller &gt; /home/deployer/apps/aws-rails/www/shared/log/telegram.log 2&gt;&amp;1 &amp; echo $! &gt; /home/deployer/apps/aws-rails/www/shared/tmp/pids/telegram-server.pid,一切正常。

【问题讨论】:

    标签: ruby-on-rails rake capistrano rbenv


    【解决方案1】:

    如果你想让 Capistrano 使用你的 rvm 版本,那么你需要使用 capistrano/rbenv gem

    # Capfile
    require 'capistrano/rbenv'
    
    
    # config/deploy.rb
    set :rbenv_type, :user # or :system, depends on your rbenv setup
    set :rbenv_ruby, '2.4.2'
    

    更多详情,请查看以下链接

    https://github.com/capistrano/rbenv

    为了将 rbenv 与 crons 一起使用,请将以下内容添加到您的 bashrc 中

    if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
    

    https://benscheirman.com/2013/12/using-rbenv-in-cron-jobs/

    【讨论】:

    • 但我使用的是 rbenv,而不是 rvm
    • 所有这些设置都已经设置好了。不知道为什么,但这种行为只有在 capistrano 或 cron 运行任务时才会出现。部署过程没有显示任何错误。
    • 对不起,你让我感到困惑,因为 crons 不使用 capastrino,你直接调用应用程序。我更新了 crons 的答案,但有 2 个不同的问题
    • eval "$(rbenv init -)" 也存在于 .bashrc 中
    【解决方案2】:

    我找到了解决方案,应该添加这个$HOME/.rbenv/bin/rbenv exec,所以,最后,可执行命令应该是这样的:execute :nohup, "$HOME/.rbenv/bin/rbenv exec bundle exec rake bot:start_poller &gt; #{shared_path}/log/telegram.log 2&gt;&amp;1 &amp; echo $! &gt; #{shared_path}/tmp/pids/telegram-server.pid"

    【讨论】:

      猜你喜欢
      • 2011-05-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-06
      • 2013-06-07
      • 2011-12-03
      • 1970-01-01
      • 2015-04-15
      相关资源
      最近更新 更多