//private static void AddErrorReasonToContent(CapReceivedMessage message, Exception exception)
        //{
        //    message.Content = Helper.AddExceptionProperty(message.Content, exception);
        //}

        private async Task InvokeConsumerMethodAsync(MediumMessage message, ConsumerExecutorDescriptor descriptor, CancellationToken cancellationToken)
        {
            var consumerContext  = new ConsumerContext(descriptor, message.Origin);
            var tracingTimestamp = TracingBefore(message.Origin, descriptor.MethodInfo);

            try
            {
                var ret = await Invoker.InvokeAsync(consumerContext, cancellationToken);

                TracingAfter(tracingTimestamp, message.Origin, descriptor.MethodInfo);

                if (!string.IsNullOrEmpty(ret.CallbackName))
                {
                    var header = new Dictionary <string, string>()
                    {
                        [Headers.CorrelationId]       = message.Origin.GetId(),
                        [Headers.CorrelationSequence] = (message.Origin.GetCorrelationSequence() + 1).ToString()
                    };

                    await _provider.GetService <ICapPublisher>().PublishAsync(ret.CallbackName, ret.Result, header, cancellationToken);
                }
            }
            catch (OperationCanceledException)
            {
                //ignore
            }
            catch (Exception ex)
            {
                var e = new SubscriberExecutionFailedException(ex.Message, ex);

                TracingError(tracingTimestamp, message.Origin, descriptor.MethodInfo, e);

                throw e;
            }
        }
Esempio n. 2
0
        private static ConsumerExecutorDescriptor InitDescriptor(
            TopicAttribute attr,
            MethodInfo methodInfo,
            TypeInfo implType)
        {
            var descriptor = new ConsumerExecutorDescriptor
            {
                Attribute    = attr,
                MethodInfo   = methodInfo,
                ImplTypeInfo = implType
            };

            return(descriptor);
        }
Esempio n. 3
0
        /// <summary>
        /// Attempts to get the topic exector associated with the specified topic name and group name from the <see cref="Entries"/>.
        /// </summary>
        /// <param name="topicName">The topic name of the value to get.</param>
        /// <param name="groupName">The group name of the value to get.</param>
        /// <param name="matchTopic">topic exector of the value.</param>
        /// <returns>true if the key was found, otherwise false. </returns>
        public bool TryGetTopicExector(string topicName, string groupName,
                                       out ConsumerExecutorDescriptor matchTopic)
        {
            if (Entries == null)
            {
                throw new ArgumentNullException(nameof(Entries));
            }

            matchTopic = null;

            if (Entries.TryGetValue(groupName, out var groupMatchTopics))
            {
                matchTopic = groupMatchTopics.FirstOrDefault(x => x.Attribute.Name == topicName);
                return(matchTopic != null);
            }
            return(false);
        }
        private static ConsumerExecutorDescriptor InitDescriptor(
            TopicAttribute attr,
            MethodInfo methodInfo,
            TypeInfo implType,
            TypeInfo serviceTypeInfo,
            IList <ParameterDescriptor> parameters)
        {
            var descriptor = new ConsumerExecutorDescriptor
            {
                Attribute       = attr,
                MethodInfo      = methodInfo,
                ImplTypeInfo    = implType,
                ServiceTypeInfo = serviceTypeInfo,
                Parameters      = parameters
            };

            return(descriptor);
        }
        public async Task <OperateResult> DispatchAsync(MediumMessage message, ConsumerExecutorDescriptor descriptor, CancellationToken cancellationToken)
        {
            bool          retry;
            OperateResult result;

            do
            {
                var executedResult = await ExecuteWithoutRetryAsync(message, descriptor, cancellationToken);

                result = executedResult.Item2;
                if (result == OperateResult.Success)
                {
                    return(result);
                }
                retry = executedResult.Item1;
            } while (retry);

            return(result);
        }
Esempio n. 6
0
        /// <summary>
        /// Attempts to get the topic executor associated with the specified topic name and group name from the
        /// <see cref="Entries" />.
        /// </summary>
        /// <param name="topicName">The topic name of the value to get.</param>
        /// <param name="groupName">The group name of the value to get.</param>
        /// <param name="matchTopic">topic executor of the value.</param>
        /// <returns>true if the key was found, otherwise false. </returns>
        public bool TryGetTopicExecutor(string topicName, string groupName,
                                        out ConsumerExecutorDescriptor matchTopic)
        {
            if (Entries == null)
            {
                throw new ArgumentNullException(nameof(Entries));
            }

            matchTopic = null;

            if (Entries.TryGetValue(groupName, out var groupMatchTopics))
            {
                matchTopic = _selector.SelectBestCandidate(topicName, groupMatchTopics);

                return(matchTopic != null);
            }

            return(false);
        }
Esempio n. 7
0
        private ConsumerExecutorDescriptor InitDescriptor(
            TopicAttribute attr,
            MethodInfo methodInfo,
            TypeInfo implType,
            TypeInfo serviceTypeInfo,
            IList <ParameterDescriptor> parameters,
            TopicAttribute classAttr = null)
        {
            var descriptor = new ConsumerExecutorDescriptor
            {
                Attribute       = attr,
                ClassAttribute  = classAttr,
                MethodInfo      = methodInfo,
                ImplTypeInfo    = implType,
                ServiceTypeInfo = serviceTypeInfo,
                Parameters      = parameters,
                TopicNamePrefix = _capOptions.TopicNamePrefix
            };

            return(descriptor);
        }
        private async Task <(bool, OperateResult)> ExecuteWithoutRetryAsync(MediumMessage message, ConsumerExecutorDescriptor descriptor, CancellationToken cancellationToken)
        {
            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            cancellationToken.ThrowIfCancellationRequested();

            try
            {
                var sp = Stopwatch.StartNew();

                await InvokeConsumerMethodAsync(message, descriptor, cancellationToken);

                sp.Stop();

                await SetSuccessfulState(message);

                _logger.ConsumerExecuted(sp.Elapsed.TotalMilliseconds);

                return(false, OperateResult.Success);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"An exception occurred while executing the subscription method. Topic:{message.Origin.GetName()}, Id:{message.DbId}");

                return(await SetFailedState(message, ex), OperateResult.Failed(ex));
            }
        }
Esempio n. 9
0
 /// <summary>
 /// create a new instance of  <see cref="ConsumerContext" /> .
 /// </summary>
 /// <param name="descriptor">consumer method descriptor. </param>
 /// <param name="message"> received message.</param>
 public ConsumerContext(ConsumerExecutorDescriptor descriptor, MessageContext message)
 {
     ConsumerDescriptor = descriptor ?? throw new ArgumentNullException(nameof(descriptor));
     DeliverMessage     = message ?? throw new ArgumentNullException(nameof(message));
 }