Esempio n. 1
0
class ScheduleModule(ScheduleBase):
    """
    Scheduler module for keeping the cluster index up to date.
    """
    def __init__(self):
        super(ScheduleModule, self).__init__(__name__)
        
        # Cache manager
        self.cache = CacheManager()
        
    def _worker(self):
        """
        Internal method for running the cache manager.
        """
        while True:
            self.log.info('Refreshing database cache')
            
            # Build a list of ACL objects
            acl_objects = list(DBAuthACLObjects.objects.all().values())
            
            # Process each ACL object type
            for acl_object in acl_objects:
                obj_type   = acl_object['type']
                
                # Get an instance of the object class
                obj_mod    = importlib.import_module(acl_object['obj_mod'])
                obj_class  = getattr(obj_mod, acl_object['obj_cls'])
                obj_key    = acl_object['obj_key']
                
                # Process and cache each object
                for obj_details in list(obj_class.objects.all().values()):
                    obj_id = obj_details[obj_key]
                    
                    # Update the cache
                    self.cache.save_object(obj_type, obj_id)
            
            # Rebuild cache every minute
            self.time.sleep(60)
        
    def launch(self):
        """
        Worker method for running the the schedule indexer.
        """
        
        # Start the cache worker
        try:
            self._worker()
            
        # Critical error when running cache scheduler
        except Exception as e:
            self.log.exception('Error when running cache scheduler: %s' % str(e))
Esempio n. 2
0
 def __init__(self):
     
     # Configuration / logger objects
     self.conf  = config.parse()
     self.log   = logger.create(__name__, self.conf.utils.log)
     
     # Cache manager
     self.cache = CacheManager()
Esempio n. 3
0
class ObjectsManager(object):
    """
    API class used to retrieve object details. If cache retrieval is enabled, the
    object details will be pulled from the database cache table.
    """
    def __init__(self):
        
        # Configuration / logger objects
        self.conf  = config.parse()
        self.log   = logger.create(__name__, self.conf.utils.log)
        
        # Cache manager
        self.cache = CacheManager()
        
    def _get_from_model(self, obj_type, obj_id, values={}, filters={}):
        """
        Retrieve object details directly from the model.
        
        @param obj_type: The type of object to retrieve
        @type  obj_type: str
        @param obj_id:   The ID of the object to retrieve
        @type  obj_id:   str
        @param values:   Extra parameters to pass to the values QuerySet method
        @type  values:   dict
        @param filters:  Extra filter parameters
        @type  filters:  dict
        """
        self.log.info('Retrieving object data from model: type=%s, id=%s' % (obj_type, repr(obj_id)))
        
        # Get the ACL object definition
        acl_object  = ACLObjects.get(obj_type)
        
        # Get an instance of the object class
        obj_mod     = importlib.import_module(getattr(acl_object, 'obj_mod'))
        obj_class   = getattr(obj_mod, getattr(acl_object, 'obj_cls'))
        obj_key     = getattr(acl_object, 'obj_key')
        
        # Create the object filter
        obj_filter  = {}
        
        # If retrieving a single object
        if obj_id:
            obj_filter[obj_key] = obj_id
        
        # Create the query object
        query_obj = obj_class.objects
        
        # If an object filter is defined
        if obj_filter:
            self.log.info('Applying object filters: %s' % json.dumps(obj_filter))
            query_obj = query_obj.filter(**obj_filter)
            
        # If a values filter is defined
        if filters:
            self.log.info('Applying value filters: %s' % json.dumps(filters))
            query_obj = query_obj.filter(**filters)
        
        # If no filters were defined
        if not obj_filter and not filters:
            self.log.info('No filters defined, retrieving all objects')
            query_obj = query_obj.all()
        
        # Log the constructed query object
        self.log.info('Constructed object query: %s' % str(query_obj))
        
        # Attempt to retrieve the object
        obj_details = list(query_obj.values())
        
        # Log the retrieved details
        log_data = json.dumps(obj_details, cls=DjangoJSONEncoder)
        self.log.info('Retrieved object details: length=%s, data=%s' % (len(log_data), (log_data[:75] + '...') if len(log_data) > 75 else log_data))
        
        # If the object doesn't exist
        if len(obj_details) == 0:
            return None
        
        # Return the object details
        if not obj_id:
            return obj_details
        return obj_details[0]
        
    def get(self, obj_type, obj_id=None, cache=True, values={}, filters={}):
        """
        Retrieve details for an API object.
        
        @param obj_type: The type of object to retrieve
        @type  obj_type: str
        @param obj_id:   The ID of the object to retrieve
        @type  obj_id:   str
        @param cache:    Enable or disable retrieval from the database cache table
        @type  cache:    bool
        @param values:   Extra parameters to pass to the values QuerySet method
        @type  values:   dict
        @param filters:  Extra filter parameters
        @type  filters:  dict
        """
        
        # If the ACL object exists
        if len(ACLObjects.get_values(obj_type)) > 0:
            self.log.info('Retrieving database object: type=%s, id=%s, cache=%s' % (obj_type, repr(obj_id), repr(cache)))
            
            # If retrieving from the database cache table and caching not explicitly disabled
            if cache and not (self.conf.server.caching == False):
            
                # Look for object details in the cache
                cached_obj = self.cache.get_object(obj_type, obj_id, filters=filters)
                
                # If cached data found
                if cached_obj:
                    self.log.info('Cached data found for object: type=%s, id=%s' % (obj_type, repr(obj_id)))
                    return cached_obj
                
                # No cached data found, retrieve directly from the object model
                return self._get_from_model(obj_type, obj_id, values=values, filters=filters)
                
            # If querying directly from the database
            else:
                return self._get_from_model(obj_type, obj_id, values=values, filters=filters)
            
        # Invalid ACL object type
        else:
            self.log.error('Failed to retrieve object <%s> of type <%s>, object type not found' % (obj_id, obj_type))
            
            # Return an empty result
            return None
Esempio n. 4
0
 def __init__(self):
     super(ScheduleModule, self).__init__(__name__)
     
     # Cache manager
     self.cache = CacheManager()