Faisal
Faisal

Reputation: 119

Can I execute azure functions during day time only with specific times

HI I am trying out azure function. Basically I want to trigger a function 3 times a day. For example 9:00 AM, 12 PM and 5 PM. This trigger a function that would get data from the database send an email or message on message on Slack? Can someone guide me?

I am using visual studio 2019, C# and SQL database.

Thanks

Upvotes: 1

Views: 896

Answers (1)

Hari Subramaniam
Hari Subramaniam

Reputation: 1876

You need to play with Cronexpression for that.

Assuming you want to it run at 09:00 12:00 17:00 everyday during working days alone, then a cronexpresstion of 0 0 9,12,17 * * 1-5 will give you an output with next 5 occurrences as (This answer is being posted on a weekend so the next occurrences during weekdays are below)

[01/04/2021 09:00:00+00:00] (01/04/2021 09:00:00Z)
[2021-01-02T12:28:08.938Z] 01/04/2021 12:00:00+00:00 (01/04/2021 12:00:00Z)
[2021-01-02T12:28:08.939Z] 01/04/2021 17:00:00+00:00 (01/04/2021 17:00:00Z)
[2021-01-02T12:28:08.939Z] 01/05/2021 09:00:00+00:00 (01/05/2021 09:00:00Z)
[2021-01-02T12:28:08.940Z] 01/05/2021 12:00:00+00:00 (01/05/2021 12:00:00Z)

The cronexpression itself can be configured on the Azure function as

    [FunctionName("Function1")]
    public static void Run([TimerTrigger("0 0 9,12,17 * * 1-5")]TimerInfo myTimer, 
        ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }

Upvotes: 2

Related Questions