Hairi
Hairi

Reputation: 3743

Execute rake task in another project

On the same machine I have two rails projects/servers.

From the one server I need to execute a rake task from the other project. This means to switch the context and the rake task to be run in the context of its rails project context.

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: cannot load such file -- rails/all /home/csrhub/git/csrhub-frontend/config/application.rb:3:in >`require'

and of course there is such line in application.rb:

require 'rails/all'

But whats the difference? From the bash works, from the script doesn't.

As you can see I am using open3 - supposed to be the best in such cases.

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:

rbenv: rails: command not found

So I think the problem comes from not looking at the correct gemfile.

Upvotes: 1

Views: 464

Answers (1)

zakariah1
zakariah1

Reputation: 394

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.

Execute bash commands from a Rakefile

Upvotes: 1

Related Questions