gpt4 book ai didi

Azure Function TimeTrigger 一遍又一遍地运行

转载 作者:行者123 更新时间:2023-12-03 06:09:27 26 4
gpt4 key购买 nike

我有一个名为 POEditorTranslations 的 Azure 函数,其 TimeTrigger 设置为:

* */10 * * * *

首先是公共(public)静态异步任务 POEditorTranslations...。它向正在等待的外部 API 发出了多个请求。我注意到它被一遍又一遍地执行,没有遵守上面每 10 分钟运行一次的间隔。

所以我将其重新制作为一个非常基本的非异步函数,而不是只记录(如下面的代码),但它仍然一遍又一遍地运行?!

函数如下所示:

 [FunctionName("POEditorTranslations")]
public static void POEditorTranslations([TimerTrigger("%POEditorSchedule%")] TimerInfo myTimer, ILogger log, Microsoft.Azure.WebJobs.ExecutionContext context) //https://arminreiter.com/2017/02/azure-functions-time-trigger-cron-cheat-sheet/
{
IConfigurationRoot config = new ConfigurationBuilder()
.SetBasePath(context.FunctionAppDirectory)
.AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
.AddEnvironmentVariables()
.Build();

log.LogInformation($"POEditorTranslations TESTING function executed at: {DateTime.Now}, POEditorSchedule: {config.GetValue<string>("POEditorSchedule")}");

}

所以它应该每 10 分钟运行一次,但它却一遍又一遍地运行?!执行后立即重新开始。

注意:在我的 Visual Studio 中本地运行它可以工作,但我有一个 Azure 中不存在的 local.settings.json 文件。

仍然如您所见,我记录了配置键“POEditorSchedule”的值,它对我来说看起来是正确的。

我做错了什么?

下面的日志显示了很多重新开始的行:

2023-08-24T08:30:00Z   [Verbose]   Timer for 'POEditorTranslations' started with interval '00:00:00.8937384'.

2023-08-24T08:30:01Z [信息] 正在执行“POEditorTranslations”(原因=“计时器于 2023-08-24T08:30:00.9949394+00:00 触发”,Id= 7a0544f4-4a28-44a4-a379-2a441edb0d82)

2023-08-24T08:30:01Z [信息] POEditorTranslations TESTING 函数执行于:8/24/2023 8:30:00 AM,POEditorSchedule: * */10 * * * *

2023-08-24T08:30:01Z [信息] 执行“POEditorTranslations”(成功,Id=7a0544f4-4a28-44a4-a379-2a441edb0d82,持续时间=1ms)

2023-08-24T08:30:01Z [详细] 函数“POEditorTranslations”更新状态:Last='2023-08-24T08:30:01.0000000+00:00',Next='2023-08-24T08:30 :02.0000000+00:00',LastUpdated='2023-08-24T08:30:01.0000000+00:00'

2023-08-24T08:30:01Z [详细]“POEditorTranslations”计时器以间隔“00:00:00.9871280”开始。

2023-08-24T08:30:02Z [信息] 正在执行“POEditorTranslations”(原因=“计时器于 2023-08-24T08:30:01.9988546+00:00 触发”,Id= 003f08fc-92de-4509-8fc3-fe97a0813346)

2023-08-24T08:30:02Z [信息] POEditorTranslations TESTING 函数执行于:8/24/2023 8:30:01 AM,POEditorSchedule: * */10 * * * *

2023-08-24T08:30:02Z [信息] 执行“POEditorTranslations”(成功,Id=003f08fc-92de-4509-8fc3-fe97a0813346,持续时间=1ms)

2023-08-24T08:30:02Z [详细] 函数“POEditorTranslations”更新状态:Last='2023-08-24T08:30:02.0000000+00:00',Next='2023-08-24T08:30 :03.0000000+00:00',LastUpdated='2023-08-24T08:30:02.0000000+00:00'

更新

我得到了有关在 Azure 门户中使用环境变量而不是配置的答案 - 但我没有这样的菜单项:

enter image description here

最佳答案

我已在我的环境中重现了该问题,但在将 Cron 表达式更新为 0 */10 * * * * 后,我的函数每 10 分钟就开始按预期工作

代码:

[FunctionName("POEditorTranslations")]
public void Run([TimerTrigger("%POEditorSchedule%")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"POEditorTranslations TESTING function executed at: {DateTime.Now}");
}

本地运行时-

local.settings

{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "UseDevelopmentStorage=true",
"FUNCTIONS_WORKER_RUNTIME": "dotnet",
"POEditorSchedule": "0 */10 * * * *"
}
}

本地输出:

enter image description here

部署后,我已将 POEditorSchedule 添加到应用设置 -

enter image description here

输出:

enter image description here

第一次触发-

enter image description here

第二次触发-

enter image description here

关于Azure Function TimeTrigger 一遍又一遍地运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/76967528/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com