带有 2 个计时器触发器的单个 Azure Functions

问题描述 投票:0回答:1

不确定这是否可以完成,因为我在网上发现了相互矛盾的答案。

我有一个 Azure 函数应用程序:

[Function("RetrieveMongoData")]
 public void Run([TimerTrigger("0 0 * * * *")] TimerInfo myTimer)
 { .....

  [Function("RetrieveMongoDataNL")]
  public void Run([TimerTrigger("0 5 * * * *")] TimerInfo myTimer)
  {....

每个函数都在自己的类中。当我在 VS 中按 F5 进行调试时,上面的第一个函数被命中,我可以调试该函数。在第二个函数中放置一个断点,它永远不会被击中。我可以像这样创建触发器还是有更好的方法?

我这样做是因为我的很多被调用的代码将在两个触发器之间共享。

azure azure-functions
1个回答
0
投票
  • 当您在一个函数应用程序中本地创建两个计时器触发函数时,将为每个函数创建两个单独的 .cs 文件。
  • 验证CRON表达式是否正确。例如,我在函数中使用了
    * * * * *
    */2 * * * *
    表达式。
  • 确保 Azurite 在本地调试时正在运行。您可以通过单击 Fn + F1 启动 Azurite,然后在 VS Code 中选择 Start Azurite。还要在本地设置文件中将 AzureWebJobsStorage 设置为 UseDevelopmentStorage=true
{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet-isolated"
  }
}

检索MongoData.cs:-

[Function("RetrieveMongoData")]
public void Run([TimerTrigger("* * * * *")] TimerInfo myTimer)
{
    _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            
    if (myTimer.ScheduleStatus is not null)
    {
        _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
    }
}

检索MongoDataNL.cs:-

[Function("RetrieveMongoDataNL")]
public void Run([TimerTrigger("*/2 * * * *")] TimerInfo myTimer)
{
    _logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
            
    if (myTimer.ScheduleStatus is not null)
    {
        _logger.LogInformation($"Next timer schedule at: {myTimer.ScheduleStatus.Next}");
    }
}

两个功能均在本地成功触发。

Azure Functions Core Tools
Core Tools Version:       4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875

[2025-01-06T03:50:28.958Z] Found C:\Users\****\Documents\functionApp\79326599\79326599.csproj. Using for user secrets file configuration.
[2025-01-06T03:50:31.164Z] Worker process started and initialized.

Functions:

        RetrieveMongoData: timerTrigger

        RetrieveMongoDataNL: timerTrigger

For detailed output, run func with --verbose flag.
[2025-01-06T03:50:36.170Z] Host lock lease acquired by instance ID '0000000000000000000000000D2022A4'.
[2025-01-06T03:51:00.050Z] Executing 'Functions.RetrieveMongoData' (Reason='Timer fired at 2025-01-06T09:21:00.0275508+05:30', Id=f923c3b2-9537-4ce4-b0da-74473ad1d4a7)
[2025-01-06T03:51:00.212Z] Next timer schedule at: 06-01-2025 09:21:00
[2025-01-06T03:51:00.212Z] C# Timer trigger function executed at: 06-01-2025 09:21:00
[2025-01-06T03:51:00.254Z] Executed 'Functions.RetrieveMongoData' (Succeeded, Id=f923c3b2-9537-4ce4-b0da-74473ad1d4a7, Duration=207ms)
[2025-01-06T03:52:00.000Z] Executing 'Functions.RetrieveMongoData' (Reason='Timer fired at 2025-01-06T09:21:59.9996713+05:30', Id=34bf0d9d-65cf-4e9f-8770-07b56366eb37)
[2025-01-06T03:52:00.015Z] Executing 'Functions.RetrieveMongoDataNL' (Reason='Timer fired at 2025-01-06T09:22:00.0152879+05:30', Id=9266d492-8c96-4b9b-b2a0-8337a7b2a4d1)
[2025-01-06T03:52:00.100Z] C# Timer trigger function executed at: 06-01-2025 09:22:00
[2025-01-06T03:52:00.103Z] Next timer schedule at: 06-01-2025 09:22:00
[2025-01-06T03:52:00.107Z] C# Timer trigger function executed at: 06-01-2025 09:22:00
[2025-01-06T03:52:00.108Z] Next timer schedule at: 06-01-2025 09:22:00
[2025-01-06T03:52:00.109Z] Executed 'Functions.RetrieveMongoDataNL' (Succeeded, Id=9266d492-8c96-4b9b-b2a0-8337a7b2a4d1, Duration=94ms)
[2025-01-06T03:52:00.108Z] Executed 'Functions.RetrieveMongoData' (Succeeded, Id=34bf0d9d-65cf-4e9f-8770-07b56366eb37, Duration=109ms)

然后,我将这些功能部署到应用程序服务计划功能应用程序中,并能够得到预期的响应。

enter image description here

enter image description here

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.