Ejemplo n.º 1
0
        /// <summary>
        /// Create the default <see cref="RetryPolicyOption"/>.
        /// </summary>
        /// <returns>A <see cref="RetryPolicyOption"/>.</returns>
        public static RetryPolicyOption DefaultRetryPolicy()
        {
            var retry = new RetryPolicyOption();

            retry.MaxRetries = 1;
            retry.ShouldRetryOn <System.Exception>();

            return(retry);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Execute any actions of the <see cref="IFluentDbCommand"/>, with the retry policy.
        /// </summary>
        /// <param name="dbActions">Actions to be executed.</param>
        /// <param name="retry">The <see cref="RetryPolicyOption"/>.</param>
        /// <param name="throwException">Define if the exception should be throwned.</param>
        /// <returns>The <see cref="DbCommandResult"/>.</returns>
        public static async Task <DbCommandResult> SafeExecuteDbCommandAction(Func <Task <DbCommandResult> > dbActions, RetryPolicyOption?retry, bool throwException = false)
        {
            int  retries    = 1;
            bool isExecuted = false;

            Stopwatch stopwatch = new Stopwatch();

            if (retry != null && retry.MaxRetries >= 1)
            {
                retries = retry.MaxRetries;
            }
            else
            {
                // Create the default retry policy
                retry   = RetryPolicyOption.DefaultRetryPolicy();
                retries = retry.MaxRetries;
            }

            DbCommandResult?result = null;

            stopwatch.Start();

            Stack <System.Exception> exceptions = new Stack <System.Exception>();

            // Execute the actions inside the retry policy
            while (!isExecuted && retries > 0)
            {
                try
                {
                    result = await dbActions();

                    // Mark that actions have been executed.
                    isExecuted = true;
                }
                catch (System.Exception e)
                {
                    // Cheick if the exception is concerned by the retry policy
                    if (retry != null && retry.Exceptions !.Contains(e.GetType()))
                    {
                        // decrease the retries count;
                        retries   -= 1;
                        isExecuted = false;
                    }