skip to Main Content

I need to execute a rake task in the deployment process. For doing this, I have a task in the deploy namespace:

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

It is executed like this: after :stop_telegram, :start_telegram. The output of logfile is:

/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 version is 2.5.3, ruby -v gives me ruby 2.5.3p105 (2018-10-18 revision 65156) [x86_64-linux], and I can’t get why does it looks into /usr/lib/gems/2.3.0. The same happens when a rake task is executed by cron. Any ideas of what can be wrong? Thanks ahead.

EDIT:
If I execute it manually on the server: 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, everything works just fine.

2

Answers


  1. Chosen as BEST ANSWER

    I've found the solution, this $HOME/.rbenv/bin/rbenv exec should be added, so, finally, the executable command should look like this: 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"


  2. If you want Capistrano to use your rvm version then you need to use 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'
    

    for more details, check the link below

    https://github.com/capistrano/rbenv

    for using rbenv with crons add the following to your bashrc

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

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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search