namespace :test do
desc "Pick a random participant from database"
task :pick_participant => :environment do
if Rails.env.production?
# MySQL is used as the production DB, its random function is RAND()
p = Participant.find(:first, :order => 'RAND()')
else
# sqlite3 is used as the development DB, its random function is random()
p = Participant.find(:first, :order => 'random()')
end
puts "#{Time.now}"
puts "* environment is: #{Rails.env}"
puts "* picked participant: #{p.first_name}"
end
end
To test the rake task from command line
$ bundle exec rake test:pick_participant
2011-11-04 10:49:08 -0700
* environment is: development
* picked participant: Macy
Add the rake task to Whenever gem's schedule.rb
every 1.minutes do
rake "test:pick_participant"
end
Whenever defaults the environment to "production", to specify a different environment to run:
every 1.minutes do
rake "test:pick_participant RAILS_ENV=development"
#rake "test:pick_participant"
end
Call Whenever to update the crontab
$ whenever --update-crontab app_0515
[write] crontab file updated
$ crontab -l
# Begin Whenever generated tasks for: app_0515
* * * * * /bin/bash -l -c 'cd /Users/[username]/Development/rails_projects/app_0515 && RAILS_ENV=production bundle exec rake test:pick_participant --silent'
# End Whenever generated tasks for: app_0515
No comments:
Post a Comment