A.Sharma
A.Sharma

Reputation: 2799

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.

Upvotes: 3

Views: 3289

Answers (4)

Alkemichar
Alkemichar

Reputation: 81

In my case, settings were fine but the problem was in the timer method, I was returning some responses rather than keeping it void.

Correct:

[FunctionName("function-name")]
public async Task RunAsync([TimerTrigger("%ConfigurationSetting%")] TimerInfo myTimer, ILogger log)

Incorrect:

[FunctionName("function-name")]
public async Task<Response> RunAsync([TimerTrigger("%ConfigurationSetting%")] TimerInfo myTimer, ILogger log)

Upvotes: 1

suziki
suziki

Reputation: 14080

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.

Upvotes: 2

George Chen
George Chen

Reputation: 14324

If you already configure the storage account in the appsettings, my first advice is change you Console method to ILogger method cause the Console.WriteLine could not write log in the Logs console so firstly use log.LogInformation.

Secondly, if you change the method and the azure still could not output any log, please go to the Kudu site check the log file, in there you could get a detailed file.

The file location should be D:\home\LogFiles\Application\Functions\Host, check the latest log file.

Upvotes: 2

Rakesh Suryawanshi
Rakesh Suryawanshi

Reputation: 520

In your Azure function configuration settings make sure you have 'AzureWebJobsStorage' added and it's pointing to azure storage account connection string

enter image description here

Upvotes: 0

Related Questions