Example #1
0
        /// <summary>
        /// Processes the asynchronous event.
        /// </summary>
        /// <param name="eventData">The event data.</param>
        /// <returns></returns>
        public AsyncHandlerResult ProcessAsyncEvent(AsyncEvent eventData)
        {
            AsyncServiceException.Assert(eventData != null, "异步服务池处理器只能处理异步事件。");
            if (_queueManager.ShuttingDown)
            {
                _queueManager.TrackCompleteOutstandingOperation(eventData, null, false);
                return null;
            }

            MyTrace.Write(TraceCategory.AsyncService, TraceLevel.Info,
                          "正在处理异步作业。Id: {0} 作业类型: {1}", eventData.EventId, eventData.OperationType);
            var result = _queueManager.HandleSpecialTransition(eventData);
            if (result != null)
            {
                _queueManager.TrackCompleteOutstandingOperation(eventData, result, false);
            }
            else
            {
                SemaphoreSlim semaphoreSlim = null;
                bool hasDone = false;
                if (
                    _queueManager.Configuration.OperationTypeThrottle.TryGetValue(eventData.OperationType,
                                                                                  out semaphoreSlim) &&
                    !semaphoreSlim.Wait(0))
                {
                    var failure = new AsyncFailedResult(new AsyncServiceException("服务正忙且请求未被完成,请稍后重试。"))
                        {
                            FriendlyMessage = "服务正忙且请求未被完成,请稍后重试。"
                        };
                    result = new AsyncRetryResult(failure);
                    hasDone = true;
                }
                if (!hasDone)
                    _queueManager.TrackBeginExecutingOperation(eventData);

                if (_queueManager.ShuttingDown)
                {
                    _queueManager.TrackCompleteOutstandingOperation(eventData, result, false);
                    return result;
                }

                try
                {
                    if (!hasDone)
                        result = _handler(eventData);
                }
                catch (Exception ex)
                {
                    ErrorAction action = _errorHandler.Handle(ex);
                    AsyncServiceException.Assert(action != null, "错误处理后的动作不应为null。");
                    string errorFormat = action.ErrorMessage == null ? string.Empty : action.ErrorMessage;
                    result = new AsyncFailedResult(ex, action.ErrorCode, errorFormat);
                    switch (action.Type)
                    {
                        case ErrorActionType.Retry:
                            result = new AsyncRetryResult((AsyncFailedResult) result);
                            break;
                        case ErrorActionType.Fail:
                            break;
                        case ErrorActionType.Suspend:
                            if (action.ErrorCode == -2147204743)
                            {
                                result = new AsyncPausedResult(DateTime.Now.AddMinutes(0));
                                break;
                            }
                            else
                            {
                                result = new AsyncSystemPausedResult(action.ErrorCode, "{0}", action.ErrorMessage);
                                break;
                            }
                        default:
                            result = new AsyncSystemPausedResult(ex);
                            throw;
                    }
                }
                finally
                {
                    EndAsynchronousEventProcessing(eventData, result);
                }
            }

            return result;
        }
Example #2
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AsyncRetryResult"/> class.
 /// </summary>
 /// <param name="failure">The failure.</param>
 public AsyncRetryResult(AsyncFailedResult failure)
     : base(10, failure.ErrorCode, failure.ErrorMessage, new object[0])
 {
     base.FriendlyMessage = failure.FriendlyMessage;
 }