Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Programmatically Schedule one-time execution of Azure function

I have looked through documentation for WebJobs, Functions and Logic Apps in Azure but I cannot find a way to schedule a one-time execution of a process through code. My users need to be able to schedule notifications to go out at a specific time in the future (usually within a few hours or a day from being scheduled). Everything I am reading on those processes is using CRON expressions which is not designed for one-time executions. I realize that I could schedule the job to run on intervals and check the database to see if the rest of the job needs to run, but I would like to avoid running the jobs unnecessarily if possible. Any help is appreciated.

If it is relevant, I am using C#, ASP.NET MVC Core, App Services and a SQL database all hosted in Azure. My plan was to use Logic apps to check the database for a scheduled event and send notifications through Twilio, SendGrid, and iOS/Android push notifications.

like image 593
Brian Swart Avatar asked Sep 24 '17 23:09

Brian Swart


People also ask

Can you schedule an Azure function?

Schedule a Function in Azure by Timer Trigger One of the triggers for Azure Functions is the timer-trigger – allowing you to run a function on a schedule. With the timer trigger, you can use cron-expression to define when the function needs to run.

What is execution time in Azure function?

Azure Functions in a consumption plan are limited to 10 minutes maximum run duration. In an Azure Function premium plan that run duration is unbounded. By default we will limit you to 30 minutes (mostly to prevent against runaway executions) but you can modify the host. json to make this unbounded.

How do I manually trigger a timer in Azure?

Navigate to your function app in the Azure portal, select App Keys, and then the _master key. In the Edit key section, copy the key value to your clipboard, and then select OK. After copying the _master key, select Code + Test, and then select Logs.


3 Answers

One option is to create Azure Service Bus Messages in your App using the ScheduledEnqueueTimeUtc property. This will create the message in the queue, but will only be consumable at that time.

Then a Logic App could be listening to that Service Bus Queue and doing the further processing, e.g. SendGrid, Twilio, etc...

HTH

like image 138
Paco de la Cruz Avatar answered Nov 12 '22 04:11

Paco de la Cruz


You could use Azure Queue trigger with deferred visibility. This will keep the message invisible for a specified timeout. This conveniently acts as a timer.

CloudQueue queueOutput; // same queue as trigger listens on 
var strjson = JsonConvert.SerializeObject(message); // message is your payload
var cloudMsg = new CloudQueueMessage(strjson);

var delay = TimeSpan.FromHours(1); 
queueOutput.AddMessage(cloudMsg, initialVisibilityDelay: delay);

See https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.storage.queue.cloudqueue.addmessage?view=azure-dotnet for more details on this overload of AddMessage.

like image 38
Mike S Avatar answered Nov 12 '22 04:11

Mike S


You can use Azure Automation to schedule tasks programmatically using REST API. Learn about it here.

You can use Azure Event Grid also. Based on this article you can “Extend existing workflows by triggering a Logic App once there is a new record in your database".

Hope this helps.

like image 25
Alberto Morillo Avatar answered Nov 12 '22 04:11

Alberto Morillo