簡體   English   中英

capistrano 和 cron 中的 Rake 任務使用錯誤的 ruby​​ 版本

[英]Rake tasks in capistrano and cron use wrong ruby version

我需要在部署過程中執行一個 rake 任務。 為此,我在 deploy 命名空間中有一個任務:

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 > /home/deployer/apps/aws-rails/www/shared/log/telegram.log 2>&1 & echo $! > /home/deployer/apps/aws-rails/www/shared/tmp/pids/telegram-server.pid RAILS_ENV=production nohup bundle exec rake bot:start_poller > /home/deployer/apps/aws-rails/www/shared/log/telegram.log 2>&1 & echo $! > /home/deployer/apps/aws-rails/www/shared/tmp/pids/telegram-server.pid ,一切正常。

如果您希望 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

要在 crons 中使用 rbenv,請將以下內容添加到您的 bashrc

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

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

我找到了解決方案,應該添加這個$HOME/.rbenv/bin/rbenv exec ,所以,最后,可執行命令應該是這樣的: execute :nohup, "$HOME/.rbenv/bin/rbenv exec bundle exec rake bot:start_poller > #{shared_path}/log/telegram.log 2>&1 & echo $! > #{shared_path}/tmp/pids/telegram-server.pid"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM