Rails cron with whenever, setting the environment Rails cron with whenever, setting the environment ruby-on-rails ruby-on-rails

Rails cron with whenever, setting the environment


Whenever doesn't detect your environment, it just defaults to using production. You can set the environment for all jobs using set:

set :environment, 'staging' 

Or per job:

every 2.hours do   runner 'My.runner', :environment => 'staging' end 


Don't write the RAILS_ENV variable. It should set it automatically.

every 1.day, :at => '4am' do  command "cd #{RAILS_ROOT} && rake thinking_sphinx:stop"  command "cd #{RAILS_ROOT} && rake thinking_sphinx:index"  command "cd #{RAILS_ROOT} && rake thinking_sphinx:start"end

It works in my app:

every 4.days do  runner "AnotherModel.prune_old_records"end$ whenever --set environment=production0 0 1,5,9,13,17,21,25,29 * * /Users/weppos/Sites/git/app/script/runner -e production "AnotherModel.prune_old_records"$ whenever --set environment=development0 0 1,5,9,13,17,21,25,29 * * /Users/weppos/Sites/git/app/script/runner -e development "AnotherModel.prune_old_records"


For Whenever (0.9.2)

Use the @environment variable for environment check:

case @environmentwhen 'production'every 1.minutes do   rake "user:take_sample"  endwhen 'development'every 1.minutes do  rake "user:dev_sample"  endend