Exemplo n.º 1
0
 private void DoProcessing(BlockingCollection <CapReceivedMessage> receivedMessageQueue)
 {
     try
     {
         foreach (var message in receivedMessageQueue.GetConsumingEnumerable(_cts.Token))
         {
             _executor.ExecuteAsync(message);
         }
     }
     catch (OperationCanceledException)
     {
         // expected
     }
 }
Exemplo n.º 2
0
        private async Task ProcessReceivedAsync(IStorageConnection connection, ProcessingContext context)
        {
            var messages = await connection.GetReceivedMessagesOfNeedRetry();

            var hasException = false;

            foreach (var message in messages)
            {
                if (message.Retries > _options.FailedRetryCount)
                {
                    continue;
                }

                if (!hasException)
                {
                    try
                    {
                        _options.FailedCallback?.Invoke(MessageType.Subscribe, message.Name, message.Content);
                    }
                    catch (Exception ex)
                    {
                        hasException = true;
                        _logger.LogWarning("Failed call-back method raised an exception:" + ex.Message);
                    }
                }

                await _subscriberExecutor.ExecuteAsync(message);

                context.ThrowIfStopping();

                await context.WaitAsync(_delay);
            }
        }
Exemplo n.º 3
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));
            }
        }
Exemplo n.º 4
0
        private async Task ProcessReceivedAsync(IStorageConnection connection, ProcessingContext context)
        {
            context.ThrowIfStopping();

            var messages = await GetSafelyAsync(connection.GetReceivedMessagesOfNeedRetry);

            foreach (var message in messages)
            {
                await _subscriberExecutor.ExecuteAsync(message);

                await context.WaitAsync(_delay);
            }
        }
Exemplo n.º 5
0
 private void Processing()
 {
     try
     {
         foreach (var message in _receivedMessageQueue.GetConsumingEnumerable(_cts.Token))
         {
             _executor.ExecuteAsync(message, _cts.Token);
         }
     }
     catch (OperationCanceledException)
     {
         // expected
     }
 }
Exemplo n.º 6
0
        private async Task ProcessReceivedAsync(IStorageConnection connection, ProcessingContext context)
        {
            var messages = await connection.GetFailedReceivedMessages();

            var hasException = false;

            foreach (var message in messages)
            {
                if (message.Retries > _options.FailedRetryCount)
                {
                    continue;
                }

                if (!hasException)
                {
                    try
                    {
                        _options.FailedCallback?.Invoke(MessageType.Subscribe, message.Name, message.Content);
                    }
                    catch (Exception ex)
                    {
                        hasException = true;
                        _logger.LogWarning("Failed call-back method raised an exception:" + ex.Message);
                    }
                }

                using (var transaction = connection.CreateTransaction())
                {
                    var ret = await _subscriberExecutor.ExecuteAsync(message);

                    if (ret.Succeeded)
                    {
                        _stateChanger.ChangeState(message, new SucceededState(), transaction);
                    }
                    else
                    {
                        message.Retries++;
                        message.Content = Helper.AddExceptionProperty(message.Content, ret.Exception);
                        transaction.UpdateMessage(message);
                    }
                    await transaction.CommitAsync();
                }

                context.ThrowIfStopping();

                await context.WaitAsync(_delay);
            }
        }
Exemplo n.º 7
0
 private void Processing()
 {
     try
     {
         foreach (var message in _receivedMessageQueue.GetConsumingEnumerable(_cts.Token))
         {
             try
             {
                 _executor.ExecuteAsync(message);
             }
             catch (Exception ex)
             {
                 _logger.ExceptionOccuredWhileExecuting(message.Name, ex);
             }
         }
     }
     catch (OperationCanceledException)
     {
         // expected
     }
 }
Exemplo n.º 8
0
        private async Task ProcessReceivedAsync(IStorageConnection connection, ProcessingContext context)
        {
            var messages = await connection.GetReceivedMessagesOfNeedRetry();

            var hasException = false;

            foreach (var message in messages)
            {
                if (message.Retries > _options.FailedRetryCount)
                {
                    continue;
                }

                using (var transaction = connection.CreateTransaction())
                {
                    var result = await _subscriberExecutor.ExecuteAsync(message);

                    if (result.Succeeded)
                    {
                        _stateChanger.ChangeState(message, new SucceededState(), transaction);
                        _logger.LogInformation("The message was execute successfully during the retry. MessageId:" + message.Id);
                    }
                    else
                    {
                        message.Content = Helper.AddExceptionProperty(message.Content, result.Exception);
                        message.Retries++;
                        if (message.StatusName == StatusName.Scheduled)
                        {
                            message.ExpiresAt  = GetDueTime(message.Added, message.Retries);
                            message.StatusName = StatusName.Failed;
                        }
                        transaction.UpdateMessage(message);

                        if (message.Retries >= _options.FailedRetryCount)
                        {
                            _logger.LogError($"[Subscriber]The message still executed failed after {_options.FailedRetryCount} retries. " +
                                             "We will stop retrying to execute the message. message id:" + message.Id);

                            if (message.Retries == _options.FailedRetryCount)
                            {
                                if (!hasException)
                                {
                                    try
                                    {
                                        _options.FailedThresholdCallback?.Invoke(MessageType.Subscribe, message.Name, message.Content);
                                    }
                                    catch (Exception ex)
                                    {
                                        hasException = true;
                                        _logger.LogWarning("Failed call-back method raised an exception:" + ex.Message);
                                    }
                                }
                            }
                        }
                    }
                    await transaction.CommitAsync();
                }

                context.ThrowIfStopping();

                await context.WaitAsync(_delay);
            }
        }