微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Azure C#.NET计时器功能未按计划运行或手动运行

如何解决Azure C#.NET计时器功能未按计划运行或手动运行

我有一个带有计时器触发器的Azure函数。时间表为"*/6 * * * *"(每六分钟运行一次)。我无法手动运行它,并且不会自动启动。以下是我的function.json

{
  "generatedBy": "Microsoft.NET.Sdk.Functions-1.0.31","configurationSource": "attributes","bindings": [
    {
      "type": "timerTrigger","schedule": "%TimerTriggerPeriod%","useMonitor": true,"runOnStartup": false,"name": "myTimer"
    }
  ],"disabled": false,"scriptFile": "../bin/AccessChangeMonitoring.dll","entryPoint": "Microsoft.IT.Security.AccessChangeMonitoring.AccessChangeMonitoring.InitiateChangeMonitoring"
}

%TimerTriggerPeriod%在我的local.settings.json文件"TimerTriggerPeriod": "0 */6 * * * *")中定义。查看仪表板上的“应用程序功能计数”度量标准显示,它表明我的功能已被执行 0次

enter image description here

下面是我的host.json

    {
      "version": "2.0","logging": {
        "applicationInsights": {
          "samplingExcludedTypes": "Request","samplingSettings": {
          

  "isEnabled": true
      }
    }
  },"functionTimeout": "00:07:00"
}

下面是功能代码

[FunctionName("InitiateChangeMonitoring")]
public static async Task InitiateChangeMonitoring([TimerTrigger("%TimerTriggerPeriod%")] TimerInfo myTimer,ILogger log)
{
    log.Loginformation("Change Monitoring started.");

    // Reset the listing of app ids we need to retrieve delta role assignments for
    oneAuthZAppIds = new List<string>();
    await GetoneAuthZAppIdsAsync();

    // Create the necessary Cosmos DB infastructure
    await CreateDatabaseAsync();
    await CreateContainerAsync();
    await CreateDeltaAPICallDatabaseAsync();
    await CreateDeltaAPICallContainerAsync();

    await CreateManagerMappingDatabaseAsync();
    await CreateManagerMappingContainerAsync();

    // Compute the authentication token needed to access the PAP Service API
    log.Loginformation("\nRetrieve PAPServiceAPIToken");
    string PAPServiceAPIToken = await GetTokenAsync(Environment.GetEnvironmentvariable("OneAuthZAppUri"),Environment.GetEnvironmentvariable("OneAuthZAppId"),PAPAuthenticationSecret);
    log.Loginformation("PAPServiceAPIToken = " + PAPServiceAPIToken);

    string GraphAPIAuthenticationToken = await GetTokenAsync(Environment.GetEnvironmentvariable("GraphAppUri"),Environment.GetEnvironmentvariable("GraphClientId"),graphKey);
    log.Loginformation("graphAPIAuthenticationToken = " + GraphAPIAuthenticationToken);

    await runchangeMonitoringSystemAsync(PAPServiceAPIToken);
}

解决方法

首先,我注意到您指定您正在使用local.settings.json来保存环境变量,并同时在天蓝色上显示指标。

所以,我认为为什么无法触发您的azure函数的第一个问题是因为您没有在azure上设置环境变量。

您应该在此位置设置而不是local.settings.json :(因为将azure函数部署到azure时,它将永远不会从local.settings.json中获取环境变量。)

enter image description here

(不要忘记保存所做的编辑。)

第二,如Ivan所说,您的cron格式错误。时间触发的格式应为以下格式:

{second} {minute} {hour} {day} {month} {day of week}

让我知道您是否可以解决这个问题。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。