Azure服务总线功能使用Try-Except重试

2024-05-13 07:21:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我很抱歉,如果这可能会让人困惑,我会尽量清楚地表达出来。我有一个带有服务总线触发器的Azure函数

我有两个功能:

1-获取消息并使用部分消息调用另一个函数的服务总线触发器(运行orchestrator管道)。最大传递计数为3

2-使用SB消息中的某些详细信息发出post请求的函数(运行orchestrator管道)

我需要的是:我必须确保,如果POST请求失败3次,我会死信。为了显式地死信消息,我必须在第一个函数上抛出一个异常

我所做的:在第二个函数中,我返回了异常

    try:
      request = requests.post(url, auth=(orchestrator_username, orchestrator_pat), json = body)
      request.raise_for_status()
    except requests.exceptions.RequestException as e:
        return e

在第二个函数中调用该函数后,我检查了返回值,如果第一个函数中没有返回值,则引发valueError

    try:
        pipelineRun = run_orchestrator_pipeline(var)
        if pipelineRun is not None:
           raise ValueError("Failed to trigger orchestrator build.")
    except pipelineRun as v:
        logging.error(f"Function failed: {v}")

现在我的问题是:

  1. 有了这段代码,它确实运行了3次。当我逐行运行时,除了pipelinerunas v:之外,之后会直接转到post请求,再次重试。但是,它没有到达logging.info部分。为什么呢?我不太明白它为什么起作用

  2. 如果我将第一个函数中的except改为just except,它将一直运行到logging.info部分,但不再重试

任何帮助都将不胜感激


Tags: 函数消息管道requestloggingpostrequestsraise
1条回答
网友
1楼 · 发布于 2024-05-13 07:21:44

似乎您需要添加context.call_activity_with_retry(function, retry_options)

失败时自动重试

调用活动函数或子业务流程函数时,可以指定自动重试策略。以下示例尝试调用函数最多三次,每次重试之间等待5秒:


import azure.functions as func

import azure.durable_functions as df

 

def orchestrator_function(context: df.DurableOrchestrationContext):

    first_retry_interval_in_milliseconds = 5000

    max_number_of_attempts = 3

 

    retry_options = df.RetryOptions(first_retry_interval_in_milliseconds, max_number_of_attempts)

 

    yield context.call_activity_with_retry('FlakyFunction', retry_options)

 

main = df.Orchestrator.create(orchestrator_function)

您可以参考Automatic retry on failureCalling activity functions with retryReduce the max returns in the Service Bus and allow the "retry and wait" to be handled by the durable function

相关问题 更多 >