Home > Enterprise >  Timer function blocks HttpTrigger Azure function ap
Timer function blocks HttpTrigger Azure function ap

Time:09-22

I have 1 function app project, with a Timer trigger and a http trigger. (using DI or not, does not seem to matter)

The timertrigger:

public static class Timer
{
    [FunctionName("Timer")]
    public static void Run([TimerTrigger("0 */5 * * * *", RunOnStartup =true)]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        Thread.Sleep(10000);//this is just for demonstrating the behavior!!

        log.LogInformation($"C# Timer trigger function executed done");
    }
}

the http trigger:

public static class Function1
{
    [FunctionName("test2")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
        ILogger log)
    {

        return new OkObjectResult("Done");
    }
}

The issue is, that when the timer runs, all requests are blocked. Resulting in "Error: connect ECONNREFUSED 127.0.0.1:7071"

I read here and there (mostly in comments) that multiple triggers are (were) not supported, but that doesn't make sense to me as everything seems prepared for it anyway (i mean: i could not find any conclusive documentation about it, VS doesnt throw ANY resistance to add multiple, and even the debug interface and azure interface nicely sums up all the functions it could find...)

Is this behaviour expected, or is there some setting i can change to make it run in parallel or so??

CodePudding user response:

This behavior is expected, since you're completely halting the Azure Functions process by running a Thread.Sleep(). As long as your Function App hasn't scaled to multiple instances, there's only one process running your Functions. This is where working asynchronously comes in.

If you change your timer triggered function to this, it should work fine.

[FunctionName("Timer")]
    public static async Task Run([TimerTrigger("0 */5 * * * *", RunOnStartup=true)] TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

        await Task.Delay(10000);

        log.LogInformation($"C# Timer trigger function executed done");
    }

Next to this, please be advised:

  • It is not recommended to have your timer triggered Function RunOnStartup set to true
  • It is not advised to explicitly build in delays in Functions
  • Multiple triggers for one Function are not supported, having multiple Functions each with their own trigger within one Function App is supported.
  • A Function App is a collection of one or more Functions

Triggers are what cause a function to run. A trigger defines how a function is invoked and a function must have exactly one trigger. Triggers have associated data, which is often provided as the payload of the function.

Source: Azure Functions triggers and bindings concepts

CodePudding user response:

This turns out to be because of "RunOnStartup". Any scheduled runs after the initial one do not have this issue...

However, if RunOnStartup = false, the trigger still might be called on startup and then it DOES have the issue.

Like stated in the comment, async does not change anything (i actually use async and DI in real scenario, but simplified the example here.)

  • Related