apscheduler

Pyhon APScheduler stop jobs before starting a new one


I need to start a job every 30 minutes, but before a new job is being started I want the old but same job being terminated. This is to make sure the job always fetches the newest data file which is constantly being updated.

Right now I'm using the BlockingScheduler paired with my own condition to stop the job (stop job if processed 1k data etc.), I was wondering if APScheduler supports this "only 1 job at the same time and stop old one before new one" behavior natively

I've read the docs but I think the closest is still the default behavior which equals max_instances=1, this just prevents new jobs firing before the old job finishes, which is not what I'm looking for.

Any help is appreciated. Thanks!


Solution

  • After further research I came to a conclusion that this is not supported natively in APScheduler, but by inspired by Get number of active instances for BackgroundScheduler jobs , I modified the answer into a working way of detecting the number of current running instances of the same job, so when you have a infinite loop/long task executing, and you want the new instance to replace the old instance, you can add something like

        if(scheduler._executors['default']._instances['set_an_id_you_like'] > 1):
            # if multiple instances break loop/return
            return
    

    and this is what should look like when you start:

        scheduler = BlockingScheduler(timezone='Asia/Taipei')
        scheduler.add_job(main,'cron', minute='*/30', max_instances=3, next_run_time=datetime.now(),\
                          id='set_an_id_you_like')
        scheduler.start() 
    

    but like the answer in the link, please refrain from doing this if someday there's a native way to do this, currently I'm using APScheduler 3.10

    This method at least doesn't rely on calculating time.now() or datetime.datetime.now() in every iteration to check if the time has passed compared when the loop started. In my case since my job runs every 30 minutes, I didn't want to calculate deltatime so this is what I went for, hope this hacky method helped someone that googled for a few days to come here.