如果 Azure 服务总线队列触发器函数失败,如何 return 将消息排队到 Azure 服务总线队列

How to return queue message to Azure Service Bus Queue if Azure Service Bus Queue Trigger Func Fails

我创建了一个 Azure 服务总线队列触发器 Python 函数 Visual Studio 代码 我想return 如果我的代码 失败 .[= 消息 服务总线队列 14=]

import requests
import azure.functions as func
from requests.exceptions import HTTPError

def main(msg: func.ServiceBusMessage):
    message = msg.get_body().decode("utf-8")

    url = "http://..."

    # My code
    try: 
         requests.post(url=url, params=message)
    except Exception as error:
         logging.error(error)
         # RETURN MESSAGE TO QUEUE HERE

我找到了一些关于名为 unlock()abandon() 的方法的信息,但我不知道如何实现它.以下是此类文档的链接:

我还发现如果函数失败,队列会自动将消息发送到队列,但是,我是否应该写一个 raise... 来发送异常功能?

此外,有没有办法将此消息 return 发送到 queue 并设置 schedule 稍后重试?

服务总线触发器python 函数以 PeekLock 模式运行。您不必调用 unlock() 和 abandon() 方法。检查 PeekLock behavior 描述:

The Functions runtime receives a message in PeekLock mode. It calls Complete on the message if the function finishes successfully, or calls Abandon if the function fails. If the function runs longer than the PeekLock timeout, the lock is automatically renewed as long as the function is running.