You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Azure Function Python版Timer Trigger重试机制运行失败问题

问题:Azure Python函数v2中Timer Trigger重试装饰器报错AttributeError

环境信息

  • Python 3.10
  • azure-functions 1.19.0
  • azure-core 1.30.1
  • azure-identity 1.16.0
  • Azure Python模型v2
  • Azure运行时版本4.34.1.1
  • 部署方式:Docker容器+Poetry,镜像基于mcr.microsoft.com/azure-functions/python:4-python3.10-core-tools

问题代码

from azure.functions import FunctionApp, TimerRequest, Context, AuthLevel
import logging

app = FunctionApp(http_auth_level=AuthLevel.ANONYMOUS)


@app.timer_trigger(schedule="*/1 * * * * *", arg_name="mytimer",
                   run_on_startup=False,
                   use_monitor=False)
@app.retry(strategy="fixed_delay", max_retry_count="3",
           delay_interval="00:00:01")
def mytimer(mytimer: TimerRequest, context: Context) -> None:
    logging.info(f'Current retry count: {context.retry_context.retry_count}')

    if context.retry_context.retry_count == \
            context.retry_context.max_retry_count:
        logging.info(
            f"Max retries of {context.retry_context.max_retry_count} for "
            f"function {context.function_name} has been reached")
    else:
        raise Exception("This is a retryable exception")

错误日志

[2024-06-05T03:09:22.466Z] Worker failed to index functions
[2024-06-05T03:09:22.466Z] Exception: AttributeError: 'FunctionApp' object has no attribute 'retry'

解决方案

  1. 升级azure-functions包版本:@app.retry装饰器是在azure-functions 1.20.0及以上版本才引入的,当前使用的1.19.0版本不支持该特性。

    • 修改Poetry的pyproject.toml文件,将azure-functions的版本指定为>=1.20.0,例如:
      azure-functions = "^1.20.0"
      
    • 执行命令更新依赖:poetry update azure-functions
    • 重新构建Docker镜像并启动函数。
  2. 修正代码参数类型:原代码中max_retry_count使用了字符串值"3",需要改为整数类型3,否则会导致参数解析错误。

修正后的代码:

from azure.functions import FunctionApp, TimerRequest, Context, AuthLevel
import logging

app = FunctionApp(http_auth_level=AuthLevel.ANONYMOUS)


@app.timer_trigger(schedule="*/1 * * * * *", arg_name="mytimer",
                   run_on_startup=False,
                   use_monitor=False)
@app.retry(strategy="fixed_delay", max_retry_count=3,
           delay_interval="00:00:01")
def mytimer(mytimer: TimerRequest, context: Context) -> None:
    logging.info(f'Current retry count: {context.retry_context.retry_count}')

    if context.retry_context.retry_count == context.retry_context.max_retry_count:
        logging.info(
            f"Max retries of {context.retry_context.max_retry_count} for "
            f"function {context.function_name} has been reached")
    else:
        raise Exception("This is a retryable exception")

内容的提问来源于stack exchange,提问作者aks786

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.22 22:02:05