示例#1
0
文件: consumer.py 项目: mSOHU/arkham
    def get_service(cls, service_name, force=False):
        if force:
            return ArkhamService.get_instance(service_name)

        instance = cls.service_instances.get(service_name)
        if not instance:
            instance = cls.service_instances[service_name] = ArkhamService.get_instance(service_name)

        return instance
示例#2
0
文件: consumer.py 项目: mSOHU/arkham
    def __init__(self, worker_cls, consumer, config_path, consumer_name):
        self.consumer = consumer
        self.logger = self.consumer.logger = self.consumer.logger or LOGGER
        self.logger.setLevel(getattr(logging, self.consumer.log_level))

        # setup flags
        self.inactivate_state = False
        self.stop_flag = False
        self.last_slept = 0
        self.last_activity = time.time()

        # for IDE
        self.generator = []

        self.logger.info('Using %s: %r', worker_cls.__name__, worker_cls)
        self.worker = worker_cls(self)

        ArkhamService.init_config(config_path)
        self.subscriber = ArkhamService.get_instance(consumer_name)

        self.setup_signal_handler()
        if self.consumer.enable_healthy_checker:
            self.setup_healthy_checker()

        self.callbacks = collect_period_callbacks(self.consumer)
示例#3
0
    def start(self, consumer_name):
        subscriber = ArkhamService.get_instance(consumer_name)
        for method, properties, body in subscriber.consume():
            try:
                message = self.parse_message(method, body, properties)
            except (ValueError, TypeError, AssertionError) as err:
                LOGGER.error('Invalid request in RPC queue. [%s], %r', err.message, properties.__dict__)
                subscriber.reject(method.delivery_tag, requeue=False)
                continue

            try:
                result = self.handle_message(message)
                reply = {
                    'status': 'ok',
                    'message': 'success',
                    'data': result,
                }
            except Exception:
                reply = {
                    'status': 'fail',
                    'message': traceback.format_exc(),
                    'data': None,
                }

            try:
                subscriber.channel.basic_publish('', properties.reply_to, json.dumps(reply))
                subscriber.acknowledge(method.delivery_tag)
            except Exception as err:
                subscriber.reject(method.delivery_tag)
                LOGGER.exception('Exception occurs when trying to reply RPC. %r, %r', err, reply)
示例#4
0
    def __init__(self, consumer, config_path, consumer_name):
        self.consumer = consumer
        self.logger = self.consumer.logger = self.consumer.logger or LOGGER
        self.logger.setLevel(getattr(logging, self.consumer.log_level))

        # setup flags
        self.inactivate_state = False
        self.stop_flag = False
        self.last_activity = time.time()

        # for IDE
        self.generator = []

        # early initialize worker so gevent can patch in time.
        assert self.consumer.worker_class in self.WORKER_CLASSES, (
            "Unsupported worker class: `%s`" % self.consumer.worker_class
        )
        worker_class = self.WORKER_CLASSES[self.consumer.worker_class]
        self.logger.info("Using %s worker: %r", self.consumer.worker_class, worker_class)
        self.worker = worker_class(self)

        ArkhamService.init_config(config_path)
        self.subscriber = ArkhamService.get_instance(consumer_name)

        self.setup_signal_handler()
        self.setup_healthy_checker()

        self.callbacks = collect_period_callbacks(self.consumer)