Cron jobs are really useful for recurring background jobs ( mailing, indexing, rake task etc.). In Ruby on Rails world there are plenty of options for background proccessing but most of them are not that simple and easy to being with. However Whenever from Javan Makhmali is pretty simple and easy to use DSL.
Whenever helps you keep your cron jobs with your code so that there is no separation of logic. Since Whenever is a wrapper for cron, however, it’s really focused on on UNIX and UNIX-like machines.
Here an example of Whenever configuration for multiple environments:
set :output, { :error => 'log/cron.error.log', :standard => 'log/cron.log' }
job_type :rake, "cd :path && RAILS_ENV=:environment bundle exec rake :task --silent :output"
job_type :script, "cd :path && RAILS_ENV=:environment bundle exec script/:task"
case @environment
when 'production'
every 12.hours do
rake 'load_prices:run'
end
every '15,45 * * * *' do
rake 'load_availabilities:run'
end
every :hour do
script 'move_transaction'
end
when 'alpha'
every :day, :at => '6:30 am' { rake 'load_prices:run' }
every :day, :at => '6:00 am' { rake 'load_availabilities:run' }
every 8.hours { script 'copy_production_files' }
when '...'
...
...
end
To test and see the result of Whenever, execute whenever --set environment=production
in terminal.
0 * * * * /bin/bash -l -c 'cd /app/path && RAILS_ENV=production bundle exec script/move_transaction'
0 0,12 * * * /bin/bash -l -c 'cd /app/path && RAILS_ENV=production bundle exec rake load_prices:run --silent >> log/cron.log 2>> log/cron.error.log'
15,45 * * * * /bin/bash -l -c 'cd /app/path && RAILS_ENV=production bundle exec rake load_availabilities:run --silent >> log/cron.log 2>> log/cron.error.log'
Leave a Reply