如何在 Rails 中使用 when 触发 rake 任务 [英] How to trigger a rake task using whenever in Rails

查看:54
本文介绍了如何在 Rails 中使用 when 触发 rake 任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个 Rake 任务.我想在每个月的最后一个星期日晚上 11 点触发它.

I am writing a Rake task. I want to trigger it every last sunday of every month at 11 pm.

我如何使用 Ruby on Rails 中的 Every gem 来安排这个?

How can I schedule this using the Whenever gem in Ruby on Rails?

我的 rake 任务位于以下位置:app/lib/tasks/my_task.rake

I have my rake task in the following location: app/lib/tasks/my_task.rake

task :create_entries => :environment do
  puts "Hello"
end 

推荐答案

我不认为一个月的最后 x 天"有规则,但您总是可以进行额外的 if 测试块内:

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

所以,这个预定的代码会在每个星期天运行,但只有在时间满足进一步的条件时才会继续调用 rake 任务.

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.

这篇关于如何在 Rails 中使用 when 触发 rake 任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆