How to trigger a rake task using whenever in Rails

给你一囗甜甜゛ 提交于 2020-01-03 05:14:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!