I have several Azure Functions running with Http Triggers, and they are all set up like
public async Task<IActionResult> Run(
[HttpTrigger(AuthorizationLevel.Function, "post", Route = "GAdminRemoveEntry")] HttpRequest req,
ILogger log)
{...
But now I need a Timer Trigger Azure Function, and adding the TimerTrigger attribute does not work.
public async Task<IActionResult> Run(
[TimerTrigger("0 */2 * * * *")] TimerInfo myTimer,
ILogger log)
{
The function shows up as a Timer trigger, and the timer settings shows up correctly in the Integration tab. I did not fi.
The function looks like this:
public class TestTimerTrigger
{
[FunctionName("TestTimerTrigger")]
public async Task<IActionResult> Run(
[TimerTrigger("0 */2 * * * *")] TimerInfo myTimer,
ILogger log)
{
log.LogInformation("TestTimerTrigger");
await BlobStorageHelper.WriteWebview("somefile", "somedata");
return new OkObjectResult("all good!");
}
}
The above function does not execute, nor would I be able to call it manually from the Code + Test tab. I did not see any errors during building, deploying, or executing.
But... shortly after beginning this post, I was able to get it to work by removing the IActionResult type, and the return command. This is okay in this instance, but I do not understand why that is necessary.
The function now looks like this, but my question stands: why did it not work initially?
public class TestTimerTrigger
{
[FunctionName("TestTimerTrigger")]
public async Task Run(
[TimerTrigger("0 */2 * * * *")] TimerInfo myTimer,
ILogger log)
{
log.LogInformation("TestTimerTrigger");
await BlobStorageHelper.WriteWebview("somefile", "somedata");
}
}
The IActionResult
is typically used in HTTP-triggered functions to represent an HTTP response that the function will return. It allows to specify the status code, headers, and content of the HTTP response.
However, for non-HTTP-triggered functions, such as Timer Trigger functions, there's no need to return an IActionResult
because Time trigger functions are not invoked via HTTP requests and don't need to return HTTP responses. Instead, it performs background tasks or jobs based on a schedule.
Timer Trigger function;
public class Function
{
[FunctionName("Function")]
public void Run([TimerTrigger("0 */5 * * * *")]TimerInfo myTimer, ILogger log)
{
log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
}
}
OUTPUT
:Local :
For detailed output, run func with --verbose flag.
[2024-02-16T04:23:05.319Z] Host lock lease acquired by instance ID '000000000000000000000000116B5F66'.
[2024-02-16T04:25:00.120Z] Executing 'Function' (Reason='Timer fired at 2024-02-16T09:55:00.0668667+05:30', Id=c36a4e00-5483-45f3-a078-f72d7762ef6c)
[2024-02-16T04:25:00.142Z] C# Timer trigger function executed at: 2/16/2024 9:55:00 AM
[2024-02-16T04:25:00.167Z] Executed 'Function' (Succeeded, Id=c36a4e00-5483-45f3-a078-f72d7762ef6c, Duration=89ms)
Azure :
2024-02-16 04:40:00.026
Executing 'Function' (Reason='Timer fired at 2024-02-16T04:40:00.0124959+00:00', Id=4b8c5990-16f5-4ee9-91b7-87d9e3951ce3)
Information
2024-02-16 04:40:00.038
C# Timer trigger function executed at: 2/16/2024 4:40:00 AM
Information
2024-02-16 04:40:00.053
Executed 'Function' (Succeeded, Id=4b8c5990-16f5-4ee9-91b7-87d9e3951ce3, Duration=34ms)
Information