Esempio n. 1
0
        static async Task ExecuteWithRetryOnTransient(Func <Task> action, IRetryPolicy retryPolicy, string uniqueActionIdentifier)
        {
            Exception lastException = null;

            int attemptNumber = 0;

            while (retryPolicy.ShouldExecute())
            {
                attemptNumber++;
                Stopwatch timer = Stopwatch.StartNew();
                try
                {
                    ServiceFabricProviderEventSource.Tracing.LogOrchestrationInformation(uniqueActionIdentifier, null, "Executing action");
                    await action();

                    timer.Stop();
                    ServiceFabricProviderEventSource.Tracing.LogMeasurement($"{uniqueActionIdentifier}, Attempt Number : {attemptNumber}, Result : Success", timer.ElapsedMilliseconds);
                    return;
                }
                catch (Exception e)
                {
                    timer.Stop();
                    lastException = e;
                    bool shouldRetry = ExceptionUtilities.IsRetryableFabricException(e);

                    ExceptionUtilities.LogReliableCollectionException(uniqueActionIdentifier, attemptNumber, e, shouldRetry);
                    ServiceFabricProviderEventSource.Tracing.LogMeasurement($"{uniqueActionIdentifier}, Attempt Number : {attemptNumber}, ShouldRetry : {shouldRetry}", timer.ElapsedMilliseconds);

                    if (shouldRetry)
                    {
                        await Task.Delay(retryPolicy.GetNextDelay());
                    }
                    else
                    {
                        throw;
                    }
                }
            }

            throw lastException;
        }