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

Azure Timer Trigger与手动触发执行结果不一致问题排查

问题描述

我配置了一个每30分钟触发的Azure Function Timer Trigger,功能是从SQL表拉取数据,异常时发送邮件,正常则记录日志。目前遇到以下问题:

  • Timer Trigger能按时触发,但返回结果不正确,会复用上次手动运行的结果
  • 在Azure Portal的「代码+测试」板块手动触发时,功能完全正常
  • 已通过VS Code部署并重启Function App同步触发器,想咨询两种触发方式的代码执行机制是否存在差异(比如请求来源IP不同)

相关配置及代码如下:

function.json

{  
  "scriptFile": "__init__.py",  
  "bindings": [    
    {      
      "name": "mytimer",      
      "type": "timerTrigger",      
      "direction": "in",      
      "schedule": "0 */30 * * * *"    
    }  
  ]
}

init.py

import datetime
import logging

import azure.functions as func
from heartbeatmailapp import run_the_app

def main(mytimer: func.TimerRequest) -> None:

    run_the_app()

    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    if mytimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function ran at %s', utc_timestamp)
解决方案

问题根源在于存储数据表的全局变量在Timer Trigger触发时未更新,而手动触发时会重新初始化该变量,因此表现正常。

修改前代码片段:

import pandas as pd

df = pd.read_sql(query, connection)

修改后代码片段:

import pandas as pd

def update_table():
    df = pd.read_sql(query, connection)

修改后需要在主函数开头调用update_table()函数,确保每次触发时都会重新拉取最新数据。


内容的提问来源于stack exchange,提问作者Dævli

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.26 05:36:22