Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to tell bundler where the Gemfile is?

Tags:

ruby

bundler

I'm trying to run a Ruby script from cron.

The script uses Bundler to manage gem dependencies. Since cron does not run in $PWD I get a 'Could not locate Gemfile' error from Bundler everytime, which makes sense since Gemfile is not in the currrent path when running from cron.

Is there a way to tell Bundler to use a Gemfile not in the current path?

like image 744
Camilo Avatar asked Jul 30 '10 14:07

Camilo


People also ask

Where is Gemfile located?

The Gemfile is wherever you want it to be - usually in the main directory of your project and the name of the file is Gemfile . It's convenient to have one because it allows you to use Bundler to manage which gems and which versions of each your project needs to run.

Where is Jekyll Gemfile?

Gemfile. A Gemfile is a list of gems used by your site. Every Jekyll site has a Gemfile in the main folder.

Where are gems installed bundler?

I know that when using gem install , the gem will be stored under /home/username/. rvm/gems/, under which gemset the gem was installed.


2 Answers

The best thing to do would be to cd into the directory in question in your cron. You could also use the BUNDLE_GEMFILE environment variable to point at the Gemfile. Please let us know if you have any problems with BUNDLE_GEMFILE.

like image 67
Yehuda Katz Avatar answered Sep 18 '22 13:09

Yehuda Katz


You could have the main script change its dir:

Dir.chdir File.dirname(__FILE__) 

You might have to tweak it using File.expand_path to get your app's root dir (where the Gemfile is). Suppose your script is /apps/myapp/bin/main.rb; the chdir line would be:

Dir.chdir File.expand_path('../..', __FILE__) 

Of course, you have to make sure your app doesn't break because of the chdir - but it shouldn't if you're cd-ing in the cron.

If the script is a daemon and you want it to be changed to the root dir / during normal operation, you can do that after calling Bundler.setup or Bundler.require.

While you could do cd in the cron, I prefer not to, because crons are already hard enough to read and maintain.

like image 33
Kelvin Avatar answered Sep 20 '22 13:09

Kelvin