I have a periodic job. Since its upgrades are needed and I do not want to stop the main code, I wish I could modify/update the job on the fly. I'll demonstrate by simple example.
Main script:
from apscheduler.schedulers.blocking import BlockingScheduler
from my_job_func import my_job
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(my_job, 'interval', seconds=3)
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
pass
job to be modified on-the-fly in file my_job_func.py:
def my_job():
print('Make some changes!!')
So, is it somehow possible to modify or update my_job() on-the-fly without stopping the main script?
from apscheduler.schedulers.blocking import BlockingScheduler
from apscheduler.triggers.interval import IntervalTrigger
from my_job_func import my_job
def observer(job):
# you can either reschedule the job
job.reschedule(...)
# or modify it
changes = get_update() # get update from configuration
job.modify(**changes)
if __name__ == '__main__':
scheduler = BlockingScheduler()
my_job_id = scheduler.add_job(my_job, 'interval', seconds=3)
scheduler.add_job(observer, IntervalTrigger(minutes=5), args=(my_job_id,), name="observer")
scheduler.start()
You can add a separate observer job help modifying my_job
on the fly.
Note that the change may not take effect in no time. You can adjust the observer job's interval to fit your need.