Search code examples
python-3.xfilesqlalchemyjupyter-notebookapscheduler

APScheduler needs to be redefined on startup


I'm using apscheduler on jupyter notebook to schedule cron jobs and I'm using the sqlalchemy jobstore.

Every time I open up the application I have to redefine the variables and as there is no physical job store file, the jobs are reset.

So my question is: How can I get that jobs.sqlite as a physical file? And what is the proper technique for defining an apscheduler scheduler so that it doesn't have to be redefined every time?

import sqlalchemy
import pymongo
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.jobstores.mongodb import MongoDBJobStore
from apscheduler.jobstores.sqlalchemy import SQLAlchemyJobStore
from apscheduler.executors.pool import ThreadPoolExecutor,     ProcessPoolExecutor
from apscheduler.triggers.cron import CronTrigger
import logging

logging.basicConfig()
logging.getLogger('apscheduler').setLevel(logging.DEBUG)

jobstores = {
    'default': SQLAlchemyJobStore(url='sqlite:///jobs.sqlite'),
    'mongo': MongoDBJobStore()
}

executors = {
    'default': ThreadPoolExecutor(20),
    'processpool': ProcessPoolExecutor(5)
}

job_defaults = {
    'coalesce': False, 
    'max_instances': 100
}

scheduler = BackgroundScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone="Asia/Karachi")
scheduler.configure({'apscheduler.daemon': False})

Solution

  • I solved it.

    Instead of doing jobstores=jobstores in the last line. You add another line:

    scheduler.add_jobstore('sqlalchemy', url='sqlite:///jobs.sqlite')