How does Polly’s Fallback work?

Asked

Viewed 471 times

4

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

From what I understand he executes another action if the first fails, but that’s 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 simulating correctly, but is not called the second method after the error of the first.

inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

1 answer

1

I was riding the wrong way :( of course, what I really needed was to pass the action to POLLY’s Fallback method without doing anything else, the way it was implemented was not giving error, but it does not miss the form of calling a new action to be executed but rather a way to record logs, this form has also been marked as Obsolete.

Obsolete("This Overload is deprecated and will be Removed in a Future release. Prefer the Overload in which Both fallbackAction and onFallback take a Context input Parameter.")]

There is the 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.