7

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.

Brian Swart
  • 922
  • 1
  • 9
  • 25

4 Answers4

2

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://learn.microsoft.com/en-us/dotnet/api/microsoft.azure.storage.queue.cloudqueue.addmessage?view=azure-dotnet for more details on this overload of AddMessage.

Martin4ndersen
  • 2,806
  • 1
  • 23
  • 32
Mike S
  • 3,058
  • 1
  • 22
  • 12
  • 1
    Note that the items the Azure storage queues have a maximum lifetime of 7 days. After 7 days the message will be removed. Alternatively you can use a Azure service bus queue. – Wessel T. Aug 26 '18 at 20:15
  • this argument has a limit "The argument 'initialVisibilityDelay' is larger than maximum of '00:04:59' Parameter name: initialVisibilityDelay" – Ahmad Hajou Aug 01 '19 at 14:02
2

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

Paco de la Cruz
  • 2,066
  • 13
  • 22
1

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.

Alberto Morillo
  • 13,893
  • 2
  • 24
  • 30
1

The other answers are all valid options, but there are some others as well.

For Logic Apps you can build this behavior into the app as described in the Scheduler migration guide. The solution described there is to create a logic app with a http trigger, and pass the desired execution time to that trigger (in post data or query parameters). The 'Delay Until' block can then be used to postpone the execution of the following steps to the time passed to the trigger. You'd have to change the logic app to support this, but depending on the use case that may not be an issue.

For Azure functions a similar pattern could be achieved using Durable Functions which has support for Timers.

AVee
  • 3,348
  • 17
  • 17