天天看點

apscheduler的監聽事件

from apscheduler.events import EVENT_JOB_ERROR, EVENT_JOB_MISSED, EVENT_JOB_EXECUTED
import logging

logger = logging.getLogger('job')

def job_listener(Event):
    job = scheduler.get_job(Event.job_id)
    if not Event.exception:
        logger.info("jobname=%s|jobtrigger=%s|jobtime=%s|retval=%s", job.name, job.trigger,
                    Event.scheduled_run_time, Event.retval)
    else:
        logger.error("jobname=%s|jobtrigger=%s|errcode=%s|exception=[%s]|traceback=[%s]|scheduled_time=%s", job.name,
                     job.trigger, Event.code,
                     Event.exception, Event.traceback, Event.scheduled_run_time)
scheduler.add_listener(job_listener, EVENT_JOB_ERROR | \
                           EVENT_JOB_MISSED | \
                           EVENT_JOB_EXECUTED)