Execute system command with Rake outside Bundle scope

旧时模样 提交于 2019-12-10 12:54:03

问题


Let say I have this Rake task:

namespace :db do
  namespace :dump do.
    desc 'Backup database dump to s3'
    task :backup => :environment do 
      cmd = ['backup', 'perform',  '-t project_backup',  "-c #{Rails.root.join 'lib', 'backup', 'config.rb'}"] 
      system(*cmd)                      # ...I've tried `` & exec() sa well, same thing
    end
  end
end

Backup gem is stand alone ruby gem application which dependencies needs to be isolated from application bundler. In other words it cannot be part of Gemfile. This gem is simply installed over gem install backup

When I run backup command over bash console, it successfully run:

$ backup perform -t validations_backup -c /home/equivalent/my_project/lib/backup/config.rb

When I execute rake db:dump:backup I will get

backup is not part of the bundle. Add it to Gemfile. (Gem::LoadError)

...which is the same thing when I run backup command with bundle exec from bash

$ bundle exec backup perform -t validations_backup -c /home/equivalent/my_project/lib/backup/config.rb

...meaning that the backup command is executed over bundler when run as part of rake task.

my question: How can I run rake db:dump:backup outsite the bundle scope, meaning that backup command won`t be executed over bundler?

Thank you


回答1:


I found a workaround for this problem here:

namespace :db do
  namespace :dump do
    desc 'Backup database dump to s3'
    task :backup do
      Bundler.with_clean_env do
        sh "backup perform -t project_backup -c #{Rails.root.join 'lib', 'backup', 'config.rb'}"
      end
    end
  end
end

The key here is to enclose the code that must not run under bundler's environment in a block like this:

Bundler.with_clean_env do
  # Code that needs to run without the bundler environment loaded
end



回答2:


Here is the Capistrano solution I was mentioning for those who need it while we figure out how to fix Rake.

class BackupDatabaseCmd
  def self.cmd
    # some logic to calculate :
    'RAILS_ENV=production backup perform -t name_of_backup_task -c  /home/deploy/apps/my_project/current/lib/backup/config.rb'
    # in the configuration file I'm loading `config/database.yml`
    # and passing them to backup gem configuration
  end
end

namespace :backup do
  namespace :database do
    task :to_s3 do
      on roles(:web) do
        within release_path do
          with rails_env: fetch(:rails_env) do
            execute(BackupDatabaseCmd.cmd)
          end
        end
      end
    end
  end
end

# cap production backup:database:to_s3


来源:https://stackoverflow.com/questions/24284347/execute-system-command-with-rake-outside-bundle-scope

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!