In my Rails 3.2 app I have a custom rake task that I am trying to run every day at 5pm with a cron job. Right now I am running it on our site's Staging server. The cron job is setup correctly but according to the e-mail output from the cron daemon the rake is being aborted because it's trying to invoke sqlite3.
Here is my cron job:
#crontab
0 17 * * * cd /u/apps/my_app/current && /usr/local/bin/rake my_task
I have reserved sqlite3 for development and test, like so:
#Gemfile
group :development, :test do
gem 'factory_girl_rails'
gem 'letter_opener'
gem 'rspec-rails'
gem 'sqlite3'
gem 'thin'
gem 'pry-rails'
end
I also have set my rake task to load the proper environment like so:
#mytask.rake
task :my_task => :environment do
# my task
end
This is the error I'm getting in the e-mail from crond:
rake aborted!
Please install the sqlite3 adapter: `gem install activerecord-sqlite3-adapter` (sqlite3 is not part of the bundle. Add it to Gemfile.)
If I run the rake tasks directly from the shell I don't get an error. It would seem that this works because of the following line in ~/.bashrc
:
alias rake='RAILS_ENV=staging rake'
This, however, doesn't seem to have any effect on the task when run from the cron job. I've tried adding export RAILS_ENV=staging
to the .bashrc
file as recommended here but it didn't help.
The one thing I've found that works is writing the cron job like this:
#crontab
0 17 * * * cd /u/apps/my_app/current && /usr/local/bin/rake my_task RAILS_ENV=staging
...with the env declaration directly in the cron command. This doesn't seem very elegant but it's okay for now. Is there a better way to go about this?