Exemplo n.º 1
0
        async def handler() -> None:
            kwargs = dict(original_kwargs)

            increase_execution_context_value(
                "scheduled_functions_current_tasks")
            increase_execution_context_value("scheduled_functions_total_tasks")
            try:
                routine = func(*(obj, ), **kwargs)
                if isinstance(routine, Awaitable):
                    await routine
            except Exception as e:
                logging.getLogger("exception").exception(
                    "Uncaught exception: {}".format(str(e)))
            decrease_execution_context_value(
                "scheduled_functions_current_tasks")
Exemplo n.º 2
0
        async def handler() -> None:
            increase_execution_context_value(
                "scheduled_functions_current_tasks")
            try:
                kwargs = dict(original_kwargs)

                increase_execution_context_value(
                    "scheduled_functions_total_tasks")

                routine = func(*(obj, ), **kwargs)
                if inspect.isawaitable(routine):
                    await routine

            except (Exception, asyncio.CancelledError) as e:
                logging.getLogger("exception").exception(
                    "Uncaught exception: {}".format(str(e)))
            except BaseException as e:
                logging.getLogger("exception").exception(
                    "Uncaught exception: {}".format(str(e)))
            finally:
                decrease_execution_context_value(
                    "scheduled_functions_current_tasks")
Exemplo n.º 3
0
        async def handler(payload: Any, delivery_tag: Any,
                          routing_key: str) -> Any:
            kwargs = dict(original_kwargs)

            message = payload
            message_uuid = None
            message_key = None
            if message_envelope:
                try:
                    parse_message_func = getattr(message_envelope,
                                                 "parse_message", None)
                    if parse_message_func:
                        if len(parser_kwargs):
                            message, message_uuid, timestamp = await parse_message_func(
                                payload, **parser_kwargs)
                        else:
                            message, message_uuid, timestamp = await parse_message_func(
                                payload)
                    if message_uuid:
                        if not context.get("_amqp_received_messages"):
                            context["_amqp_received_messages"] = {}
                        message_key = "{}:{}".format(message_uuid,
                                                     func.__name__)
                        if context["_amqp_received_messages"].get(message_key):
                            return
                        context["_amqp_received_messages"][
                            message_key] = time.time()
                        _received_messages = context["_amqp_received_messages"]
                        if (_received_messages
                                and isinstance(_received_messages, dict)
                                and len(_received_messages) > 100000):
                            context["_amqp_received_messages"] = {
                                k: v
                                for k, v in
                                context["_amqp_received_messages"].items()
                                if v > time.time() - 60
                            }

                    if _callback_kwargs:
                        for k, v in message.items():
                            if k in _callback_kwargs:
                                kwargs[k] = v
                        if "message" in _callback_kwargs and (not isinstance(
                                message, dict) or "message" not in message):
                            kwargs["message"] = message
                        if "routing_key" in _callback_kwargs and (
                                not isinstance(message, dict)
                                or "routing_key" not in message):
                            kwargs["routing_key"] = routing_key
                except (Exception, asyncio.CancelledError, BaseException) as e:
                    logging.getLogger("exception").exception(
                        "Uncaught exception: {}".format(str(e)))
                    if message is not False and not message_uuid:
                        await cls.channel.basic_client_ack(delivery_tag)
                    elif message is False and message_uuid:
                        pass  # incompatible envelope, should probably ack if old message
                    elif message is False:
                        await cls.channel.basic_client_ack(delivery_tag)
                    return
            else:
                if _callback_kwargs:
                    if "message" in _callback_kwargs:
                        kwargs["message"] = message
                    if "routing_key" in _callback_kwargs:
                        kwargs["routing_key"] = routing_key

                if len(values.args[1:]) and values.args[1] in kwargs:
                    del kwargs[values.args[1]]

            @functools.wraps(func)
            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

            increase_execution_context_value("amqp_current_tasks")
            increase_execution_context_value("amqp_total_tasks")
            return_value = await execute_middlewares(
                func, routine_func, context.get("message_middleware", []),
                *(obj, message, routing_key))
            decrease_execution_context_value("amqp_current_tasks")

            return return_value