diff --git a/inventory_provider/tasks/worker.py b/inventory_provider/tasks/worker.py index 4e1351c6974b7db511a12f0b65107167f5a8b1bd..0083aee763dbc9df0c52fd9d273b9938c051c316 100644 --- a/inventory_provider/tasks/worker.py +++ b/inventory_provider/tasks/worker.py @@ -253,24 +253,37 @@ def load_netconf_data(hostname): return etree.fromstring(netconf.decode('utf-8')) -def clear_cached_classifier_responses(hostname): +def clear_cached_classifier_responses(hostname=None): logger = logging.getLogger(__name__) - logger.debug( - 'removing cached classifier responses for %r' % hostname) + if hostname: + logger.debug( + 'removing cached classifier responses for %r' % hostname) + else: + logger.debug('removing all cached classifier responses') + r = get_redis(InventoryTask.config) - for k in r.keys('classifier:cache:%s:*' % hostname): - r.delete(k) - # TODO: very inefficient ... but logically simplest at this point - for k in r.keys('classifier:peer-cache:*'): - value = r.get(k.decode('utf-8')) - if not value: - # deleted in another thread - continue - value = json.loads(value.decode('utf-8')) - interfaces = value.get('interfaces', []) - if hostname in [i['interface']['router'] for i in interfaces]: - r.delete(k) + def _hostname_keys(): + for k in r.keys('classifier:cache:%s:*' % hostname): + yield k + + # TODO: very inefficient ... but logically simplest at this point + for k in r.keys('classifier:peer-cache:*'): + value = r.get(k.decode('utf-8')) + if not value: + # deleted in another thread + continue + value = json.loads(value.decode('utf-8')) + interfaces = value.get('interfaces', []) + if hostname in [i['interface']['router'] for i in interfaces]: + yield k + + def _all_keys(): + return r.keys('classifier:*') + + keys_to_delete = _hostname_keys() if hostname else _all_keys() + for k in keys_to_delete: + r.delete(k) def _refresh_peers(hostname, key_base, peers): @@ -386,7 +399,7 @@ def reload_router_config(self, hostname): refresh_vpn_rr_peers(hostname, netconf_doc) refresh_interface_address_lookups(hostname, netconf_doc) refresh_juniper_interface_list(hostname, netconf_doc) - clear_cached_classifier_responses(hostname) + # clear_cached_classifier_responses(hostname) # load snmp indexes community = juniper.snmp_community_string(netconf_doc) @@ -403,6 +416,8 @@ def reload_router_config(self, hostname): }) snmp_refresh_interfaces.apply(args=[hostname, community]) + clear_cached_classifier_responses(None) + logger.debug('<<< reload_router_config') return { @@ -453,7 +468,7 @@ def launch_refresh_cache_all(config): # juniper netconf & snmp data subtasks = [ update_equipment_locations.s(), - update_interface_statuses.s() + # update_interface_statuses.s() ] for hostname in _derive_router_hostnames(config): logger.debug(