首页 > 解决方案 > Azure Function App: TimerTrigger not Firing when Deployed

问题描述

I have an Azure Function App (v2) that is run through a TimerTrigger (every day at midnight - EST). It works fine when I run it locally in Visual Studio. However, when I use the built in Visual Studio Publishing Tool to publish it to my Function App directly (Zip Deploy), it doesn't trigger at midnight nor any time for that matter.

I have also tried to run the function directly through the Azure portal and that doesn't work either. Here is my code. All it is doing is pulling a file from Azure Storage, updating the ranked game JSON, and then replacing the file back in Azure Storage. I have a mobile app that pulls the ranked_settings.json and generates the ranked game.

[FunctionName("RankedSettingsIntervalFunction")]
public static void Run([TimerTrigger("0 0 0 */1 * *")]TimerInfo myTimer, ILogger log, ExecutionContext context)
{
    Console.WriteLine("App started running at " + DateTimeOffset.UtcNow.ToString());

    const string fileName = "ranked_settings.json";

    var config = new ConfigurationBuilder()
                .SetBasePath(context.FunctionAppDirectory)
                .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                .AddEnvironmentVariables()
                .Build();

    var conn1 = config["FileStorage:ConnectionStringOne"];
    var conn2 = config["FileStorage:ConnectionStringTwo"];

    StorageService service = new StorageService(conn1, conn2);
    Settings settings = RankedSettingsGenerator.Generate();
    var json = JsonConvert.SerializeObject(settings);

    Console.WriteLine("App original JSON created at " + DateTimeOffset.UtcNow.ToString());
    Console.WriteLine(json);

    byte[] byteArray = Encoding.ASCII.GetBytes(json);
    MemoryStream stream = new MemoryStream(byteArray);

    service.AddFile(fileName, stream);

    Console.WriteLine("App finished running at " + DateTimeOffset.UtcNow.ToString());
}

My function.json file has the following bindings:

"bindings": [
    {
      "type": "timerTrigger",
      "schedule": "0 0 0 */1 * *",
      "useMonitor": true,
      "runOnStartup": false,
      "name": "myTimer"
    }
  ],

I was thinking the issue was that the runOnStartup property is false, but that doesn't explain why it doesn't work when I press the Run button in the portal.

标签: c#.netazureazure-functions

解决方案


Update:

For your situation, please note that there is not a local.settings.json file on Azure. The local.settings.json will be publish to Azure Application Settings as usual.

The Application Settings is here:

enter image description here

enter image description here

And the code that you use to get settings should be like this:

var config = new ConfigurationBuilder()
    .AddEnvironmentVariables()
    .Build();
string conn1 = config["xxxxxxxxxxxxx"];
string conn2 = config["xxxxxxxxxxxxx"];

Or simply

Environment.GetEnvironmentVariable("xxxxxxxxxxxxx");

In addition,

I notice you are using Console.WriteLine() in your code. Please note that the index of Console.WriteLine() will not show on the portal. You need to use log.LogInformation() to show the information.

Original Answer:

For Azure Function, many things are possible to run locally. But there are a lot of things to watch out for when deploying to Azure.

Do you use the Consumption plan? Or you don't set the time zone? These two reason will both cause this problem.

Have a look of this Offcial doc.


推荐阅读