简体   繁体   English

我可以在我的asp.net mvc网站触发的特定日期运行一次Azure功能吗?

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

I know I can create a scheduled Azure function to run on a schedule. 我知道我可以创建一个计划的Azure函数来按计划运行。 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. 但我想要的是能够在给定特定日期/时间的情况下运行Azure函数,以及一些数据参数。

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. 因此,当该类在10月9日星期一下午4:00创建时,我想在同一天发送消息以触发我的Azure功能,但是比如1小时后的下午5:00。 And I want to pass it some info like the class id or something. 我想传递一些类似id或类似的信息。 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? 这在Azure和我的ASP.Net MVC站点中是否可行?

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. 我从模式#5中采用了这个解决方案,因为你可以通过类取消的方式进行潜在的人工交互。

The below is an untested rough framework of what your orchestrator function would look like. 下面是一个未经测试的粗略框架,展示了你的orchestrator函数的样子。 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 . 您的电子邮件逻辑将使用名为SendEmail的函数,该函数使用ActivityTrigger,您可以添加新类并使用这些API取消它们。

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;}
} 

My classes might be scheduled upto 30 days ahead of the current date. 我的班级可能会安排在当前日期之前30天。

Per my understanding, you could also leverage the Scheduled messages from Azure Service Bus and set the ScheduledEnqueueTimeUtc property for your message. 根据我的理解,您还可以利用Azure Service Bus中的预定消息 ,并为您的消息设置ScheduledEnqueueTimeUtc属性。 For more details about triggering the service bus queue message, you could follow Azure Functions Service Bus bindings . 有关触发服务总线队列消息的更多详细信息,您可以遵循Azure功能服务总线绑定 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. 要发送消息,可以在MVC应用程序中安装WindowsAzure.ServiceBus ,并利用QueueClient.ScheduleMessageAsync发送预定消息,使用QueueClient.CancelScheduledMessageAsync取消预定消息。 Moreover, you could follow the code snippet in this issue . 此外,您可以按照此问题中的代码段进行操作。

You should be using the queue trigger for this. 您应该使用队列触发器 You can have your MVC app add CloudQueueMessage objects to an Azure Storage Queue. 您可以让MVC应用程序将CloudQueueMessage对象添加到Azure存储队列。 These messages can contain the data parameters (ie 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" . 这些消息可以包含该类唯一的数据参数(即学生列表,教授姓名等),并且当您执行“放置消息”时,可以使用visibilitytimeout参数延迟队列中的visibilitytimeout

You can calculate the visibilitytimeout by subtracting the desired time from the current time. 您可以通过从当前时间减去所需时间来计算visibilitytimeout时间。 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. 请注意,超时不能超过7天,因此如果这些事件排队很长一段时间,您可能会被迫重复将消息重新插入队列,直到您进入7天范围内并且您可以成功处理信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM