Implement fact receiver thread worker

Optionally allow processing of fact receiver messages in a worker
thread.  This works around an issue where data could take a while to
page into Mongo and cross the zmq socket timeout.
This commit is contained in:
Matthew Jones 2015-08-28 10:59:52 -04:00
parent 64b65edf2c
commit 2f39393cf3
2 changed files with 8 additions and 3 deletions

View File

@ -3,6 +3,7 @@
# Python
import logging
from threading import Thread
from datetime import datetime
# Django
@ -76,14 +77,18 @@ class FactCacheReceiver(object):
(fact_obj, version_obj) = Fact.add_fact(self.timestamp, facts, host, module)
logger.info('Created new fact <fact, fact_version> <%s, %s>' % (fact_obj.id, version_obj.id))
def run_receiver(self):
def run_receiver(self, use_processing_threads=True):
with Socket('fact_cache', 'r') as facts:
for message in facts.listen():
if 'host' not in message or 'facts' not in message or 'date_key' not in message:
logger.warn('Received invalid message %s' % message)
continue
logger.info('Received message %s' % message)
self.process_fact_message(message)
if use_processing_threads:
wt = Thread(target=self.process_fact_message, args=(message,))
wt.start()
else:
self.process_fact_message(message)
class Command(NoArgsCommand):
'''

View File

@ -142,7 +142,7 @@ class RunFactCacheReceiverUnitTest(BaseTest, MongoDBRequired):
receiver = FactCacheReceiver()
receiver.process_fact_message = MagicMock(name='process_fact_message')
receiver.run_receiver()
receiver.run_receiver(use_processing_threads=False)
receiver.process_fact_message.assert_called_once_with(TEST_MSG)