apscheduler可以通过添加监听器,得到定时任务的反馈。监听会监听到的是apscheduler.events
,进入apscheduler/events.py
文件中可以看到,使用常量对事件的定义:
EVENT_SCHEDULER_STARTED = EVENT_SCHEDULER_START = 2 ** 0
EVENT_SCHEDULER_SHUTDOWN = 2 ** 1
EVENT_SCHEDULER_PAUSED = 2 ** 2
EVENT_SCHEDULER_RESUMED = 2 ** 3
EVENT_EXECUTOR_ADDED = 2 ** 4
EVENT_EXECUTOR_REMOVED = 2 ** 5
EVENT_JOBSTORE_ADDED = 2 ** 6
EVENT_JOBSTORE_REMOVED = 2 ** 7
EVENT_ALL_JOBS_REMOVED = 2 ** 8
EVENT_JOB_ADDED = 2 ** 9
EVENT_JOB_REMOVED = 2 ** 10
EVENT_JOB_MODIFIED = 2 ** 11
EVENT_JOB_EXECUTED = 2 ** 12
EVENT_JOB_ERROR = 2 ** 13
EVENT_JOB_MISSED = 2 ** 14
EVENT_JOB_SUBMITTED = 2 ** 15
EVENT_JOB_MAX_INSTANCES = 2 ** 16
常量描述事件类
- EVENT_SCHEDULER_STARTED:调度程序已启动
- EVENT_SSCHEDULER_SHUTDOWN:计划程序已关闭
- EVENT_SSCHEDULER_PAUSED:计划程序中的作业处理已暂停
- EVENT_SSCHEDULER_RESUMED:计划程序中的作业处理已恢复
- EVENT_EXECUTOR_ADDED:已将一个执行程序添加到调度程序
- EVENT_EXECUTOR_REMOVED:已将执行程序删除到计划程序
- EVENT_JOBSTORE_ADDED:已将作业存储添加到调度程序
- EVENT_JOBSTORE_REMOVED:已从计划程序中删除作业存储
- EVENT_ALL_JOBS_REMOVED:所有作业都已从所有作业存储或一个特定作业存储中删除
- EVENT_JOB_ADDED:作业已添加到作业存储
- EVENT_JOB_REMOVED:已从作业存储中删除作业
- EVENT_JOB_MODIFIED:从调度程序外部修改了作业
- EVENT_JOB_SUBMIT:已将作业提交给其执行者
示例程序
from datetime import datetime
from apscheduler.schedulers.background import BackgroundScheduler
from apscheduler.events import EVENT_JOB_ERRORdef print_each_five_seconds():print(f"=== 自动化每5s执行一次:{datetime.now()} ===")raise # 主动报错def my_listener(event):print(event)if event.code == EVENT_JOB_ERROR: # 报错信息 或使用 event.code==EVENT_JOB_ERROR 判断也行print(f"{event.scheduled_run_time} 定时任务报错 {event.exception}")def main():print("============================ 启动 自动化 ============================")# 1.创建调度器scheduler = BackgroundScheduler()# 2. 添加任务scheduler.add_job(print_each_five_seconds, 'interval', seconds=5)scheduler.add_listener(my_listener) # 添加监听# 3.满足条件执行器scheduler.start()if __name__ == '__main__':main()
得到结果:
============================ 启动 自动化 ============================
<SchedulerEvent (code=16)>
<SchedulerEvent (code=64)>
<JobEvent (code=512)>
<SchedulerEvent (code=1)>=== 自动化每5s执行一次:2024-03-12 15:18:09.146576 ===
<JobSubmissionEvent (code=32768)>
<JobExecutionEvent (code=8192)>
2024-03-12 15:18:09.143157+08:00 定时任务报错 No active exception to reraise
Job "print_each_five_seconds (trigger: interval[0:00:05], next run at: 2024-03-12 15:18:14 CST)" raised an exception
Traceback (most recent call last):File "C:\Users\User4\Anaconda3\envs\qlib_learn\lib\site-packages\apscheduler\executors\base.py", line 125, in run_jobretval = job.func(*job.args, **job.kwargs)File "E:/dingzeng/granfna_helper/test/add_scheduler_listener/check_a.py", line 10, in print_each_five_secondsraise # 主动报错
RuntimeError: No active exception to reraise
<JobSubmissionEvent (code=32768)>=== 自动化每5s执行一次:2024-03-12 15:18:14.148286 ===
<JobExecutionEvent (code=8192)>
2024-03-12 15:18:14.143157+08:00 定时任务报错 No active exception to reraise
Job "print_each_five_seconds (trigger: interval[0:00:05], next run at: 2024-03-12 15:18:19 CST)" raised an exception
Traceback (most recent call last):File "C:\Users\User4\Anaconda3\envs\qlib_learn\lib\site-packages\apscheduler\executors\base.py", line 125, in run_jobretval = job.func(*job.args, **job.kwargs)File "E:/dingzeng/granfna_helper/test/add_scheduler_listener/check_a.py", line 10, in print_each_five_secondsraise # 主动报错
RuntimeError: No active exception to reraise
可以看到 ,我们可以根据code
获取事件的结果,并给与反馈