Azure Functions Event Hub binding retry

1.6k Views Asked by At

I have a HTTP triggered function with an output binding where I am using IAsyncCollector<string> outputEventHubMessages outputEventHubMessages.AddAsync("message") to send messages to an Event Hub.

If, for some reason, the Event Hub has an outage/blip and doesn't accept the message, will the Azure Function retry the send?

Thanks.

2

There are 2 best solutions below

0
On

From what I've seen, HTTP binding will return 500 to you in case another output binding failed to do its job (e.g. connect to Event Hub). It won't retry the operation. Here is an example of response body:

{
  "id": "112a4e4f-2c6b-4b43-85a6-a8d801ca6f23",
  "requestId": "0c0514e3-27dc-476f-8a85-3c8fe77aa762",
  "statusCode": 500,
  "errorCode": 0,
  "message": "Exception while executing function: Functions.SoFunc -> Error while handling parameter outputEventHubMessage after function returned: -> Put token failed. status-code: 404, status-description: The messaging entity 'sb://tmdevmike.servicebus.windows.net/outeventhub' could not be found.."
}

I can't quote any official docs though.

0
On

It will be nice to have this feature built in the output bindings. However, the following code snippet shows an example of the retrying message to the Event Hub. The concept is based on the catching an error on the output binding and sending a message to the storage queue for retrying process:

    [FunctionName("Function4")]       
    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Function, "post")] HttpRequestMessage req,
        [EventHub("%EventHub%", Connection = "connectionEventHub")] IAsyncCollector<string> outputEventHubMessages,
        [Queue("%RetryQueue%")] IAsyncCollector<string> outputQueueRetryMessages,
        TraceWriter log)
    {
        log.Info("C# HTTP trigger function processed a request.");

        // Get request body
        string message = JsonConvert.SerializeObject(JObject.Parse(await req.Content.ReadAsStringAsync()));

        try
        {
            await outputEventHubMessages.AddAsync(message);
            await outputEventHubMessages.FlushAsync();
        }
        catch(Exception ex)
        {
            log.Error(ex.Message);
            //await Task.Delay(5000);
            await outputQueueRetryMessages.AddAsync(message);
        }

        return req.CreateResponse(HttpStatusCode.OK);
    }

the queue can be configured in the host.json file:

{
  "queues": {
  "maxDequeueCount": 3,
  "visibilityTimeout": "00:00:30"
  }
}