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?
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: