Exemplo n.º 1
0
    def handle(self, message):
        try:
            # Convert message from json.
            request = Message.from_json(message[1])
            response = None

            # Check target.
            self.logger.trace('Got request: %s' % request)
            target = request.get('target')
            if target is None:
                self.logger.error('Wrong target: %s in request: %s' %
                                  (target, request))

            # Assign message id.
            request['message_id'] = str(uuid.uuid1())

            # Preprocess request.
            try:
                target_preprocessor = self.get_target_preprocessor(target)
                if target_preprocessor is not None:
                    self.logger.debug(
                        'Preprocessing target %s using preprocessor %s' %
                        (target, target_preprocessor))
                    target_preprocessor(request)
            except URBException, ex:
                self.logger.error(ex)
            except Exception, ex:
                errMsg = 'Cannot preprocess target %s: %s' % (target, ex)
                self.logger.error(errMsg)
                self.logger.exception(ex)
Exemplo n.º 2
0
    def manage(self):
        """ Managment thread. """
        self.logger.debug('Entering retry channel management loop')
        while True:
            if not self.__manage:
                break
            self.__thread_event.clear()

            (name, message_list) = self.retry_channel.read_timestamp_range()
            self.logger.debug('Message list: %s', message_list)
            for message_string in message_list:
                try:
                    message = Message.from_json(message_string)
                    retry_count = message.get('retry_count', 0)
                    self.logger.debug('Retry count is %s for message %s' %
                                      (retry_count, message))
                    if retry_count > self.max_retry_count:
                        self.logger.warn(
                            'Max. retry count exceded for message %s' %
                            message)
                        continue

                    self.channel.write(message.to_json())
                except Exception, ex:
                    self.logger.error(
                        'Error writing message (%s) to channel %s: %s' %
                        (message_string, self.channel.name, ex))

                # Must allow other greenlets to run.
                gevent.sleep(RetryManager.GREENLET_SLEEP_PERIOD_IN_SECONDS)

            # Monitoring thread sleep
            self.__thread_event.wait(
                RetryManager.MANAGER_SLEEP_PERIOD_IN_SECONDS)