Merge pull request #85 from matburt/fact_scans_and_caching

Fact scans and caching
This commit is contained in:
Matthew Jones
2015-02-25 13:50:48 -05:00
8 changed files with 197 additions and 2 deletions

View File

@@ -87,7 +87,7 @@ class CallbackReceiver(object):
queue_worker[2] = w
if workers_changed:
signal.signal(signal.SIGINT, shutdown_handler([p[2] for p in worker_queues] + [main_process]))
signal.signal(signal.SIGTERM, shutdown_handler([p[2] for p in worker_queues] + [main_process]))
signal.signal(signal.SIGTERM, shutdown_handler([p[2] for p in worker_queues] + [main_process]))
if not main_process.is_alive():
sys.exit(1)
time.sleep(0.1)
@@ -239,7 +239,7 @@ class Command(NoArgsCommand):
Save Job Callback receiver (see awx.plugins.callbacks.job_event_callback)
Runs as a management command and receives job save events. It then hands
them off to worker processors (see Worker) which writes them to the database
'''
'''
help = 'Launch the job callback receiver'
def handle_noargs(self, **options):

View File

@@ -0,0 +1,55 @@
# Copyright (c) 2015 Ansible, Inc.
# All Rights Reserved
import logging
from django.core.management.base import NoArgsCommand
from awx.main.models import * # noqa
from awx.main.socket import Socket
from pymongo import MongoClient
logger = logging.getLogger('awx.main.commands.run_fact_cache_receiver')
class FactCacheReceiver(object):
def __init__(self):
self.client = MongoClient('localhost', 27017)
def process_fact_message(self, message):
host = message['host']
facts = message['facts']
date_key = message['date_key']
host_db = self.client.host_facts
host_collection = host_db[host]
facts.update(dict(tower_host=host, datetime=date_key))
rec = host_collection.find({"datetime": date_key})
if rec.count():
this_fact = rec.next()
this_fact.update(facts)
host_collection.save(this_fact)
else:
host_collection.insert(facts)
def run_receiver(self):
with Socket('fact_cache', 'r') as facts:
for message in facts.listen():
print("Message received: " + str(message))
if 'host' not in message or 'facts' not in message or 'date_key' not in message:
continue
self.process_fact_message(message)
class Command(NoArgsCommand):
'''
blah blah
'''
help = 'Launch the Fact Cache Receiver'
def handle_noargs(self, **options):
fcr = FactCacheReceiver()
try:
fcr.run_receiver()
except KeyboardInterrupt:
pass