Exemplo n.º 1
0
 /// <summary>
 /// 执行包装方法
 /// </summary>
 /// <returns></returns>
 public Task ExecuteAsync()
 {
     return(Task.Run(async() =>
     {
         while (invokeCount <= this.retryCount)
         {
             invokeCount++;
             try
             {
                 var t = doAction();
                 await t;
             }
             catch (Exception ex)
             {
                 if (this.hasExceptionFailure != null && !this.hasExceptionFailure(new RetryFaiureException(ex, invokeCount)))
                 {
                     return;
                 }
                 if (invokeCount > this.retryCount)
                 {
                     var _ex = new RetryFaiureException(ex, this.retryCount);
                     if (failureCallback != null && !failureCallback(_ex))
                     {
                         return;
                     }
                     throw _ex;
                 }
             }
             if (this.retryInterval > 0)
             {
                 await Task.Delay(this.retryInterval);
             }
         }
     }));
 }
Exemplo n.º 2
0
        /// <summary>
        /// 执行包装方法
        /// </summary>
        /// <returns></returns>
        public T Execute()
        {
            T r;

            while (invokeCount++ <= this.retryCount)
            {
                try
                {
                    r = doAction();
                    if (this.hasResultFailure == null || !this.hasResultFailure(r))
                    {
                        return(r);
                    }
                }
                catch (Exception ex)
                {
                    if (this.hasExceptionFailure != null && !this.hasExceptionFailure(new RetryFaiureException(ex, invokeCount)))
                    {
                        return(default(T));
                    }

                    if (invokeCount > this.retryCount)
                    {
                        var _ex = new RetryFaiureException(ex, this.retryCount);
                        if (failureCallback != null && !failureCallback(_ex))
                        {
                            return(default(T));
                        }
                        throw _ex;
                    }
                    if (this.retryInterval > 0)
                    {
                        Thread.Sleep(this.retryInterval);
                    }

                    continue;
                }

                if (this.retryInterval > 0)
                {
                    Thread.Sleep(this.retryInterval);
                }
            }

            throw new RetryFaiureException("retry has max. but proccess is error", this.retryCount);
        }