Azure C# (.NET) 计时器功能未按计划或手动 运行
Azure C# (.NET) timer function not running on schedule OR manually
我有一个带有计时器触发器的 Azure 函数。时间表是 "*/6 * * * *"
(每六分钟 运行ning)。我无法手动 运行 它并且它不会自动触发。下面是我的 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 * * * *"
) 中定义。查看仪表板上的 Application Function Count 指标显示,它显示我的函数已执行 0 次:
下面是我的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函数无法触发的第一个问题是你没有在azure上设置环境变量。
你应该在这个地方设置而不是local.settings.json(因为当一个函数部署到Azure时,它永远不会从local.settings.json获取环境变量):
(不要忘记保存编辑。)
其次,正如Ivan所说,你的cron格式是错误的。 timetrigger 的格式应该是这样的格式:
{second} {minute} {hour} {day} {month} {day of week}
我有一个带有计时器触发器的 Azure 函数。时间表是 "*/6 * * * *"
(每六分钟 运行ning)。我无法手动 运行 它并且它不会自动触发。下面是我的 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 * * * *"
) 中定义。查看仪表板上的 Application Function Count 指标显示,它显示我的函数已执行 0 次:
下面是我的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函数无法触发的第一个问题是你没有在azure上设置环境变量。
你应该在这个地方设置而不是local.settings.json(因为当一个函数部署到Azure时,它永远不会从local.settings.json获取环境变量):
(不要忘记保存编辑。)
其次,正如Ivan所说,你的cron格式是错误的。 timetrigger 的格式应该是这样的格式:
{second} {minute} {hour} {day} {month} {day of week}