S M
S M

Reputation: 3233

Azure functions: Move to poison blob without retry if a particular exception is thrown

I have a blob triggered function, in which a data is deserialized from a Json string and then the further process are done and save the data to db.

My blob trigger function max retry is set as 5. Means if the blob process fails it will retry for 5 times and then move the blob to poison blob.

If the deserialization is failed, then there is no need to retry it for 5 times. So, if the deserialzation exception is thrown I need to move the blob to poison blob without retry. Is there any way to handle this?

Upvotes: 5

Views: 2436

Answers (2)

bvpb
bvpb

Reputation: 1476

A cleaner way would be to set the maxDequeueCount to 1. Add the following to your host.json file :

"extensions": {
  "queues": {
      "maxDequeueCount": 1
  }
}

This effectively sets the max retries to 0 and a message is added to the poison blob queue as soon as an exception bubbles all the way up in your function.

The documentation is pretty confusing as the setting is within the queues section and doesn't look like it's related to the blob trigger.

I was able to find this from the WebJobs documentation though: https://learn.microsoft.com/en-us/azure/visual-studio/vs-storage-webjobs-getting-started-blobs#how-to-handle-poison-blobs

Upvotes: 1

Pawel Maga
Pawel Maga

Reputation: 5807

There is no built-in solution for such case afaik, but you can add an output binding to your poison queue and insert a message manually in the form described here:

https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-storage-blob#trigger---poison-blobs

The queue message for poison blobs is a JSON object that contains the following properties:

FunctionId (in the format .Functions.)

BlobType ("BlockBlob" or "PageBlob")

ContainerName

BlobName

ETag (a blob version identifier, for example: "0x8D1DC6E70A277EF")

[FunctionName("blobtrigger")]
public static async Task Run(ILogger log, ExecutionContext executionContext,
    [BlobTrigger("blobs/{name}")] Stream blob,
    [Queue("webjobs-blobtrigger-poison")] CloudQueue poisonQueue)
{
    try {
        // do something
        throw new JsonSerializationException();
    }
    catch (JsonSerializationException ex)
    {
        log.LogError(ex, ex.Message);
        await poisonQueue.AddMessageAsync(new CloudQueueMessage()); // your message
    }
}

Upvotes: 3

Related Questions