python apscheduler was missed by
from apscheduler.schedulers.blocking import BlockingScheduler
def my_listener(event):
if event.exception:
logger.error(f'The job:{event.job_id} error')
else:
logger.info(f'The job:{event.job_id} success')
scheduler = BlockingScheduler(timezone="Asia/Shanghai") scheduler.add_job(extract_user, 'cron', hour=1, minute=30, id='user_sync') scheduler.add_job(extract_interaction, 'cron', hour=5, minute=10, id='interact_sync') scheduler.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR) scheduler.start()
错误日志:
2021-11-03 07:03:48,812 - base.py: 445 - INFO: Adding job tentatively -- it will be properly scheduled when the scheduler starts 2021-11-03 07:03:48,813 - base.py: 445 - INFO: Adding job tentatively -- it will be properly scheduled when the scheduler starts 2021-11-03 07:03:48,813 - base.py: 886 - INFO: Added job "extract_user" to job store "default" 2021-11-03 07:03:48,813 - base.py: 886 - INFO: Added job "extract_interaction" to job store "default" 2021-11-03 07:03:48,813 - base.py: 171 - INFO: Scheduler started 2021-11-03 07:03:48,813 - base.py: 120 - WARNING: Run time of job "extract_user (trigger: date[2021-11-03 15:03:48 CST], next run at: 2021-11-03 07:03:48 CST)" was missed by 8:00:00.069631 2021-11-03 07:03:48,814 - base.py: 632 - INFO: Removed job user_sync 2021-11-03 07:03:48,815 - base.py: 120 - WARNING: Run time of job "extract_interaction (trigger: date[2021-11-03 15:03:48 CST], next run at: 2021-11-03 07:03:48 CST)" was missed by 8:00:00.002221 2021-11-03 07:03:48,815 - base.py: 632 - INFO: Removed job interact_sync
错误的原因主要是因为时区不对引起的,修改后:
from tzlocal import get_localzone scheduler = BlockingScheduler(timezone=get_localzone()) scheduler.add_job(extract_user, 'cron', hour=1, minute=30, id='user_sync') scheduler.add_job(extract_interaction, 'cron', hour=5, minute=10, id='interact_sync') scheduler.add_listener(my_listener, EVENT_JOB_EXECUTED | EVENT_JOB_ERROR) scheduler.start()
apscheduler配置参见: