2

Issue : I have two applications A & B both running on the same server. I have a script in the filesystem

cd /data/B/
bundle exec rake -T 

When i run the script through the rails console of application A it errors as the console loads the gems of A and the rake task fails

  eg: system("sh ~/test.sh")

rake aborted!
LoadError: cannot load such file -- log4r
/home/kumolus/api/config/application.rb:9:in `require'
/home/kumolus/api/config/application.rb:9:in `<top (required)>'
/home/kumolus/api/Rakefile:1:in `require'
/home/kumolus/api/Rakefile:1:in `<top (required)>'
(See full trace by running task with --trace)

When i run the script through the unix command line (irrespective of my pwd) it works

cd ~
sh test.sh  #works

cd /data/A   #my application A's dir
sh ~/test.sh   #also works

I need it to work through rails.Any help ? Thanks!

PrashantG
  • 91
  • 6
  • 1
    Are you using RVM? Do the projects have different environments, eg different gemsets/ruby versions? – Max Williams Oct 20 '15 at 08:16
  • No we are managing the applications though bundler – PrashantG Oct 20 '15 at 08:25
  • both the applications are on the same environment but they use different gems through bundler – PrashantG Oct 20 '15 at 08:29
  • When you do it on the command line, and it works, are you in the project folder for application A? – Max Williams Oct 20 '15 at 08:45
  • it works from any location, if i give the scripts absolute path, as well as if i am in project A with the scripts relative path – PrashantG Oct 20 '15 at 08:52
  • You haven't included any absolute path examples in your question... Can you add, to your question, exactly what you are typing into the command line when you're in the Project A folder and successfully run the task for project B? – Max Williams Oct 20 '15 at 08:54
  • i believe the issue is that in the console the libraries that are loaded are of the current application(A) hence running the rake task of another applicaition(B) calls its applicaiton.rb with attempts to load the gems that are used by (B) but not loaded by Application A and hence fails – PrashantG Oct 20 '15 at 08:56
  • I have added to the question. Hope it helps you understand better. Please let me know if i can provide more info. thanks. – PrashantG Oct 20 '15 at 09:05

2 Answers2

2

Thanks guys for your help, I found a solution its due to bundler default behaviour.

I used with_clean_env method with a block and it gets executed.

Thanks Team Cheers

PrashantG
  • 91
  • 6
1

I think cd'ing to the folder should be sufficient to switch the environment within the shell process which is doing the cd'ing. Try this.

Go into project B and do

which bundle

in the command line, and copy the result somewhere. Then do

which rake

and copy the result of that.

Then, switch to project A and start a rails console. Then try this:

`cd /path/to/projectB; bundle exec rake  -T`

If that doesn't work, try this:

`cd /path/to/projectB; <result of doing "which bundle" earlier> exec rake -T`

Then if it still doesn't work, try this:

`cd /path/to/projectB; <result of doing "which bundle" earlier> exec <result of doing "which rake" earlier> -T`
Max Williams
  • 32,435
  • 31
  • 130
  • 197
  • That didn't work either, I get the same error regarding log4r...as its used in Application B..but not in Application A..I believe loading the console through A loads all of its libraries, as a result the script doesn't have the log4r gem loaded – PrashantG Oct 20 '15 at 09:26