0

I have the below code snippet that runs every 3 hours from when the script starts which is great, I'm wondering if there is a way to specify it to run every 3 hours at the top of the hour for example 00:00, 03:00, 06:00, 09:00 etc.?

schedule.every(3).hours.do(self.run_report)

I'm trying to be able to consistently say the report will generate every 3 hours at these times instead of it'll run 3 hours after the last run.

llanato
  • 2,508
  • 6
  • 37
  • 59

1 Answers1

0

The schedule module you are using enables combining different attributes as well.

You could do this as:

schedule.every(3).hours.at("00:00").do(self.run_report)

This way no matter when script starts, the job will be scheduled at start of day and run at every 00:00, 03:00, 06:00, 09:00.. and so on. You can choose schedule at time accordingly, eventually your scripts runs like you want to

MohitC
  • 4,541
  • 2
  • 34
  • 55
  • Close but if the script is run at say 1pm the scheduler wont kick in for another 11 hours. – llanato Apr 17 '20 at 23:31
  • In that case you could choose your start time as 3pm. Eventually your script picks up at time u want. – MohitC Apr 18 '20 at 08:46
  • that was just an example, the script could be restarted at any time during in the day for a number of reasons, so not a runner unfortunately. – llanato Apr 19 '20 at 07:46
  • 1
    @llanato, you could easily create a function to get next best time to start the schedule from defined list of time. Just get the current time programmatically and then find next best time and put that in `at()` value of your function. Refer these to get next best time https://stackoverflow.com/questions/32237862/find-the-closest-date-to-a-given-date https://stackoverflow.com/questions/17249220/getting-the-closest-date-to-a-given-date – MohitC Apr 22 '20 at 19:31