dbader / schedule

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

Fail and re-schedule a job #444

Closed VahidTa closed 3 years ago

VahidTa commented 3 years ago

Hi, I tried your library in my app. I needed that scheduling for a function happens in a specific time and if result of the function were False, it start to check the function again every e.g. 10 mins until the function result be True and then it stop 10mins re-scheduling for rest of the day. Also, if at the end of the day function not be True, it reset the re-scheduling of every 10 mins and start the task for the new day.

I hope you understand my mean!

So, I use schedule tag function to resolve my issue. I want to share it with you and your users if they need functionality like this.

Here is the code:

def test(arg1, arg2):
    schedule.clear(f'{arg1}') ###to stop the re-schedule with dynamic tag

    now = datetime.now().strftime('%H:%M') ## to find out the time at starting scheduling
    end = '23:45'  ## time of reseting before midnight

    if now > end: ## it stops all the re-scheduling if time passed '23:45' 
        return 

    result = some_function(arg1, arg2)
    if not result:
      schedule.every(10).minutes.do(test, arg1, arg2).tag(f'{arg1}') ## to create a new dynamic tag for this specific rescheduling

schedule.every().day.at("11:12").do(test, arg1, arg2)

while True:
    schedule.run_pending()
    time.sleep(1)

I hope this can help others too.

SijmenHuizenga commented 3 years ago

Hi @VahidTa, thanks for sharing this example. I've marked this ticket as 'question' so others can find it.