问题
I am writing a Rake task. I want to trigger it every last sunday of every month at 11 pm.
How can I schedule this using the Whenever gem in Ruby on Rails?
I have my rake task in the following location: app/lib/tasks/my_task.rake
task :create_entries => :environment do
puts "Hello"
end
回答1:
I don't think there's a rule for "last x day of the month" but you can always put an extra if
test inside the block:
every :sunday, :at => '11pm' do
#if the month is different a week from now, we must be in the last
#sunday of the month
if Time.now.month != 1.week.from_now.month
rake "my:rake:task"
end
end
So, this scheduled code will run every sunday, but it will only go on to call the rake task if the time meets the further conditions.
来源:https://stackoverflow.com/questions/32785557/how-to-trigger-a-rake-task-using-whenever-in-rails