Rails - 指定不运行命令的日期

时间:2014-12-17 12:45:18

标签: ruby-on-rails ruby datetime ruby-on-rails-4 delayed-job

我有一个通过delayed_job运行的方法。 有一个字段,用户输入他们不希望任务运行的日期,例如

days_off = ["Wednesday","Thursday"]

每当运行该方法时,我想检查今天是否在' days_off'数组,若然,则将24小时加入延迟作业

if days_off.include? Time.now.strftime("%A")
  Call.delay(run_at: (DateTime.now + 24.hours )).my_method(c)
else
  Call.my_method(c)
end

但是我不确定如何连续几天检查days_off数组,即在上面的示例中,该方法应该延迟48小时,因为它不应该在星期三(今天)运行或星期四

1 个答案:

答案 0 :(得分:0)

我会从这样的事情开始:

days_off = ["Wednesday","Thursday"]

if days_off.include? Time.now.strftime("%A")
  one_day = 60 * 60 * 24
  next_run = Time.now
  while days_off.include? next_run.strftime("%A")
    next_run = next_run + one_day
  end
  puts "call.delay() with next_run goes here"
else
  puts "call.my_method() goes here"
end