我正在使用python Apscheduler来安排我的工作。我所有的工作都存储为cron job,并使用BackgroundScheduler。我有以下的代码。
def letschedule():
jobstores = {
'default': SQLAlchemyJobStore(url=app_jobs_store)
executors = {
'default': ThreadPoolExecutor(20),
'processpool': ProcessPoolExecutor(5)
job_defaults = {
'coalesce': False,
'max_instances': 1,
'misfire_grace_time':1200
scheduler = BackgroundScheduler(jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=utc)
#jobstores=jobstores, executors=executors, job_defaults=job_defaults, timezone=utc
return scheduler
我在应用程序中启动了工作调度器,如下所示。
sch = letschedule()
sch.start()
log.info('the scheduler started')
而我有以下新增的工作职能。
def addjobs():
jobs = []
sch.add_job(forecast_jobs, 'cron', day_of_week=os.environ.get("FORECAST_WEEKOFDAY"),
id="forecast",
replace_existing=False,week='1-53',hour=os.environ.get("FORECAST_HOUR"),
minute=os.environ.get("FORECAST_MINUTE"), timezone='UTC')
jobs.append({'job_id':'forecast', 'type':'weekly'})
log.info('the forecast added to the scheduler')
except BaseException as e:
log.info(e)
sch.add_job(insertcwhstock, 'cron',
id="cwhstock_data", day_of_week='0-6', replace_existing=False,hour=os.environ.get("CWHSTOCK_HOUR"),
minute=os.environ.get("CWHSTOCK_MINUTE"),
week='1-53',timezone='UTC')
jobs.append({'job_id':'cwhstock_data', 'type':'daily'})
log.info('the cwhstock job added to the scheduler')
except BaseException as e:
log.info(e)
return json.dumps({'data':jobs})
我在flask应用程序中使用了这个功能,我调用了/activatejobs,作业被添加到调度器中,并且工作正常。然而,当我重启wsgi服务器时,工作并没有再次启动,我必须删除.sqlite文件并重新添加工作。我想要的是,一旦调度程序启动,作业就应该自动重新启动(如果数据库中已经有作业的话)。
我试着用一些方法来得到这样的结果,但是做不到。如果有任何帮助,我将不胜感激。 谢谢。