Reputation: 2047
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?
Upvotes: 2
Views: 7739
Reputation: 2562
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
Upvotes: 1
Reputation: 14088
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:
Configuration Section:
Upvotes: 4
Reputation: 14334
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.
Upvotes: 1
Reputation: 5294
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.
Upvotes: 1