How is Polly Fallback working?

4

I'm trying to understand how Polly Fallback works, but I'm not getting the implementation right.

As I understand it, it performs another action if the first fails, but that is not what is happening.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace CircuitBreakingPolly
{
    public class Program
    {
        public static int n1 = 2;
        public static int n2 = 0;
        public static DateTime programStartTime;
        private static List<Produtos> produtos = new List<Produtos>();

        static void Main(string[] args)
        {
            var pmp = new PoliticasManipulacaoPolly();
            Task result = pmp.ExecuteFallback(() => CallTask(), () => CallTask2());

            Console.WriteLine(result.ToString());
            Console.ReadKey();
        }

        public async static Task CallTask()
        {
            if (DateTime.Now < programStartTime.AddSeconds(10))
            {
                Console.WriteLine("Task falhou.");
                throw new TimeoutException();
            }
            await Task.Delay(TimeSpan.FromSeconds(1));
            Console.WriteLine("Task completa.");
        }

        public async static Task CallTask2()
        {
            if (DateTime.Now < programStartTime.AddSeconds(10))
            {
                Console.WriteLine("Task falhou.");
                throw new TimeoutException();
            }
            await Task.Delay(TimeSpan.FromSeconds(1));
            Console.WriteLine("Task completa.");
        }
    }
}

My method;

public Task ExecuteFallback(Func<Task> action, Func<Task> fallbackAction)
{
    Program.programStartTime = DateTime.Now;
    Task result = null;

    try
    {
       var esult2 = Policy.Handle<Exception>()
            .Fallback(() => fallbackAction(), onFallback: (exception, context) =>
            {
                Console.WriteLine(exception.Message);
                //Log(exception);
                throw exception;
            })
            .Execute(action);

    }
    catch (AggregateException ex)
    {
        Console.WriteLine(ex.Message);
        //Log(ex);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
        //Log(ex);
    }

    return result;
}

In the call of my first method this is simulating correctly, but the second method is not called after the first error.

    
asked by anonymous 13.07.2017 / 15:10

1 answer

0

I was riding the wrong way :( of course, what I really needed was to move the action to the POLLY Fallback method without needing to do anything else, as it was implemented was not giving error, but does not miss the form of calls a new action to execute, but a way to write logs, this form has already been marked Obsolete.

  

Obsolete ("This overload is deprecated and will be removed in the future   release. Prefer the overload in which both fallbackAction and   onFallback take a Context input parameter. ")]

There is PolicyWrap that makes it possible to execute more than one policy.

The solution found;

public Task ExecuteFallbackWrap(Func<Task> action, Func<Task> fallbackAction)
{
    Program.programStartTime = DateTime.Now;
    Task result = null;

    var politicaWithFallback = Policy
        .Handle<Exception>()
        .Fallback(() => fallbackAction());

    var politicaRetry = Policy
        .Handle<Exception>()
        .Retry(2);
    try
    {
        result = politicaWithFallback.Execute(action);

        // ou combinado
        var mixedPolicy = Policy.Wrap(politicaWithFallback, politicaRetry).Execute(action);
    }
    catch (AggregateException ex)
    {
        Log(ex);
    }
    catch (Exception ex)
    {
        Log(ex);
    }

    return result;
}
    
03.08.2017 / 19:38