Ken Kaneki
Ken Kaneki

Reputation: 57

How to implement Polly correctly in my code to avoid freezing

I'm having a problem with Polly causing the thread to freeze. As you can see under the GetData() method is going to throw an exception. I want to do is to continue to Dostuff1 and 2 when exceptions occur in the GetData() method in DoStuff(); But it seems like after retrying with polly it is just stuck and doing nothing at all...

How can I fix this?

private async Task StartUp()
{
        await DoStuff());
        // await next task DoStuff1();
        // await next task DoStuff2();
}


private async Task DoStuff()
{
        var data= await Task.WhenAll(Task.Run(() => GetData()));

        // Do stuff with the data
}


private async Task<Data> GetData()
{
     //read filepaths from file.

     var tasks = filePaths
             .Select(f => polly.MyPolicy().Execute(() => ReadDataFromCSV()))
             .ToArray();
    Return await tasks;
}

private Dictionary<string, DateTime?> ReadDataFromCSV()
{

     ///Throw exception....
}

    public PolicyForGetData()
    {
        return Policy
                 .Handle<Exception>()
                 .WaitAndRetry(
                    retryCount: 2,
                    sleepDurationProvider: t => TimeSpan.FromSeconds(5),
                    onRetry: (ex, t, i, c) => {
                        Console.WriteLine(ex.Message);
                    }
                 );
    }

Upvotes: 1

Views: 1109

Answers (1)

Andrew Skirrow
Andrew Skirrow

Reputation: 3451

I think you're misunderstanding how Polly behaves. Assuming GetData() always throws then your application will work like this

  1. GetData() throws
  2. Polly catches the retries for the first time
  3. GetData() throws again
  4. Polly catches the retries for the second time
  5. GetData() throws for a final time.
  6. Polly won't catch or retry the exception because you've only asked it to retry twice, instead the Exception escapes to the StartUp() method which prevents DoStuff1() and DoStuff2() from being called.

For example. The output of this program:

class Program
{
    static void Main(string[] args)
    {
        try
        {
            Policy
                 .Handle<Exception>()
                 .WaitAndRetry(
                    retryCount: 2,
                    sleepDurationProvider: t => TimeSpan.FromSeconds(5),
                    onRetry: (ex, t, i, c) => {
                        Console.WriteLine("OnRetry");
                    }
                 )
                 .Execute(() => ErrorMethod());

            Console.WriteLine("We never get here");
        }
        catch (Exception)
        {
            Console.WriteLine("Exception Handler");
        }
    }

    private static void ErrorMethod()
    {
        throw new InvalidOperationException("Badness");
    }
}

Is

OnRetry
OnRetry
Exception Handler

Upvotes: 1

Related Questions