How to DeadLetter a brokered message on custom exception - azure

I need to move my BrokeredMessage to deadletter queue forcefully, if I got a custom exception.
Here is my code I have used:
public static async Task Run([ServiceBusTrigger("myqueue", Connection = "myservicebus:cs")]BrokeredMessage myQueueItem, TraceWriter log)
{
try
{
// process message logic..
}
catch(CustomException ex)
{
//forcefully dead letter if custom exception occurs
await myQueueItem.DeadLetterAsync();
}
}
But, some times I'm getting MessageLockLost, exceptions if I call DeadLetterAsync, AbandonAsync() etc., explicitly in my code even though the lock was not actually lost.
Can anyone suggest me, what is the best way to move a brokered message to DeadLetter queue to handle custom exceptions.
Thanks.

Not exactly what you want for, but a creative workaround:
Add an output Service Bus binding to your function. In place of dead letter'ing the message, add a new message to the output:
public static async Task Run(
[ServiceBusTrigger("myqueue", Connection = "mysb")] BrokeredMessage myQueueItem,
[ServiceBus("mydlq", Connection = "mysb")] IAsyncCollector<BrokeredMessage> dlq,
TraceWriter log)
{
try
{
// process message logic..
}
catch(CustomException ex)
{
// forward to "DLQ" when exception occurs
var dlqMessage = ...; // you need to create a new message here
await dlq.AddAsync(dlqMessage);
}
}
The original message will be successfully completed.
Note that you need to create a new BrokeredMessage and carefully copy all the data and metadata from the original message. If you have no metadata, maybe it's better to change the type of collector to something simple like IAsyncCollector<string>.

Related

best practices with poison message handling for Azure service bus topic

Dealing with poison messages (throwing exception while consuming) from Azure Service Bus can lead to loops till number of retries has reached maxDeliveryCount setting of topic subscription.
Does the SequenceNumber of message added by Azure Service bus keeps on increasing on each failed attempt till it reaches maxDeliveryCount ?
Setting maxDeliveryCount = 1, is that best practice to deal with poison messages so that consumer never attempt twice to process message once it failed
Best practices depend on your application and your retry approach.
Most of time I noticed message get failed
Dependent service not available (Redis, SQL connection issue)
Faulty message (message doesn't have a mandatory parameter or some value is incorrect)
Process code issue (bug in message processing code)
For the 1st and 3rd scenario, I created C# web job to run and reprocess deadletter message.
Below is my code
internal class Program
{
private static string connectionString = ConfigurationSettings.AppSettings["GroupAssetConnection"];
private static string topicName = ConfigurationSettings.AppSettings["GroupAssetTopic"];
private static string subscriptionName = ConfigurationSettings.AppSettings["GroupAssetSubscription"];
private static string databaseEndPoint = ConfigurationSettings.AppSettings["DatabaseEndPoint"];
private static string databaseKey = ConfigurationSettings.AppSettings["DatabaseKey"];
private static string deadLetterQueuePath = "/$DeadLetterQueue";
private static void Main(string[] args)
{
try
{
ReadDLQMessages(groupAssetSyncService, log);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
throw;
}
finally
{
documentClient.Dispose();
}
Console.WriteLine("All message read successfully from Deadletter queue");
Console.ReadLine();
}
public static void ReadDLQMessages(IGroupAssetSyncService groupSyncService, ILog log)
{
int counter = 1;
SubscriptionClient subscriptionClient = SubscriptionClient.CreateFromConnectionString(connectionString, topicName, subscriptionName + deadLetterQueuePath);
while (true)
{
BrokeredMessage bmessgage = subscriptionClient.Receive(TimeSpan.FromMilliseconds(500));
if (bmessgage != null)
{
string message = new StreamReader(bmessgage.GetBody<Stream>(), Encoding.UTF8).ReadToEnd();
syncService.UpdateDataAsync(message).GetAwaiter().GetResult();
Console.WriteLine($"{counter} message Received");
counter++;
bmessgage.Complete();
}
else
{
break;
}
}
subscriptionClient.Close();
}
}
For 2nd scenario, we manually verify deadletter messages (Custom UI/ Service Bus explore), sometimes we correct message data or sometimes we purge message and clear queue.
I won't recommend maxDeliveryCount=1. If some network/connection issue occurs, the built-in retry will process and clear from the queue. When I was working in a finance application, I was keeping maxDeliveryCount=5 while in my IoT application is maxDeliveryCount=3.
If you are reading messages in batch, a complete batch will re-process if an error occurred any of message.
SequenceNumber The sequence number can be trusted as a unique identifier since it is assigned by a central and neutral authority and not by clients. It also represents the true order of arrival, and is more precise than a time stamp as an order criterion, because time stamps may not have a high enough resolution at extreme message rates and may be subject to (however minimal) clock skew in situations where the broker ownership transitions between nodes.

In queue-triggered Azure Webjobs can an Azure Storage Queue message be modified after webjob function failure but before poisoning?

I've got queue-triggered functions in my Azure webjobs. Normal behavior of course is when the function fails MaxDequeueCount times the message is put into the appropriate poison queue. I would like to modify the message after the error but before poison queue insertion. Example:
Initial message:
{ "Name":"Tom", "Age", 30" }
And upon failure I want to modify the message as follows and have the modified message be inserted into the poison queue:
{ "Name":"Tom", "Age", 30", "ErrorMessage":"Unable to find user" }
Can this be done?
According to the Webjobs documentation, messages will get put on the poison queue after 5 failed attempts to process the message:
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.
Source: https://github.com/Azure/azure-webjobs-sdk/wiki/Queues#poison
This is the automatic behavior. But you can still handle exceptions in your WebJobs Function code (so the exception doesn't leave your function and automatic poison message handling is not triggered) and put a modified message to the poison queue using output bindings.
Another option would be to check the dequeueCount property which indicates how many times the message was tried to be processed.
You can get the number of times a message has been picked up for
processing by adding an int parameter named dequeueCount to your
function. You can then check the dequeue count in function code and
perform your own poison message handling when the number exceeds a
threshold, as shown in the following example.
public static void CopyBlob(
[QueueTrigger("copyblobqueue")] string blobName, int dequeueCount,
[Blob("textblobs/{queueTrigger}", FileAccess.Read)] Stream blobInput,
[Blob("textblobs/{queueTrigger}-new", FileAccess.Write)] Stream blobOutput,
TextWriter logger)
{
if (dequeueCount > 3)
{
logger.WriteLine("Failed to copy blob, name=" + blobName);
}
else
{
blobInput.CopyTo(blobOutput, 4096);
}
}
(also taken from above link).
Your function signature could look like this
public static void ProcessQueueMessage(
[QueueTrigger("myqueue")] CloudQueueMessage message,
[Queue("myqueue-poison")] CloudQueueMessage poisonMessage,
TextWriter logger)
The default maximum retry time is 5. you also can set this value by yourself using the property Queues.MaxDequeueCount of the JobHostConfiguration() instance, code like below:
static void Main(string[] args)
{
var config = new JobHostConfiguration();
config.Queues.MaxDequeueCount = 5; // set the maximum retry time
var host = new JobHost(config);
host.RunAndBlock();
}
Then you can update the failed queue message when the maximum retry time have reached. You can specify a non-existing Blob container to enforce the retry mechanism. Code like below:
public static void ProcessQueueMessage([QueueTrigger("queue")] CloudQueueMessage message, [Blob("container/{queueTrigger}", FileAccess.Read)] Stream myBlob, ILogger logger)
{
string yourUpdatedString = "ErrorMessage" + ":" + "Unable to find user";
string str1 = message.AsString;
if (message.DequeueCount == 5) // here, the maximum retry time is set to 5
{
message.SetMessageContent(str1.Replace("}", "," + yourUpdatedString + "}")); // modify the failed message here
}
logger.LogInformation($"Blob name:{message} \n Size: {myBlob.Length} bytes");
}
When the above is done, you can see the updated queue message in the queue-poison.
UPDATED:
Since CloudQueueMessage is a sealed class, we cannot inherit it.
For your MySpecialPoco message, you can use JsonConvert.SerializeObject(message), code like below:
using Newtonsoft.Json;
static int number = 0;
public static void ProcessQueueMessage([QueueTrigger("queue")] object message, [Blob("container/{queueTrigger}", FileAccess.Read)] Stream myBlob, ILogger logger)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
CloudQueueClient queueClient = storageAccount.CreateCloudQueueClient();
CloudQueue queue = queueClient.GetQueueReference("queue-poison");// get the poison queue
CloudQueueMessage msg1 = new CloudQueueMessage(JsonConvert.SerializeObject(message));
number++;
string yourUpdatedString = "\"ErrorMessage\"" + ":" + "\"Unable to find user\"";
string str1 = msg1.AsString;
if (number == 5)
{
msg1.SetMessageContent(str1.Replace("}", "," + yourUpdatedString + "}"));
queue.AddMessage(msg1);
number = 0;
}
logger.LogInformation($"Blob name:{message} \n Size: {myBlob.Length} bytes");
}
But the bad thing is that, both the original / updated queue messages are written into poison queue.

Azure functions with service bus: Unable to "complete" and access properties of a brokered message

This is my working code in a console app:
Writer (working code):
static void Main(string[] args)
{
Console.WriteLine("Starting..");
string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "xxx");
// Create message, passing a string message for the body.
BrokeredMessage message = new BrokeredMessage("");
// Set some addtional custom app-specific properties.
message.Properties["UserCode"] = "HELLOOO22353";
message.Properties["UserId"] = "4511";
try
{
// Send message to the queue.
Client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
}
Console.WriteLine("Complete..");
//Console.ReadKey();
}
Reader: (working code)
static void Main(string[] args)
{
Console.WriteLine("Starting");
string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
QueueClient Client = QueueClient.CreateFromConnectionString(connectionString, "xxx");
OnMessageOptions options = new OnMessageOptions();
options.AutoComplete = false;
options.AutoRenewTimeout = TimeSpan.FromMinutes(1);
Client.OnMessage((message) =>
{
try
{
string sMessage = " UserCode: " + message.Properties["UserCode"];
Console.WriteLine("Found new User - " + sMessage);
// Remove message from queue.
message.Complete();
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
// Indicates a problem, unlock message in queue.
message.Abandon();
}
}, options);
Console.ReadKey();
}
Now, this is the code I have in Azure functions which isn't working:
public static void Run(BrokeredMessage myMessage, TraceWriter log)
{
log.Info($"C# ServiceBus queue trigger function processed message: {myMessage}");
log.Info("1111");
log.Info(myMessage.MessageId); // this works
myMessage.Complete(); // fails: Microsoft.ServiceBus: Operation is not valid due to the current state of the object.
log.Info(myMessage.Properties["UserCode"].ToString()); // fails: myMessage.Properties is empty for some reason
}
What I'm failing to understand is why the Reader console app is able to read and complete message correctly but Azure function one (which is essentially based on the same idea) isn't. Both the codes are using the same version of Windows.ServiceBus package.
You do not need to complete the message yourself when using the ServiceBus trigger with Azure Functions. The ServiceBus trigger automatically uses PeekLock mode and will handle automatically completing, abandoning and renewing the message lock for you. You can also take control the finer details of this behavior via host.json settings for your function.

How to get queue message type

I'm using Azure Storage Queues and I want to write some code that retrieves all queues, and then finds a handler that can process the message in this queue. For that I defined an interface like this:
public interface IHandler<T>
I have multiple implementations of this interface, like these: IHandler<CreateAccount> or IHandler<CreateOrder>. I use 1 queue per message type, so the CreateAccount messages would go into the create-account-queue.
How do I hook these up? In order to find the right Handler class for a message, I first need to know the message type, but it seems that CloudQueueMessage objects don't contain that information.
Not really an answer to your question but I will share how we're handling exact same situation in our application.
In our application, we're sending different kinds of messages like you are and handling those messages in a background process.
What we're doing is including the message type in the message body itself. So our message typically looks like:
message: {
type: 'Type Of Message',
contents: {
//Message contents
}
}
One key difference is that all messages go in a single queue (instead of different queues in your case). The receiver (background process) just polls one queue, gets the message and identifies the type of message and call handler for that message accordingly.
You can associate metadata with each queue. Since you mentioned that you use one queue per message type, you could put the handler name in the metadata for each queue. You can then enumerate all queues and get the metadata per queue that tells you what type of handler you should use. Here's a quick console app that demonstrates what I think you're asking for:
using System;
using System.Collections.Generic;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.Queue;
namespace QueueDemo
{
class Program
{
static void Main(string[] args)
{
//get a ref to our account.
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true;");
CloudQueueClient cloudQueueClient = storageAccount.CreateCloudQueueClient();
//create our queues and add metadata showing what type of class each queue contains.
CloudQueue queue1 = cloudQueueClient.GetQueueReference("queue1");
queue1.Metadata.Add("classtype", "classtype1");
queue1.CreateIfNotExists();
CloudQueue queue2 = cloudQueueClient.GetQueueReference("queue2");
queue2.Metadata.Add("classtype", "classtype2");
queue2.CreateIfNotExists();
//enumerate our queues in a storage account and look at their metadata...
QueueContinuationToken token = null;
List<CloudQueue> cloudQueueList = new List<CloudQueue>();
List<string> queueNames = new List<string>();
do
{
QueueResultSegment segment = cloudQueueClient.ListQueuesSegmented(token);
token = segment.ContinuationToken;
cloudQueueList.AddRange(segment.Results);
}
while (token != null);
try
{
foreach (CloudQueue cloudQ in cloudQueueList)
{
//call this, or else your metadata won't be included for the queue.
cloudQ.FetchAttributes();
Console.WriteLine("Cloud Queue name = {0}, class type = {1}", cloudQ.Name, cloudQ.Metadata["classtype"]);
queueNames.Add(cloudQ.Name);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception thrown listing queues: " + ex.Message);
throw;
}
//clean up after ourselves and delete queues.
foreach (string oneQueueName in queueNames)
{
CloudQueue cloudQueue = cloudQueueClient.GetQueueReference(oneQueueName);
cloudQueue.DeleteIfExists();
}
Console.ReadKey();
}
}
}
However, it might be easier to subclass QueueMessage, then dequeue each message and identify what subclass you're currently looking at, then pass it to the proper handler.

Azure WebJobs SDK ErrorTrigger

This question is related to QueueTrigger and ErrorTrigger in WebJobs.
I have one Process Queue method inside one public class(see code below). When any exception comes(e.g Timeout exception) we have 5 retry attempt to process the queue. After 5 unsuccessful attempt we want to send alert mail to one recipient. In Order to do that I have added one method using ErrorTrigger attribute( see code below) and setting threshold and window value. But in my case after 5 retry it does not hit the ErrorTrigger method. Could you please look this code and let me know where I am doing wrong? And if there is other way to send alert mail after 5 unsuccessful retry attempt, please help me.
public static class ProcessQueue
{
public static void ProcessQueue([QueueTrigger("testqueue")] string queueMessage, TextWriter logger)
{
try
{
if (logger != null)
{
//logger.WriteLine(filter.GetDetailedMessage(5));
//message.Text = filter.GetDetailedMessage(1);
}
throw new TimeoutException(); // Intentionaly throwing timeout exception
}
catch (Exception ex)
{
throw ex;
}
}
public static void ErrorMonitor(
[ErrorTrigger("00:00:10", 4)] TraceFilter filter, TextWriter log,
[SendGrid(To = "abc#gmail.com", Subject = "Error!")]
SendGridMessage message)
{
// log last 5 detailed errors to the Dashboard
log.WriteLine("Test");
message.Text = "Failed";
}
}
In your example [ErrorTrigger("00:00:10", 4)] isn't that error trigger: "If there are 4 errors in a 10 second period" (see Error-Monitoring) - which might be bit fast for a queue.
Try [ErrorTrigger("00:05:00", 4)] = four failures in a 5 minute period

Resources