Setting a Whenever job at different hours every day
Asked Answered
E

2

12

I'm trying to set a Whenever job that should be executed 2 times a day, exactly at 11am and 11pm. Is there any way to do it with only one block? I mean something like this:

every :day, :at => ['11am','11pm'] do
  runner "Task"
end
Equites answered 24/10, 2012 at 21:36 Comment(2)
This is actually possible in the newest version, as written here.Collator
@Collator did you intend to include documentation?Bertolde
M
8

If you're concerned about DRYness of your code, then how about this?

['11am','11pm'].each do |at|
  every :day, :at => at  do
    runner "Task"
  end  
end
Magdalenemagdalenian answered 24/10, 2012 at 21:38 Comment(1)
For posterity: this is actually possible as written in the question in the newest version, so this isn't necessary (in case you want to update the answer).Collator
P
10

Whenever now supports the syntax proposed in the question.

Just pass an array to the :at option.

every :day, at: ["11am", "11pm"] do
  runner "Task"
end
Plantain answered 9/3, 2016 at 4:12 Comment(0)
M
8

If you're concerned about DRYness of your code, then how about this?

['11am','11pm'].each do |at|
  every :day, :at => at  do
    runner "Task"
  end  
end
Magdalenemagdalenian answered 24/10, 2012 at 21:38 Comment(1)
For posterity: this is actually possible as written in the question in the newest version, so this isn't necessary (in case you want to update the answer).Collator

© 2022 - 2024 — McMap. All rights reserved.