以编程方式安排 Azure 函数的一次性执行 [英] Programmatically Schedule one-time execution of Azure function

查看:11
本文介绍了以编程方式安排 Azure 函数的一次性执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我查看了 Azure 中 WebJobs、Functions 和 Logic Apps 的文档,但找不到通过代码安排一次性执行流程的方法.我的用户需要能够安排通知在未来的特定时间发出(通常在安排后的几个小时或一天内).我在这些过程中阅读的所有内容都在使用不是为一次性执行而设计的 CRON 表达式.我意识到我可以安排作业以间隔运行并检查数据库以查看作业的其余部分是否需要运行,但我想尽可能避免不必要地运行作业.任何帮助表示赞赏.

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.

如果它是相关的,我正在使用 C#、ASP.NET MVC Core、应用服务和一个 SQL 数据库,它们都托管在 Azure 中.我的计划是使用 Logic 应用程序检查数据库中的预定事件,并通过 Twilio、SendGrid 和 iOS/Android 推送通知发送通知.

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.

推荐答案

您可以使用带有 延迟可见性的 Azure 队列触发器.这将使消息在指定的超时时间内不可见.这方便地用作计时器.

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

请参阅 https://docs.microsoft.com/en-us/dotnet/api/microsoft.azure.storage.queue.cloudqueue.addmessage?view=azure-dotnet 了解有关 AddMessage 重载的更多详细信息.

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.

这篇关于以编程方式安排 Azure 函数的一次性执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆