python - Azure计时器触发函数 ".past_due"返回false

标签 python python-3.x azure timer azure-functions

我正在编写我的第一个计时器触发函数,但在将它们配置为自行运行时遇到问题。为了便于解释,我们只关注其中一个。

函数记录为运行成功,但由于日志记录,我发现每次函数运行时“myTimer.past_due”都会返回“false”,并且函数逻辑不会启动。

我应该如何设置它才能可靠地返回“true”?

我应该如何设置它,以便它可以通过“测试代码”表单门户运行?

我在这里没有得到什么?是否还有其他计时器需要匹配?

我什至通过在 Azure 中设置新功能应用程序和存储帐户来“将其关闭并再次打开”,以确保我在此过程中没有破坏某些设置。

原理:这是一个ETL函数,通过API连接到第三方平台,并将所需数据插入到azure SQL数据库中。我希望它每天凌晨 1 点运行。对于启动函数脚本,我使用了通过 Microsoft DOC 页面提供的脚本,稍加修改,来自此处:https://learn.microsoft.com/pl-pl/azure/azure-functions/functions-bindings-timer?tabs=python

这是我的“init.py”:

import datetime
import logging

import azure.functions as func

from Toggl_Data_Procurement_Function import toggl_script


def main(myTimer: func.TimerRequest) -> None:
    utc_timestamp = datetime.datetime.utcnow().replace(
        tzinfo=datetime.timezone.utc).isoformat()

    logging.info(f"myTimer.past_due: {myTimer.past_due}")
    if myTimer.past_due:
        logging.info('Function %s, started by timer trigger, started running at %s', func.Context.function_name, utc_timestamp)
        toggl_script.TogglDataProcurementScript(root_directory=str(func.Context.function_directory)).run()

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

    logging.info('Function %s, started by timer trigger finished running at %s', func.Context.function_name, utc_timestamp)

这是我的 function.json - 为了调试,我将其设置为每 15 分钟运行一次:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "name": "myTimer",
      "type": "timerTrigger",
      "direction": "in",
      "schedule": "0 */15 * * * *",
      "useMonitor": true
    }
  ]
}

这是我的 local.settings.json。我编辑了存储帐户和仪器 key ,但它们是从平台的适当应用程序 key 复制的:

{
  "IsEncrypted": false,
  "Values": {
    "FUNCTIONS_EXTENSION_VERSION": "~3",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "APPINSIGHTS_INSTRUMENTATIONKEY": "secret_key_2",
    "APPLICATIONINSIGHTS_CONNECTION_STRING": "InstrumentationKey=secret_key_2;IngestionEndpoint=https://centralus-0.in.applicationinsights.azure.com/",
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=account_name;AccountKey=secret_key;EndpointSuffix=core.windows.net",
  }
}

最佳答案

属性past_due代表你的定时器触发函数是否被及时调用。如果你的定时器触发函数被及时调用(工作正常),它将是“假”。只有当定时器触发函数的调用晚于预定时间时,past_due 才会为“true”。可以引用这个document .

enter image description here

所以你不能把代码放在if myTimer.past_due:下,你应该像下面的截图一样编写代码:

enter image description here

对于您的问题我应该如何设置它以便它可以通过“测试代码”表单门户运行?。定时器触发函数应该根据cron表达式自动执行。我们不应该在门户上手动运行它。

关于python - Azure计时器触发函数 ".past_due"返回false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66984779/

相关文章:

python - 使用 Beautiful Soup 抓取网站时出现编码错误

azure - 从AD B2C中提取授权码

python - 为什么 < 比 >= 慢

django - "Need a valid file name!"xhtml2pdf 与 Django

python - 我的 python 文件如何访问已安装的库 (Pip)

python-3.x - 将列转换为时间戳 - Pandas Dataframe

asp.net - Azure错误进程 'msbuild.exe'退出,代码为 '1'

powershell - 安装模块 : The term 'Install-Module' is not recognized as the name of a cmdlet

python - Tensorflow 变量重用

python - 将 ffmpeg 上传到 Azure 函数环境 PATH [Python]