diff --git a/awx/main/management/commands/run_fact_cache_receiver.py b/awx/main/management/commands/run_fact_cache_receiver.py index 1526062c47..29742cae3e 100644 --- a/awx/main/management/commands/run_fact_cache_receiver.py +++ b/awx/main/management/commands/run_fact_cache_receiver.py @@ -4,7 +4,6 @@ import logging from django.core.management.base import NoArgsCommand -from django.utils.timezone import now from awx.main.models import * # noqa from awx.main.socket import Socket @@ -21,16 +20,23 @@ class FactCacheReceiver(object): 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=now())) - host_collection.insert(facts) + 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: + if 'host' not in message or 'facts' not in message or 'date_key' not in message: continue self.process_fact_message(message) diff --git a/awx/plugins/fact_caching/tower.py b/awx/plugins/fact_caching/tower.py index 73a44b33a0..0d9a5297b5 100755 --- a/awx/plugins/fact_caching/tower.py +++ b/awx/plugins/fact_caching/tower.py @@ -30,6 +30,7 @@ # POSSIBILITY OF SUCH DAMAGE. import sys +import datetime from ansible import constants as C from ansible.cache.base import BaseCacheModule @@ -45,6 +46,7 @@ class CacheModule(BaseCacheModule): # This is the local tower zmq connection self._tower_connection = C.CACHE_PLUGIN_CONNECTION + self.date_key = datetime.datetime.utcnow() try: self.context = zmq.Context() self.socket = self.context.socket(zmq.REQ) @@ -57,7 +59,7 @@ class CacheModule(BaseCacheModule): return {} # Temporary until we have some tower retrieval endpoints def set(self, key, value): - self.socket.send_json(dict(host=key, facts=value)) + self.socket.send_json(dict(host=key, facts=value, date_key=self.date_key)) self.socket.recv() def keys(self):