简体   繁体   中英

Timer function blocks HttpTrigger Azure function ap

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??

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

EDIT:
Tested it, and this also occurs when the Function is async , and also when it's running on Azure. This means the initial run of the Function with runAtStartup=true always blocks any other incoming requests, and with runOnStartup enabled the trigger is invoked whenever your function app is scaled.

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.

As stated in the comments, async does not change anything (I actually use async and DI in the real scenario, but simplified the example here.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM