Home Can I run a Azure function once on a specific date triggered by my asp.net mvc site?
Reply: 3

Can I run a Azure function once on a specific date triggered by my asp.net mvc site?

user1186050
1#
user1186050 Published in 2017-12-07 22:28:37Z

I know I can create a scheduled Azure function to run on a schedule. But what I want is to be able to run a Azure function once given a specific date/time I pass it, along with some data parameters.

Ex. I'm scheduling classes in my site and I want to email out all students when the class is over. So when the class is created for Monday October 9th @ 4:00PM I want to send a message to trigger my Azure function on that same day, but like 1 hour later at 5:00PM. And I want to pass it some info like the class id or something. I also want to be able to remove this queued trigger if the class is canceled.

Is this possible in Azure and my ASP.Net MVC site?

Connor McMahon
2#
Connor McMahon Reply to 2017-12-08 00:22:13Z

You should be using the queue trigger for this. You can have your MVC app add CloudQueueMessage objects to an Azure Storage Queue. These messages can contain the data parameters (i.e. student list, professor name, etc.) that are unique to the class, and you can delay the visibility in the Queue with the visibilitytimeout parameter when you perform the "Put Message".

You can calculate the visibilitytimeout by subtracting the desired time from the current time. Note that the timeout cannot be longer than 7 days, so if these events are being queued for long periods of times, you may be forced to reinsert the message into the queue repeatedly until you get within the 7 day range and you can successfully process the message.

Bruce Chen
3#
Bruce Chen Reply to 2017-12-08 08:49:54Z

My classes might be scheduled upto 30 days ahead of the current date.

Per my understanding, you could also leverage the Scheduled messages from Azure Service Bus and set the ScheduledEnqueueTimeUtc property for your message. For more details about triggering the service bus queue message, you could follow Azure Functions Service Bus bindings. For sending the message, you could install the WindowsAzure.ServiceBus in your MVC application and leverage QueueClient.ScheduleMessageAsync for sending scheduled message and QueueClient.CancelScheduledMessageAsync for cancelling the scheduled message. Moreover, you could follow the code snippet in this issue.

Connor McMahon
4#
Connor McMahon Reply to 2017-12-11 19:47:53Z

Another potential way to achieve this would be using Durable Functions. I adopted this solution off of Pattern #5, since you have potential human interaction in the form of a class cancellation.

The below is an untested rough framework of what your orchestrator function would look like. Your email logic would go in a function called SendEmail that utilizes an ActivityTrigger, and you could add new classes and cancel them by utilizing these APIs.

public static async Task Run(DurableOrchestrationContext ctx)
{
    var classInfo = ctx.GetInput<ClassInfo>();
    var targetDateTime = DateTime.Parse(classInfo.ClassEndDateString);
    var maxTimeSpan = new TimeSpan(96, 0, 0);
    using (var timeoutCts = new CancellationTokenSource())
    {
        while(true)
        {
            TimeSpan timeLeft = targetDateTime.Subtract(ctx.CurrentUtcDateTime);
            if(timeLeft <= TimeSpan.Zero) {
                break;
            }

            DateTime checkTime; 
            if(timeLeft > maxTimeSpan) {
                checkTime = ctx.CurrentUtcDateTime.Add(maxTimeSpan);
            } else {
                checkTime = ctx.CurrentUtcDateTime.Add(timeLeft);
            }

            Task durableTimeout = ctx.CreateTimer(checkTime, timeoutCts.Token);

            Task<bool> cancellationEvent = ctx.WaitForExternalEvent<bool>("Cancellation");
            if (cancellationEvent == await Task.WhenAny(cancellationEvent, durableTimeout))
            {
                timeoutCts.Cancel();
                return
            }
        }
        await ctx.CallActivityAsync("SendEmail", classInfo.ClassData);
    }
}

public class ClassInfo {
    public string ClassEndDateString {get; set; }
    public string ClassData {get; set;}
} 
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.330006 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO