2013-11-15 20:50:20 +00:00
|
|
|
import logging
|
|
|
|
import json
|
2014-04-11 17:32:45 +00:00
|
|
|
import signal
|
2013-11-15 20:50:20 +00:00
|
|
|
|
|
|
|
from threading import Event
|
|
|
|
from apscheduler.scheduler import Scheduler
|
|
|
|
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
class Worker(object):
|
2014-04-02 23:32:41 +00:00
|
|
|
def __init__(self, queue, poll_period_seconds=30, reservation_seconds=300,
|
|
|
|
watchdog_period_seconds=60):
|
2013-11-15 20:50:20 +00:00
|
|
|
self._sched = Scheduler()
|
|
|
|
self._poll_period_seconds = poll_period_seconds
|
|
|
|
self._reservation_seconds = reservation_seconds
|
2014-04-02 23:32:41 +00:00
|
|
|
self._watchdog_period_seconds = watchdog_period_seconds
|
2013-11-15 20:50:20 +00:00
|
|
|
self._stop = Event()
|
|
|
|
self._queue = queue
|
|
|
|
|
|
|
|
def process_queue_item(self, job_details):
|
|
|
|
""" Return True if complete, False if it should be retried. """
|
|
|
|
raise NotImplementedError('Workers must implement run.')
|
|
|
|
|
2014-04-02 23:32:41 +00:00
|
|
|
def watchdog(self):
|
|
|
|
""" Function that gets run once every watchdog_period_seconds. """
|
|
|
|
pass
|
|
|
|
|
2013-11-15 20:50:20 +00:00
|
|
|
def poll_queue(self):
|
|
|
|
logger.debug('Getting work item from queue.')
|
|
|
|
|
|
|
|
item = self._queue.get()
|
|
|
|
while item:
|
|
|
|
logger.debug('Queue gave us some work: %s' % item.body)
|
|
|
|
|
|
|
|
job_details = json.loads(item.body)
|
|
|
|
|
|
|
|
if self.process_queue_item(job_details):
|
|
|
|
self._queue.complete(item)
|
|
|
|
else:
|
|
|
|
logger.warning('An error occurred processing request: %s' % item.body)
|
|
|
|
self._queue.incomplete(item)
|
|
|
|
|
|
|
|
item = self._queue.get(processing_time=self._reservation_seconds)
|
|
|
|
|
|
|
|
logger.debug('No more work.')
|
|
|
|
|
|
|
|
def start(self):
|
|
|
|
logger.debug("Scheduling worker.")
|
|
|
|
|
|
|
|
self._sched.start()
|
2014-04-02 23:32:41 +00:00
|
|
|
self._sched.add_interval_job(self.poll_queue, seconds=self._poll_period_seconds)
|
|
|
|
self._sched.add_interval_job(self.watchdog, seconds=self._watchdog_period_seconds)
|
2013-11-15 20:50:20 +00:00
|
|
|
|
2014-04-11 17:32:45 +00:00
|
|
|
signal.signal(signal.SIGTERM, self.join)
|
|
|
|
signal.signal(signal.SIGINT, self.join)
|
|
|
|
|
2013-11-15 20:50:20 +00:00
|
|
|
while not self._stop.wait(1):
|
|
|
|
pass
|
|
|
|
|
2014-04-11 17:32:45 +00:00
|
|
|
logger.debug('Waiting for running tasks to complete.')
|
|
|
|
self._sched.shutdown()
|
|
|
|
logger.debug('Finished.')
|
|
|
|
|
|
|
|
def join(self, signal_num=None, stack_frame=None):
|
|
|
|
logger.debug('Shutting down worker gracefully.')
|
2013-11-15 20:50:20 +00:00
|
|
|
self._stop.set()
|