dbader / schedule

Python job scheduling for humans.
https://schedule.readthedocs.io/
MIT License
11.83k stars 965 forks source link

random based on time of day #472

Open rocky4546 opened 3 years ago

rocky4546 commented 3 years ago

Library already has random for interval, but would be very nice if the time base of at() could also have something that would allow randomizing. It could use the to() function to store the maximum delta time. Example: schedule.every().day().at('18:00').to(60).do(job) This would allow for a random time from 18:00 to 19:00. Also, this is simple in that the random delta time would be from 0 to the number provided and then added to the at() time. Changes would be mostly in the _schedule_next_run method around line 670 Need for the update is to have tv guide data updated around 6:00pm each day before the evening broadcasts, but need to have everyone who uses the app to not request the data from the server at the same time causing possible overload.

leirons commented 2 years ago

i will try to do it

jamesgeddes commented 1 year ago

Would this be outside the scope of the library? The string necessary to accomplish this can easily be built with

from random import randint

go_time = f'18:{randint(0,59):02}'

That string can then be fed into schedule.

schedule.every().day().at(go_time).do(job)