简体   繁体   中英

How to trigger a rake task using whenever in Rails

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 

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM