示例#1
0
文件: amqp.py 项目: xdmiodz/tomodachi
            async def routine_func(*a: Any, **kw: Any) -> Any:
                try:
                    if not message_envelope and len(values.args[1:]) and len(
                            values.args[2:]) == len(a):
                        routine = func(*(obj, message, *a))
                    elif not message_envelope and len(values.args[1:]) and len(
                            merge_dicts(kwargs, kw)):
                        routine = func(*(obj, message, *a),
                                       **merge_dicts(kwargs, kw))
                    elif len(merge_dicts(kwargs, kw)):
                        routine = func(*(obj, *a), **merge_dicts(kwargs, kw))
                    elif len(values.args[1:]):
                        routine = func(*(obj, message, *a), **kw)
                    else:
                        routine = func(*(obj, *a), **kw)
                except (Exception, asyncio.CancelledError, BaseException) as e:
                    logging.getLogger("exception").exception(
                        "Uncaught exception: {}".format(str(e)))
                    if issubclass(
                            e.__class__,
                        (AmqpInternalServiceError,
                         AmqpInternalServiceErrorException,
                         AmqpInternalServiceException),
                    ):
                        if message_key:
                            del context["_amqp_received_messages"][message_key]
                        await cls.channel.basic_client_nack(delivery_tag)
                        return
                    await cls.channel.basic_client_ack(delivery_tag)
                    return

                if inspect.isawaitable(routine):
                    try:
                        return_value = await routine
                    except (Exception, asyncio.CancelledError,
                            BaseException) as e:
                        logging.getLogger("exception").exception(
                            "Uncaught exception: {}".format(str(e)))
                        if issubclass(
                                e.__class__,
                            (AmqpInternalServiceError,
                             AmqpInternalServiceErrorException,
                             AmqpInternalServiceException),
                        ):
                            if message_key:
                                del context["_amqp_received_messages"][
                                    message_key]
                            await cls.channel.basic_client_nack(delivery_tag)
                            return
                        await cls.channel.basic_client_ack(delivery_tag)
                        return
                else:
                    return_value = routine

                await cls.channel.basic_client_ack(delivery_tag)
                return return_value
示例#2
0
            async def routine_func(*a: Any, **kw: Any) -> Any:
                try:
                    if not message_protocol and len(values.args[1:]):
                        routine = func(*(obj, message, *a))
                    elif len(merge_dicts(kwargs, kw)):
                        routine = func(*(obj, *a), **merge_dicts(kwargs, kw))
                    elif len(values.args[1:]):
                        routine = func(*(obj, message, *a), **kw)
                    else:
                        routine = func(*(obj, *a), **kw)
                except Exception as e:
                    logging.getLogger('exception').exception(
                        'Uncaught exception: {}'.format(str(e)))
                    if issubclass(e.__class__,
                                  (AWSSNSSQSInternalServiceError,
                                   AWSSNSSQSInternalServiceErrorException,
                                   AWSSNSSQSInternalServiceException)):
                        if message_key:
                            del context['_aws_sns_sqs_received_messages'][
                                message_key]
                        return
                    await cls.delete_message(cls, receipt_handle, queue_url,
                                             context)
                    return

                if isinstance(routine, Awaitable):
                    try:
                        return_value = await routine
                    except Exception as e:
                        logging.getLogger('exception').exception(
                            'Uncaught exception: {}'.format(str(e)))
                        if issubclass(e.__class__,
                                      (AWSSNSSQSInternalServiceError,
                                       AWSSNSSQSInternalServiceErrorException,
                                       AWSSNSSQSInternalServiceException)):
                            if message_key:
                                del context['_aws_sns_sqs_received_messages'][
                                    message_key]
                            return
                        await cls.delete_message(cls, receipt_handle,
                                                 queue_url, context)
                        return
                else:
                    return_value = routine

                await cls.delete_message(cls, receipt_handle, queue_url,
                                         context)
                return return_value
示例#3
0
            async def routine_func(*a: Any, **kw: Any) -> Any:
                try:
                    if not message_protocol and len(values.args[1:]):
                        routine = func(*(obj, message, *a))
                    elif len(merge_dicts(kwargs, kw)):
                        routine = func(*(obj, *a), **merge_dicts(kwargs, kw))
                    elif len(values.args[1:]):
                        routine = func(*(obj, message, *a), **kw)
                    else:
                        routine = func(*(obj, *a), **kw)
                except Exception as e:
                    logging.getLogger('exception').exception(
                        'Uncaught exception: {}'.format(str(e)))
                    if issubclass(e.__class__,
                                  (AmqpInternalServiceError,
                                   AmqpInternalServiceErrorException,
                                   AmqpInternalServiceException)):
                        if message_key:
                            del context['_amqp_received_messages'][message_key]
                        await cls.channel.basic_client_nack(delivery_tag)
                        return
                    await cls.channel.basic_client_ack(delivery_tag)
                    return

                if isinstance(routine, Awaitable):
                    try:
                        return_value = await routine
                    except Exception as e:
                        logging.getLogger('exception').exception(
                            'Uncaught exception: {}'.format(str(e)))
                        if issubclass(e.__class__,
                                      (AmqpInternalServiceError,
                                       AmqpInternalServiceErrorException,
                                       AmqpInternalServiceException)):
                            if message_key:
                                del context['_amqp_received_messages'][
                                    message_key]
                            await cls.channel.basic_client_nack(delivery_tag)
                            return
                        await cls.channel.basic_client_ack(delivery_tag)
                        return
                else:
                    return_value = routine

                await cls.channel.basic_client_ack(delivery_tag)
                return return_value
示例#4
0
 async def routine_func(
     *a: Any, **kw: Any
 ) -> Union[str, bytes, Dict, List, Tuple, web.Response, Response]:
     routine = func(*(obj, request, *a), **merge_dicts(kwargs, kw))
     return_value = (await routine) if isinstance(
         routine, Awaitable
     ) else routine  # type: Union[str, bytes, Dict, List, Tuple, web.Response, Response]
     return return_value
示例#5
0
            async def routine_func(*a: Any, **kw: Any) -> Any:
                if not message_envelope and len(values.args[1:]) and len(
                        values.args[2:]) == len(a):
                    routine = func(*(obj, message, *a))
                elif not message_envelope and len(values.args[1:]) and len(
                        merge_dicts(kwargs, kw)):
                    routine = func(*(obj, message, *a),
                                   **merge_dicts(kwargs, kw))
                elif len(merge_dicts(kwargs, kw)):
                    routine = func(*(obj, *a), **merge_dicts(kwargs, kw))
                elif len(values.args[1:]):
                    routine = func(*(obj, message, *a), **kw)
                else:
                    routine = func(*(obj, *a), **kw)

                if inspect.isawaitable(routine):
                    return_value = await routine
                else:
                    return_value = routine

                await cls.channel.basic_client_ack(delivery_tag)
                return return_value
示例#6
0
def parse_config_files(config_files: List[str]) -> Optional[Dict]:
    if not config_files:
        return None
    if isinstance(config_files, str):
        config_files = [config_files]

    configuration = {}  # type: Dict

    for config_file in config_files:
        with open(config_file) as f:
            data = f.read()
            json_data = ujson.loads(data)

            configuration = merge_dicts(configuration, json_data)

    return configuration
示例#7
0
        async def _func(obj: Any, request: web.Request, *a: Any,
                        **kw: Any) -> None:
            websocket = web.WebSocketResponse()  # type: ignore

            request_ip = RequestHandler.get_request_ip(request, context)
            try:
                await websocket.prepare(request)
            except Exception:
                try:
                    await websocket.close()
                except Exception:
                    pass

                if access_log:
                    logging.getLogger('transport.http').info(
                        '[{}] {} {} "CANCELLED {}{}" {} "{}" {}'.format(
                            RequestHandler.colorize_status('websocket',
                                                           101), request_ip,
                            '"{}"'.format(request._cache['auth'].login.replace(
                                '"', '')) if request._cache.get('auth')
                            and getattr(request._cache.get('auth'), 'login',
                                        None) else '-', request.path,
                            '?{}'.format(request.query_string)
                            if request.query_string else '',
                            request._cache.get('websocket_uuid', ''),
                            request.headers.get('User-Agent',
                                                '').replace('"', ''), '-'))

                return

            context['_http_open_websockets'] = context.get(
                '_http_open_websockets', [])
            context['_http_open_websockets'].append(websocket)

            if access_log:
                logging.getLogger('transport.http').info(
                    '[{}] {} {} "OPEN {}{}" {} "{}" {}'.format(
                        RequestHandler.colorize_status('websocket',
                                                       101), request_ip,
                        '"{}"'.format(request._cache['auth'].login.replace(
                            '"', '')) if request._cache.get('auth')
                        and getattr(request._cache.get('auth'), 'login', None)
                        else '-', request.path,
                        '?{}'.format(request.query_string)
                        if request.query_string else '',
                        request._cache.get('websocket_uuid', ''),
                        request.headers.get('User-Agent',
                                            '').replace('"', ''), '-'))

            result = compiled_pattern.match(request.path)
            values = inspect.getfullargspec(func)
            kwargs = {
                k: values.defaults[i]
                for i, k in enumerate(values.args[len(values.args) -
                                                  len(values.defaults):])
            } if values.defaults else {}
            if result:
                for k, v in result.groupdict().items():
                    kwargs[k] = v

            try:
                routine = func(*(obj, websocket, *a),
                               **merge_dicts(kwargs, kw))
                callback_functions = (await routine) if isinstance(
                    routine, Awaitable
                ) else routine  # type: Optional[Union[Tuple, Callable]]
            except Exception as e:
                logging.getLogger('exception').exception(
                    'Uncaught exception: {}'.format(str(e)))
                try:
                    await websocket.close()
                except Exception:
                    pass

                try:
                    context['_http_open_websockets'].remove(websocket)
                except Exception:
                    pass

                if access_log:
                    logging.getLogger('transport.http').info(
                        '[{}] {} {} "{} {}{}" {} "{}" {}'.format(
                            RequestHandler.colorize_status('websocket',
                                                           500), request_ip,
                            '"{}"'.format(request._cache['auth'].login.replace(
                                '"', '')) if request._cache.get('auth')
                            and getattr(request._cache.get('auth'), 'login',
                                        None) else '-',
                            RequestHandler.colorize_status('ERROR', 500),
                            request.path, '?{}'.format(request.query_string)
                            if request.query_string else '',
                            request._cache.get('websocket_uuid', ''),
                            request.headers.get('User-Agent',
                                                '').replace('"', ''), '-'))

                return

            _receive_func = None
            _close_func = None

            if callback_functions and isinstance(callback_functions, tuple):
                try:
                    _receive_func, _close_func = callback_functions
                except ValueError:
                    _receive_func, = callback_functions
            elif callback_functions:
                _receive_func = callback_functions

            try:
                async for message in websocket:
                    if message.type == WSMsgType.TEXT:
                        if _receive_func:
                            try:
                                await _receive_func(message.data)
                            except Exception as e:
                                logging.getLogger('exception').exception(
                                    'Uncaught exception: {}'.format(str(e)))
                    elif message.type == WSMsgType.ERROR:
                        if not context.get('log_level') or context.get(
                                'log_level') in ['DEBUG']:
                            ws_exception = websocket.exception()
                            if isinstance(ws_exception,
                                          (EofStream, RuntimeError)):
                                pass
                            elif isinstance(ws_exception, Exception):
                                logging.getLogger('exception').exception(
                                    'Uncaught exception: {}'.format(
                                        str(ws_exception)))
                            else:
                                logging.getLogger('transport.http').warning(
                                    'Websocket exception: "{}"'.format(
                                        ws_exception))
                    elif message.type == WSMsgType.CLOSED:
                        break  # noqa
            except Exception as e:
                pass
            finally:
                if _close_func:
                    try:
                        await _close_func()
                    except Exception as e:
                        logging.getLogger('exception').exception(
                            'Uncaught exception: {}'.format(str(e)))
                try:
                    await websocket.close()
                except Exception:
                    pass

                try:
                    context['_http_open_websockets'].remove(websocket)
                except Exception:
                    pass