3

Suppose I wanted to run a rake every x minutes between two times every day? How might I do that? Would between work? Or until? Or must I use cron syntax?

# Refresh daily scores
every 5.minutes, # :between '3pm' :and '1am' do ?
  rake "games:scores_refresh"
end
iamse7en
  • 609
  • 8
  • 21
  • Maybe `every 5.minutes, :at => "3pm", :until => "1am" do`? Documentation is thin and this is untested. Watch to make sure it works over day boundary (if not, you may need two separate ones). – steve klein Jul 15 '15 at 02:37

2 Answers2

3

I don't think the between option is available but you can pass the exact time or day etc the syntax are available as examples here:

https://github.com/javan/whenever

And for your task may be you can pass a array of time:

every :day, :at => (1..15).to_a.map{ |x| (0..55).step(5).to_a.map{ |a| ["#{x}:#{a}"] } }.flatten do
  # Run rake task.
end

# => ["1:0", "1:5", "1:10", "1:15", "1:20", "1:25", "1:30", "1:35", "1:40", "1:45", "1:50", "1:55", "2:0", "2:5", "2:10", "2:15", "2:20", "2:25", "2:30", "2:35", "2:40", "2:45", "2:50", "2:55", "3:0", "3:5", "3:10", "3:15", "3:20", "3:25", "3:30", "3:35", "3:40", "3:45", "3:50", "3:55", "4:0", "4:5", "4:10", "4:15", "4:20", "4:25", "4:30", "4:35", "4:40", "4:45", "4:50"...]

This is just a example. You can make it as you desire. And I am not sure if whenever accepts time in 24 hour format or not. So please check it out and try like this.

Update 1:

Was doing more R&D and found the above to be the solution to the problem as denoted by the gem creator. Here is the link:

https://github.com/javan/whenever/issues/506

Didn't knew about rjust which can be added above. And regarding AM & PM I think you will be needing to add that (not sure). If it needs to be added break it into two. One for AM and other for PM.

Update 2:

Another easy solution I Found is:

every '*/30 6-9 * * *' do
  runner "Model.method"
end

The above runs in every 30 minutes between 6 to 9.

Source:

Rails Execute Cron Job or Rake task for specific time period

Hope this helps.

Community
  • 1
  • 1
Deepesh
  • 6,138
  • 1
  • 24
  • 41
  • Thanks for sharing but you could explain what 3 star stand for please? – stan liu Dec 04 '17 at 02:48
  • 1
    @stanliu this means Every Day, Every Month and Every Day of the Week. You may specify some specific days or month or days of week to run – Deepesh Dec 05 '17 at 11:15
3

A simple cron approach which should work:

every '*/5 15-23,0 * * *' do
  rake "games:scores_refresh"
end

Run rake job every five minutes for any hour starting between 3 pm and 11 pm and for the hour starting at midnight.

steve klein
  • 2,566
  • 1
  • 14
  • 27