Search code examples
python-3.xexeschedule

How to clear the pending jobs before it finished in python schedule module?


Actually, I tried this solution, Schedule python clear jobs queue. And it works as I expected.

But, I'm making a simple parenting program, that fetches the time from a sqlite database file. this is my database.

And when the time comes, program will show a notification. After 5 minuets, another notifications will show. 30 sec after the 2nd notification, program will execute logoff command.

My code:

def job():   
   toaster.show_toast(Title, MsgOne, icon_path=Icon) # 1st notification
   time.sleep(300) # Sleep for 5 mins
   toaster.show_toast(Title, MsgTwo, icon_path=Icon) # 2nd notification
   time.sleep(30) # Sleep for 30 sec
   os.system("shutdown -l") # logout 

schedule.every().day.at(TimeFajr).do(job) # Fajr
schedule.every().day.at(TimeDhuhr).do(job) # Dhuhr
schedule.every().day.at(TimeAsr).do(job) # Asr 
schedule.every().day.at(TimeMaghrib).do(job) # Maghrib 
schedule.every().day.at(TimeIshaa).do(job) # Isha'a 

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

Everything's working fine. Even after converting it to exe (using auto-py-to-exe module). I made the program runs on background by adding it to windows startup folder.

The problem is, when I put my pc on sleep mode or lock my PC BEFORE the time hits, and log back in AFTER the time passes, the program runs the QUEUED JOB and logoff the system, even after the time passes. What I want is, that QUEUED JOB needs to be cleared or not to execute that one. I tried the solution on the link above, but it doesn't work after I convert it to exe. Without this, the program works fine. (Sorry, my English is weak!)

FYI, I'm new to python and this is my 1st project. If there's anyway other way that I can achieve what I'm doing, please guide me.


Solution

  • UPDATE:

    I figured out a way to do this finally. I replaced the whole scheduler module with APScheduler

    A snippet from my code:

    from apscheduler.schedulers.blocking import BlockingScheduler
    
    
    
    def job():   
        toaster.show_toast(Title, MsgOne, icon_path=Icon) # 1st notification
        time.sleep(180) # Sleep for 3 mins
        toaster.show_toast(Title, MsgTwo, icon_path=Icon) # 2nd notification
        time.sleep(30) # Sleep for 30 sec
        toaster.show_toast(Title, MsgThree, icon_path=Icon) # 3rd notification
        #ctypes.windll.PowrProf.SetSuspendState(0, 1, 0) # This will put the PC on sleep
        os.system("rundll32.exe user32.dll,LockWorkStation") # lock windows
    
    scheduler.add_job(job, 'cron', day_of_week='mon-sun', hour=fajr_H, minute=fajr_M, misfire_grace_time=1)
    scheduler.add_job(job, 'cron', day_of_week='mon-sun', hour=dhuhr_H, minute=dhuhr_M, misfire_grace_time=1)
    scheduler.add_job(job, 'cron', day_of_week='mon-sun', hour=asr_H, minute=asr_M, misfire_grace_time=1)
    scheduler.add_job(job, 'cron', day_of_week='mon-sun', hour=maghrib_H, minute=maghrib_M, misfire_grace_time=1)
    scheduler.add_job(job, 'cron', day_of_week='mon-sun', hour=ishaa_H, minute=ishaa_M, misfire_grace_time=1)
    
    scheduler.start()
    

    This one worked as I wanted to.