通过WebJobs在Azure存储队列中重新排队或删除消息 [英] Requeue or delete messages in Azure Storage Queues via WebJobs

查看:97
本文介绍了通过WebJobs在Azure存储队列中重新排队或删除消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望有人能澄清有关Azure存储队列及其与WebJobs交互的一些事情:

I was hoping if someone can clarify a few things regarding Azure Storage Queues and their interaction with WebJobs:

  1. 要执行重复的后台任务(即添加到队列一次,然后以设置的间隔重复),有没有一种方法可以更新QueueTrigger函数中传递的相同消息,以便可以将其租期(可见性)扩展为一种重新排队并避免到期的方法?

  1. To perform recurring background tasks (i.e. add to queue once, then repeat at set intervals), is there a way to update the same message delivered in the QueueTrigger function so that its lease (visibility) can be extended as a way to requeue and avoid expiry?

使用上述重复出现的后台作业模式,我还试图找到一种按需"删除/过期作业的方法.由于这在WebJobs的上下文之外似乎不太可能,因此我考虑了将要删除的消息的messageId和popReceipt作为持久性缓存存储在表存储中,然后在QueueTrigger函数中传递消息时执行表查找以执行DeleteMessage,以便不再重复该消息.

With the above-mentioned pattern for recurring background jobs, I'm also trying to figure out a way to delete/expire a job 'on demand'. Since this doesn't seem possible outside the context of WebJobs, I was thinking of maybe storing the messageId and popReceipt for the message(s) to be deleted in Table storage as persistent cache, and then upon delivery of message in the QueueTrigger function do a Table lookup to perform a DeleteMessage, so that the message is not repeated any more.

任何建议或提示,我们感激不尽.欢呼声:)

Any suggestions or tips are appreciated. Cheers :)

推荐答案

Azure存储队列用于存储Azure Webjob,WorkerRole等可能消耗的消息.AzureWebjobs SDK提供了一种与Azure进行交互的简便方法存储(包括队列,表存储,Blob和服务总线).话虽如此,您还可以拥有一个不使用Webjobs SDK且不与Azure存储进行交互的Azure Webjob.实际上,我确实运行了与SQL Azure数据库交互的Webjob.

Azure Storage Queues are used to store messages that may be consumed by your Azure Webjob, WorkerRole, etc. The Azure Webjobs SDK provides an easy way to interact with Azure Storage (that includes Queues, Table Storage, Blobs, and Service Bus). That being said, you can also have an Azure Webjob that does not use the Webjobs SDK and does not interact with Azure Storage. In fact, I do run a Webjob that interacts with a SQL Azure database.

我将简要解释Webjobs SDK如何与Azure队列交互.一旦消息到达队列(或使其变为可见",稍后将对此进行详细说明),Webjob中的功能就会被触发(假设您正在连续模式下运行).如果该函数没有错误返回,则消息将被删除.如果出现问题,消息将返回到队列以再次处理.您可以相应地处理失败的消息. 此处是有关如何执行此操作的示例.

I'll briefly explain how the Webjobs SDK interact with Azure Queues. Once a message arrives to a queue (or is made 'visible', more on this later) the function in the Webjob is triggered (assuming you're running in continuous mode). If that function returns with no error, the message is deleted. If something goes wrong, the message goes back to the queue to be processed again. You can handle the failed message accordingly. Here is an example on how to do this.

SDK将最多调用一个函数5次以处理队列消息.如果第五次尝试失败,则将消息移至中毒队列.重试的最大次数是可配置的.

The SDK will call a function up to 5 times to process a queue message. If the fifth try fails, the message is moved to a poison queue. The maximum number of retries is configurable.

关于可见性,当您将消息添加到队列时,具有可见性超时属性.默认情况下为零.因此,如果您希望将来处理一条消息,可以将此属性设置为所需的值(最长7天).

Regarding visibility, when you add a message to the queue, there is a visibility timeout property. By default is zero. Therefore, if you want to process a message in the future you can do it (up to 7 days in the future) by setting this property to a desired value.

可选.如果指定,则必须使用2011-08-18或更高版本的x-ms版本发出请求.如果未指定,则默认值为0.指定新的可见性超时值,以秒为单位,相对于服务器时间.新值必须大于或等于0,并且不能大于7天.消息的可见性超时不能设置为晚于到期时间的值.可见性超时应设置为小于生存时间的值.

Optional. If specified, the request must be made using an x-ms-version of 2011-08-18 or newer. If not specified, the default value is 0. Specifies the new visibility timeout value, in seconds, relative to server time. The new value must be larger than or equal to 0, and cannot be larger than 7 days. The visibility timeout of a message cannot be set to a value later than the expiry time. visibilitytimeout should be set to a value smaller than the time-to-live value.

现在为您的应用提供建议.

Now the suggestions for your app.

  1. 我只想向您要完成的每个任务的队列添加一条消息.该消息显然将具有相关信息以供处理.如果需要安排多个任务,则可以运行计划的Web作业"(根据您选择的时间表),以将消息添加到队列中.然后,您连续的Webjob将接收该消息并进行处理.
  2. 向发送到队列的每条消息添加一个GUID.将该GUID存储在应用程序的其他域(数据库)中.因此,当您使消息出队以进行处理时,您要做的第一件事就是检查数据库是否需要处理消息.如果您需要取消执行一条消息,而不是从队列中删除它,只需更新数据库中的GUID.

此处获得更多信息.

希望这会有所帮助,

这篇关于通过WebJobs在Azure存储队列中重新排队或删除消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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