4

How do I capture logs inside a job from apscheduler?

Assume, I have the following job

@sched.interval_schedule(hours=3)
def some_job():
    log.info('I was here.')
    log.info('And here.')

And a listener

sched.add_listener(job_listener,
                       events.EVENT_JOB_EXECUTED |
                       events.EVENT_JOB_MISSED |
                       events.EVENT_JOB_ERROR)

def job_listener(event):
    # how do I get the logged messages here?

How can I access the messages in job_listener?

dominik
  • 5,745
  • 6
  • 34
  • 45

1 Answers1

3

This can be done using Thread Safe Queue

import logging

import Queue
from apscheduler import events
from apscheduler.scheduler import Scheduler
import time


#logging.basicConfig(level=logging.INFO,format='%(asctime)s : %(name)s : %(levelname)s : %(module)s.%(funcName)s(%(lineno)d) : %(thread)d %(threadName)s: %(message)s')

#enable logger to see exceptions caught by apscheduler
logging.basicConfig()



q = Queue.Queue()

sched = Scheduler()


@sched.interval_schedule(seconds=1)
def some_job():
    msg = "Decorated job : %s" % time.time()
    print msg
    logging.info(msg)
    q.put(msg)
    q.put("message 2")


def job_listener(event):
    #print str(event)
    while not q.empty():
        get_ = "msg from job '%s': '%s'" % (event.job, q.get())
        print get_
        logging.info(get_)


sched.add_listener(job_listener,
                   events.EVENT_JOB_EXECUTED |
                   events.EVENT_JOB_MISSED |
                   events.EVENT_JOB_ERROR)

config = {'apscheduler.jobstores.file.class': 'apscheduler.jobstores.shelve_store:ShelveJobStore',
          'apscheduler.jobstores.file.path': '/temp/dbfile'}

sched.configure(config)


sched.start()

q.join()
while True:
    pass

Output:

Decorated job : 1363960621.39
msg from job 'some_job ...: 'Decorated job : 1363960621.39'
msg from job 'some_job ...': 'message 2'
Decorated job : 1363960622.4
msg from job 'some_job ...: 'Decorated job : 1363960622.4'
msg from job 'some_job ...: 'message 2'
Decorated job : 1363960623.39
vvladymyrov
  • 5,715
  • 2
  • 32
  • 50
  • Is there a race condition here? I don't see how the messages in the queue are actually associated with jobs. Isn't it possible that job 1 puts a message in the queue, then job 2 does in another thread, and job 2's event fires first and you see it print "msg from job 2: " alongside the message from job 1? – Daniel Sep 23 '15 at 16:59
  • Yes, confirmed. Do not use the above code! If you have more than one job running simultaneously, you will NOT get the correct job ID associated with the correct message. – Daniel Sep 23 '15 at 17:20