Пример #1
0
        public async Task <OperateResult> ExecuteAsync(IStorageConnection connection, IFetchedMessage fetched)
        {
            var message = await connection.GetReceivedMessageAsync(fetched.MessageId);

            if (message == null)
            {
                _logger.LogError($"Can not found the `message` at cap received message table, message id:{fetched.MessageId} !!!");
                return(OperateResult.Failed());
            }

            try
            {
                var sp = Stopwatch.StartNew();
                await _stateChanger.ChangeStateAsync(message, new ProcessingState(), connection);

                if (message.Retries > 0)
                {
                    _logger.JobRetrying(message.Retries);
                }

                var result = await _subscriberExecutor.ExecuteAsync(message);

                sp.Stop();

                var state = GetNewState(result, message);

                await _stateChanger.ChangeStateAsync(message, state, connection);

                fetched.RemoveFromQueue();

                if (result.Succeeded)
                {
                    _logger.JobExecuted(sp.Elapsed.TotalSeconds);
                }

                return(OperateResult.Success);
            }
            catch (SubscriberNotFoundException ex)
            {
                _logger.LogError(ex.Message);

                AddErrorReasonToContent(message, ex);

                ++message.Retries;  //issue: https://github.com/dotnetcore/CAP/issues/90

                await _stateChanger.ChangeStateAsync(message, new FailedState(), connection);

                fetched.RemoveFromQueue();

                return(OperateResult.Failed(ex));
            }
            catch (Exception ex)
            {
                _logger.ExceptionOccuredWhileExecuting(message.Name, ex);

                fetched.Requeue();

                return(OperateResult.Failed(ex));
            }
        }
Пример #2
0
        public async Task <OperateResult> ExecuteAsync(IStorageConnection connection, IFetchedMessage fetched)
        {
            var message = await connection.GetReceivedMessageAsync(fetched.MessageId);

            try
            {
                var sp = Stopwatch.StartNew();
                await _stateChanger.ChangeStateAsync(message, new ProcessingState(), connection);

                if (message.Retries > 0)
                {
                    _logger.JobRetrying(message.Retries);
                }
                var result = await ExecuteSubscribeAsync(message);

                sp.Stop();

                var newState = default(IState);
                if (!result.Succeeded)
                {
                    var shouldRetry = await UpdateMessageForRetryAsync(message, connection);

                    if (shouldRetry)
                    {
                        newState = new ScheduledState();
                        _logger.JobFailedWillRetry(result.Exception);
                    }
                    else
                    {
                        newState = new FailedState();
                        _logger.JobFailed(result.Exception);
                    }
                }
                else
                {
                    newState = new SucceededState();
                }
                await _stateChanger.ChangeStateAsync(message, newState, connection);

                fetched.RemoveFromQueue();

                if (result.Succeeded)
                {
                    _logger.JobExecuted(sp.Elapsed.TotalSeconds);
                }

                return(OperateResult.Success);
            }
            catch (SubscriberNotFoundException ex)
            {
                _logger.LogError(ex.Message);
                return(OperateResult.Failed(ex));
            }
            catch (Exception ex)
            {
                _logger.ExceptionOccuredWhileExecutingJob(message?.Name, ex);
                return(OperateResult.Failed(ex));
            }
        }
Пример #3
0
        public async Task <OperateResult> ExecuteAsync(IStorageConnection connection, IFetchedMessage fetched)
        {
            var message = await connection.GetPublishedMessageAsync(fetched.MessageId);

            try
            {
                var sp = Stopwatch.StartNew();
                await _stateChanger.ChangeStateAsync(message, new ProcessingState(), connection);

                if (message.Retries > 0)
                {
                    _logger.JobRetrying(message.Retries);
                }
                var result = await PublishAsync(message.Name, message.Content);

                sp.Stop();

                IState newState;
                if (!result.Succeeded)
                {
                    var shouldRetry = UpdateMessageForRetryAsync(message);
                    if (shouldRetry)
                    {
                        newState = new ScheduledState();
                        _logger.JobFailedWillRetry(result.Exception);
                    }
                    else
                    {
                        newState = new FailedState();
                        _logger.JobFailed(result.Exception);
                    }
                    message.Content = Helper.AddExceptionProperty(message.Content, result.Exception);
                }
                else
                {
                    newState = new SucceededState(_options.SucceedMessageExpiredAfter);
                }
                await _stateChanger.ChangeStateAsync(message, newState, connection);

                fetched.RemoveFromQueue();

                if (result.Succeeded)
                {
                    _logger.JobExecuted(sp.Elapsed.TotalSeconds);
                }

                return(OperateResult.Success);
            }
            catch (Exception ex)
            {
                fetched.Requeue();
                _logger.ExceptionOccuredWhileExecuting(message?.Name, ex);
                return(OperateResult.Failed(ex));
            }
        }