2

So I'm working on sending mobile app push notifications and I've already set up Azure Notification hub, but I wanted to do scheduling in it, I understand there's an option in build in azure to do that, since it costs 200$ i decided to create Azure Functions to handle it,

I've gone through the NCRON Expressions, now I wanted to know how to schedule a job to run once on a specific date, all I could find is repetition based ones and also is it possible to run a job dynamically as in the date would vary

 public static class Function1
{
    [FunctionName("Function1")]
    public static void Run([TimerTrigger("0 0 15 2 Jan")]TimerInfo myTimer, ILogger log)
    {
        log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
    }
}

I was trying to do something like this, running something on 2nd of Jan at 15:00Hrs, it doesn't seem to work, am I missing something here and how do I make TimerTrigger dynamic?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Venky
  • 1,929
  • 3
  • 21
  • 36

4 Answers4

4

To describe clearly, I reedit the whole answer. Hope this time I can explain clearly.

OK. Fist of all, you need to know, azure Function has a declaration section and a configuration section.

On local, the declaration section is ([TimerTrigger("* * * * * *")]TimerInfo myTimer, ILogger log), and the configuration section is local.settings.json file.

When you deploy to Azure. It changes. Declaration section turns to function.json, and the Application Settings becomes the configuration section.

To your requirement, you can add a key in the configuration section and get it in your function.

For example,

On local:

function.cs:

using System;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Host;
using Microsoft.Extensions.Logging;

namespace TimeTrigger
{
    public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([TimerTrigger("%Schedule%")]TimerInfo myTimer, ILogger log)
        {
            log.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");
        }
    }
}

local.settings.json:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "Schedule": "* * * * * *"
  }
}

You can change how the timetrigger is triggered by changing the value of the key in the json.(For example, using powershell to modify the value. Or you can use code to modify.)


On portal:

And similar as on local, you can do this on portal:

Declaration section:

enter image description here

Configuration Section:

enter image description here

enter image description here

Cindy Pau
  • 13,085
  • 1
  • 15
  • 27
  • Thats not the only thing i wanted help for, i also want to dynamically schedule these functions, as in the CRON expression needs to be dynamic, would be unfair for others if someone helps me with it and is more accurate – Venky Jan 03 '20 at 10:20
  • @Venky Hi, Venky. Do you have any special needs for dynamic processing? Like getting from a database? I have update my answer. Please have look. Is it what you want? – Cindy Pau Jan 08 '20 at 09:18
  • @Venky If you have more doubts. Please ask me. – Cindy Pau Jan 08 '20 at 09:50
  • Please check my post, i nned to how to dynamically create and assign a CRON expression to a function – Venky Jan 08 '20 at 12:16
  • @Venky I am sorry to reply late. CRON expression can be changed in the local.settings.json file. And you can change the content of the json file what you want without need change the code of function. If this 'dynamic' is not what you want, can you describe clearly the 'dynamic' means? You just need to modify the json file is ok, is it not enough? – Cindy Pau Jan 15 '20 at 09:41
  • so if i make job1 with the cron expression from lets say config.json, now while creating job2 if i update the json , then would the two functions have different cron settings or would it run on the same cron ?? – Venky Jan 15 '20 at 11:40
  • @Venky Ok, Venky. Maybe my previous answer was not clear enough, and now I have rewritten the answer. I think I should have made it clear this time. Just add or delete keys and values ​​for dynamic schedule management. Can you understand now? – Cindy Pau Jan 16 '20 at 07:56
  • I think this clarifies a lot, thanks a lot for your input – Venky Jan 23 '20 at 10:52
1

You can generate the detailed CRON expression using below link:

http://corntab.com/?c=0_15_2_1_*

0 0 15 2 Jan * is the resulting CRON expression which will run it every 2nd day of January at 1500 hours for any years.

As rightly suggested by @Bowman, please check and validate the expression from above link and see if it helps.

Mohit Verma
  • 5,140
  • 2
  • 12
  • 27
1

Firstly the main problem you have now suppose is your expression, the right expression format you could refer to the doc:NCRONTAB expressions.

{second} {minute} {hour} {day} {month} {day-of-week}

Then about your requirement about run a job dynamically. You can put the schedule expression in an app setting and set this property to the app setting name wrapped in % signs, as in this example: %ScheduleAppSetting%. You could check it in the configuration.

Even with this expression it will show an error, however it will still works.

enter image description here

George Chen
  • 13,703
  • 2
  • 11
  • 26
  • How to use that though, i couldn't find any documentation in that, it just says you can use %ScheduleAppSetting%, but i wanted to schedule Function1 for 4th Jan, 5th Feb etc, then how would to create those instances, i'm trying this on Visual Studio and not on the portal, so would doing it in the portal help? – Venky Jan 03 '20 at 10:17
1

Why using a timer function if you need to run it only once?

I would say a better solution is to send a message to a queue so it runs at a scheduled time using the attribute ScheduledEnqueueTimeUtc.

https://learn.microsoft.com/en-us/azure/service-bus-messaging/message-sequencing#scheduled-messages

Kat Lim Ruiz
  • 2,425
  • 2
  • 26
  • 32