Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to test/run azure times function locally

I have a solution in Visual Studio 2019, I added a new project of type "Azure functions". I have a function in that project that runs on a timer trigger.

I want to run that function only once during debug, is there a way to do that without fiddling with the timer settings so that it will only start, like, 10 seconds after I start debugging?

The function does not have any endpoints and it's not being called by anything, its point is to run on a schedule.

If I were to start debugging and set the timer to run it every X, but I want to run it explicitly only one time while debugging.

like image 572
Tessaract Avatar asked Sep 05 '25 01:09

Tessaract


2 Answers

You can use the RunOnStartup option inside the [TimerTrigger] attribute. For example:

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

You can make it debug-only by using preprocessor directives:

public static void Run([TimerTrigger("0 */5 * * * *",
#if DEBUG
    RunOnStartup = true
#endif
    )]TimerInfo myTimer, ILogger log)
{
    log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
like image 154
Andy Avatar answered Sep 07 '25 15:09

Andy


You can execute timer triggered functions by sending a request to its admin url:

http://localhost:<your port>/admin/functions/<your function name>

Example POST:

POST /admin/functions/<function name> HTTP/1.1
Host: localhost:5800
x-functions-key: <your function key>
Content-Type: application/json

{}
like image 24
Crowcoder Avatar answered Sep 07 '25 15:09

Crowcoder