简体   繁体   English

在另一个项目中执行 rake 任务

[英]Execute rake task in another project

On the same machine I have two rails projects/servers.在同一台机器上,我有两个 rails 项目/服务器。

From the one server I need to execute a rake task from the other project.我需要从一台服务器执行另一个项目的 rake 任务。 This means to switch the context and the rake task to be run in the context of its rails project context.这意味着切换上下文和要在其 rails 项目上下文的上下文中运行的 rake 任务。

The task loads the environment first:该任务首先加载环境:

task update: :environment do .....

The code that is supposed to run this task looks like this:应该运行此任务的代码如下所示:

require 'open3'
module Tasks
  def self.test
    env_vars = {"RAILS_ENV"=>"development"}
    cmd = "bundle exec rake store:update"
    puts "#{cmd}".light_blue
    opts = {:chdir => '/home/master/git/frontend'}
    begin
      stdout_str, error_str, status = Open3.capture3(env_vars, cmd, opts)
    rescue => exception
      puts "Command failed:"
      puts "#{exception}".red      
    end
    puts "#{stdout_str}".green
    if status.success?
      puts "ok".light_green
      puts "#{status.inspect}".yellow
    else
      puts "Command finished with error:"
      puts "#{error_str}".light_red
    end
  end
end

When executed from the project directory the task runs successfully, but from the script above it throws error:从项目目录执行时,任务成功运行,但从上面的脚本中抛出错误:

rake aborted: LoadError.耙子中止:LoadError。 cannot load such file -- rails/all /home/csrhub/git/csrhub-frontend/config/application:rb:3:in >`require'无法加载此类文件 -- rails/all /home/csrhub/git/csrhub-frontend/config/application:rb:3:in >`require'

and of course there is such line in application.rb:当然在 application.rb 中有这样一行:

require 'rails/all'

But whats the difference?但是有什么区别呢? From the bash works, from the script doesn't.从 bash 作品,从脚本没有。

As you can see I am using open3 - supposed to be the best in such cases.如您所见,我正在使用open3 - 在这种情况下应该是最好的。

I think it has something to do with the context.我认为这与上下文有关。 When I substitute the command cmd = "bundle exec rake store:update" with "rails -v" , I get this error:当我将命令cmd = "bundle exec rake store:update"替换为"rails -v"时,我收到此错误:

rbenv: rails: command not found rbenv:rails:找不到命令

So I think the problem comes from not looking at the correct gemfile.所以我认为问题出在没有查看正确的gemfile。

I'm sorry, I'm not allowed to leave comments.对不起,我不允许发表评论。 I think that you want to create scripts to setup your environment and then call the rake task.我认为您想创建脚本来设置您的环境,然后调用 rake 任务。

Execute bash commands from a Rakefile 从 Rakefile 执行 bash 命令

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM