Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Azure Functions in one Timer trigger class

We have to write multiple triggers. I was hoping to create separate functions, based on the trigger types. So if I need 5 timer triggers, that will run at different times, I would create one Timer trigger function class and name the functions like [TimerTrigger1], [TimerTrigger2], [TimerTrigger3] ... and so forth. After I added the code I am not sure if I can do that anymore.

Can someone suggest how I can go about adding multiple triggers? I can't have two Run functions under one class.

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

    [FunctionName("InsertTimerTrigger2")]
    public static void Run([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, ILogger log)
    {
        //Do Task 2
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}
like image 482
Sarah Avatar asked Dec 14 '22 11:12

Sarah


2 Answers

You can create multiple functions in Single Class. You can change Run Method name.

public static class Function1
{
    [FunctionName("Function1")]
    public static void Method1([TimerTrigger("0 */2 * * * *")]TimerInfo myTimer, TraceWriter log)
    {
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    }

    [FunctionName("Function2")]
    public static void Method2([TimerTrigger("0 */3 * * * *")]TimerInfo myTimer, TraceWriter log)
    {
        log.Info($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

But I will recommend, Create multiple functions will help you (5 in your case).

  1. If you are using common business logic, you can put in a common class and inject in all function.
  2. You can independently Enable/Disable/Delete function from FunctionApp Instance.

enter image description here

  1. You can monitor each function independently (from Function Monitor section)

enter image description here

like image 200
Pankaj Rawat Avatar answered Feb 23 '23 02:02

Pankaj Rawat


You can choose any name for the methods. (Naming it as "Run" is not a requirement.)

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

    [FunctionName("InsertTimerTrigger2")]
    public static void InsertTimerTrigger2([TimerTrigger("0 */15 * * * *")]TimerInfo myTimer, ILogger log)
    {
        //Do Task 2
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}
like image 25
Turbo Avatar answered Feb 23 '23 02:02

Turbo