예제 #1
0
    def _get_panoptes_logger(self):
        """
        Returns the logger to be used by the context

        The method attempts to guess the name of the calling module based on introspection of the stack

        Returns:
            logger(logger): A Python logger subsystem logger

        Raises:
            PanoptesContextError: This exception is raised is any errors happen trying to instantiate the logger
        """
        self.__logger.info(u'Attempting to get logger')
        try:
            module = get_calling_module_name()
            logger = self.__rootLogger.getChild(module)
            self.__logger.info(u'Got logger for module %s' % module)
            return logger
        except Exception as e:
            raise PanoptesContextError(u'Could not get logger: %s' % str(e))
예제 #2
0
    def __init__(self, context, path, timeout, retries=1, identifier=None):
        """
        Creates and maintains state for a lock

        Args:
            path (str): A '/' separated path for the lock
            timeout (int): in seconds. Must be a positive integer
            retries (int): how many times to try before giving up. Zero implies try forever
            identifier (str): Name to use for this lock contender. This can be useful for querying \
            to see who the current lock contenders are

        Returns:
            PanoptesLock: lock
        """
        assert PanoptesContextValidators.valid_panoptes_context(
            context), u'context must be a valid PanoptesContext'
        assert PanoptesValidators.valid_nonempty_string(path) and re.search(r"^/\S+", path), \
            u'path must be a non-empty string that begins with /'
        assert PanoptesValidators.valid_nonzero_integer(
            timeout), u'timeout must be a positive integer'
        assert PanoptesValidators.valid_positive_integer(
            retries), u'retries must be a non-negative integer'
        assert PanoptesValidators.valid_nonempty_string(
            identifier), u'identifier must be a non-empty string'

        self._context = context
        self._logger = self._context.logger
        self._path = path
        self._timeout = timeout
        self._retries = retries
        self._identifier = identifier
        self._lock = None
        self._locked = False
        self._calling_module = get_calling_module_name(3)

        self._get_lock()
예제 #3
0
    This method creates a Panoptes Context and the Celery Instance for the Enrichment Plugin Agent

    Returns:
        None
    """
    global panoptes_context, celery

    try:
        panoptes_context = PanoptesEnrichmentAgentContext()
    except Exception as e:
        sys.exit(u'Could not create a Panoptes Context: %s' % (str(e)))

    logger = panoptes_context.logger
    logger.info(u'Attempting to start Celery application')

    celery_config = PanoptesCeleryConfig(
        const.ENRICHMENT_PLUGIN_AGENT_CELERY_APP_NAME)

    try:
        celery = PanoptesCeleryInstance(panoptes_context, celery_config).celery
    except Exception as exp:
        sys.exit(u'Could not instantiate Celery application: %s' % str(exp))
    else:
        logger.info(u'Started Celery application: %s' % celery)


if get_calling_module_name() == const.CELERY_LOADER_MODULE or \
        inspect_calling_module_for_name('celery'):  # pragma: no cover
    faulthandler.enable()
    start_enrichment_plugin_agent()
예제 #4
0
    def get_lock(self,
                 path,
                 timeout,
                 retries=1,
                 identifier=None,
                 listener=None):
        """
        A wrapper around the kazoo library lock

        Args:
            path (str): A '/' separated path for the lock
            timeout (int): in seconds. Must be a positive integer
            retries (int): how many times to try before giving up. Zero implies try forever
            identifier (str): Name to use for this lock contender. This can be useful for querying \
            to see who the current lock contenders are
            listener (callable): The callable to use to handle Zookeeper state changes

        Returns:
            kazoo.recipe.lock.Lock: lock
        """
        assert PanoptesValidators.valid_nonempty_string(path) and re.search("^/\S+", path), \
            u'path must be a non-empty string that begins with /'
        assert PanoptesValidators.valid_nonzero_integer(
            timeout), u'timeout must be a positive integer'
        assert PanoptesValidators.valid_positive_integer(
            retries), u'retries must be a non-negative integer'
        assert PanoptesValidators.valid_nonempty_string(
            identifier), u'identifier must be a non-empty string'
        assert (
            not listener) or callable(listener), u'listener must be a callable'

        logger = self.logger
        calling_module = get_calling_module_name(2)
        logger.info(u"Creating lock for module: " + calling_module +
                    u" with lock parameters: "
                    u"path=" + path + u",timeout=" + str(timeout) +
                    u",retries=" + str(retries) + u","
                    u"identifier=" + identifier)
        try:
            lock = self.zookeeper_client.Lock(path, identifier)
        except Exception as e:
            logger.error(u'Failed to create lock object: %s' % str(e))
            return None

        if retries == 0:
            while True:
                logger.info(
                    u'Trying to acquire lock with client id "%s" under path %s. Other contenders: %s. '
                    % (identifier, path, lock.contenders()))
                try:
                    lock.acquire(timeout=timeout)
                except LockTimeout:
                    logger.info(
                        u'Timed out after %d seconds trying to acquire lock.  Retrying.'
                        % timeout)
                except Exception as e:
                    logger.info(u'Error in acquiring lock: %s.  Retrying.' %
                                str(e))
                if lock.is_acquired:
                    break
        else:
            tries = 0
            while tries < retries:
                logger.info(
                    u'Trying to acquire lock with client id "%s" under path %s. Other contenders: %s. '
                    % (identifier, path, lock.contenders()))
                try:
                    lock.acquire(timeout=timeout)
                except LockTimeout:
                    logger.info(
                        u'Timed out after %d seconds trying to acquire lock. Retrying %d more times'
                        % (timeout, retries - tries))
                except Exception as e:
                    logger.info(
                        u'Error in acquiring lock: %s.  Retrying %d more times'
                        % (str(e), (retries - tries)))
                if lock.is_acquired:
                    break
                tries += 1
            if not lock.is_acquired:
                logger.warn(u'Unable to acquire lock after %d tries' % retries)

        if lock.is_acquired:
            logger.info(u'Lock acquired. Other contenders: %s' %
                        lock.contenders())

            if listener:
                self.zookeeper_client.add_listener(listener)

            return lock