Search code examples
pythonmysqlsqlalchemyapscheduler

How can i restart saved scheduler in mysql using python


With following codes, i saved jobstore in my mysql Database for testing.

from apscheduler.schedulers.background import BlockingScheduler

def tick ():
    print ("Hi")

sched = BlockingScheduler()
url = 'mysql://myname:mypw@localhost/rssdb'
sched.add_jobstore('sqlalchemy', url=url)
sched.add_job(tick,'interval',seconds=10)
sched.start()

I have checked it was saved properly and want to load and execute the job. how should i do ?


Solution

  • If the job is saved into mysql and you see that You can do following. When adding job also include which jobstore you want to add it. Also you can add other options like job_defaults to the Scheduler together with job_stores

    from apscheduler.schedulers.background import BlockingScheduler
    
    def tick ():
       print ("Hi")
    
    jobstores = {
            'sqlalchemy': SQLAlchemyJobStore(url='mysql://myname:mypw@localhost/rssdb')
    }
    sched = BlockingScheduler(jobstores=jobstores)
    sched.start()
    #to add a job you better also say which jobstore you want to add it
    sched.add_job(tick,'interval',seconds=10, jobstore='sqlalchemy')