0
votes

.NET Core 2.2, WebJobs SDK 3.0

I have a webjob that takes the messages from a queue. A standard QueueTrigger like this

public void ProcessQueueMessage(
              [QueueTrigger("%WebJobs:WorkerQueueName%")] CloudQueueMessage queueMessage, 
              ILogger log)

At the end of the process I write the message to another queue (archive).

The function finishes successfully but the message is kept in the source queue

In Storage Explorer I see this (in this example I had 3 messages pending)

enter image description here

and the message is dequeued once again after 10 minutes.

How can I make it so the message is dequeued when my function is successful?

Btw my queue config is

BatchSize             1  
MaxDequeueCount       2  
MaxPollingInterval    00:00:04  
VisibilityTimeout     02:00:00
2
Are you sure you are not throwing an exeption anywhere? Does your code contain AddAzureStorageCoreServices in ConfigureWebJobs? - Alex AIT

2 Answers

0
votes

The SDK should already handle that.

The message will be leased (or become invisible) for 30 seconds by default. If the job takes longer than that, then the lease will be renewed. The message will not become available to another instance of the function unless the host crashes or the function throws an exception. When the function completes successfully, the message is deleted by the SDK.

When a message is retrieved from the queue, the response includes the message and a pop receipt value, which is required to delete the message. The message is not automatically deleted from the queue, but after it has been retrieved, it is not visible to other clients for the time interval specified by the visibilitytimeout parameter. The client that retrieves the message is expected to delete the message after it has been processed, and before the time specified by the TimeNextVisible element of the response, which is calculated based on the value of the visibilitytimeout parameter. The value of visibilitytimeout is added to the time at which the message is retrieved to determine the value of TimeNextVisible.

So you shouldn't need to write any special code for deleting message from queue.

For more details you could refer to this article and this one.

0
votes

It turns out that I was using the queueMessage object I got as a parameter to directly put it in another queue which probably confused the SDK.

public void ProcessQueueMessage(
              [QueueTrigger("%WebJobs:WorkerQueueName%")] CloudQueueMessage queueMessage, 
              ILogger log)

So I changed that and I create a new CloudQueueMessage object before I put it in another queue.

var newMessage = new CloudQueueMessage(queueMessage.AsString);

Now the message is properly deleted from the queue when my function returns.