Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails cron with whenever, setting the environment

This question will probably only make sense if you know about the whenever gem for creating cron jobs. I have a task in my schedule.rb like

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

However when I update my crontab using

whenever --update-crontab appname --set environment=production

the cron jobs still have RAILS_ENV=development. My tasks on production and development are the same right now, I just need to change the environment variable because thinking_sphinx needs to know the current environment. Any ideas on how to do this?

Thanks!

like image 903
Tony Avatar asked Jul 01 '09 16:07

Tony


3 Answers

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 
like image 76
Trung LE Avatar answered Oct 23 '22 11:10

Trung LE


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=production
0 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=development
0 0 1,5,9,13,17,21,25,29 * * /Users/weppos/Sites/git/app/script/runner -e development "AnotherModel.prune_old_records"
like image 44
Simone Carletti Avatar answered Oct 23 '22 10:10

Simone Carletti


For Whenever (0.9.2)

Use the @environment variable for environment check:

case @environment

when 'production'

every 1.minutes do

   rake "user:take_sample"

  end

when 'development'

every 1.minutes do

  rake "user:dev_sample"

  end

end
like image 23
yogendra689 Avatar answered Oct 23 '22 10:10

yogendra689