Azure 函数在引发异常时不会出现死字

问题描述 投票:0回答:1

我有一个 Azure Functions python 函数,它调用引发异常的方法。当引发异常时,我仍然知道该函数已成功执行,我也尝试在 main 中引发相同的异常,但我面临着同样的问题:

def main(msg: func.ServiceBusMessage) -> None:
return testable_main(msg=msg)


@failsafe_logger(response_type=ResponseType.AZURE_SERVICE_BUS)
def testable_main(
    msg: func.ServiceBusMessage,
    handling_ut=None,
) -> None:
    mapper = dict(
        CREATE=dict(
            method=create_subscription,
            params=dict(msg=msg),
        ),
        UPDATE=dict(
            method=update_subscription,
            params=dict(msg=msg),
        ),
        DELETE=dict(
            method=delete_subscription,
            params=dict(msg=msg),
        ),
    )
    product_id = json.loads(msg.get_body().decode("utf-8"))[
        "productId"
    ]
    if product_id == os.environ["PRODUCT_ID"]:
        event_type = json.loads(msg.get_body().decode("utf-8"))[
            "eventType"
        ]
        (handling_ut or mapper[event_type]["method"])(
        **mapper[event_type]["params"]
        )

我尝试了一种变体,重新引发 main 中的错误,但它仍然不起作用:

try:
    mapper = dict(
        CREATE=dict(
            method=create_subscription,
            params=dict(msg=msg),
        ),
        UPDATE=dict(
            method=update_subscription,
            params=dict(msg=msg),
        ),
        DELETE=dict(
            method=delete_subscription,
            params=dict(msg=msg),
        ),
    )
    product_id = json.loads(msg.get_body().decode("utf-8"))[
        "productId"
    ]
    if product_id == os.environ["PRODUCT_ID"]:
        event_type = json.loads(msg.get_body().decode("utf-8"))[
            "eventType"
        ]
        (handling_ut or mapper[event_type]["method"])(
            **mapper[event_type]["params"]
        )
except CustomError as e:
    logging.error(f"Custom error: {e}")
    raise  # Re-raise the same exception
except Exception as e:
    logging.error(f"Unexpected error: {e}")
    raise  # Re-raise any unexpected exceptions

这在过去(几个月前)有效,知道我做错了什么或者Azure方面可能发生了什么变化吗? 谢谢!

python azure azure-functions
1个回答
0
投票

看起来,python 的服务总线触发器只支持 autoComplete = true。这意味着如果函数执行成功完成,触发器函数会自动完成消息,否则放弃消息。函数中的异常会导致运行时在后台调用abandonAsync。如果没有发生异常,则在后台调用completeAsync。此属性仅在 Azure Functions 2.x 及更高版本中可用。

这里是有关 python azure 函数服务总线触发器的一些信息,请参阅

configuration
部分。

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-service-bus-trigger?tabs=python-v1%2Cisolated-process%2Cnodejs-v4%2Cextensionv5&pivots=programming-语言-python

我的建议是只在函数中抛出异常而不做任何其他事情。然后查看 Azure 门户中的服务总线队列,查看 DeliveryCount 的计数器是否增加。当DeliveryCount达到10次时,消息应该显示在死信选项卡中。可能是您的旧代码在 v1 中,但现在在 v2 中,设置中的一些行为发生了变化。

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.